Created
February 16, 2017 18:29
-
-
Save ebetica/966b036a678dce8b5abe13f0d11bcd87 to your computer and use it in GitHub Desktop.
Example script to go through some starcraft replays and grab infomation about it, dumping into a CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script tries as best as possible to filter out bad replays | |
# Pass it a subdir, and it will read all '.rep' files, and spit out a list | |
# of the corrupt files in stdout | |
from __future__ import print_function | |
from pyreplib import replay | |
from itertools import repeat | |
from multiprocessing import Pool, Process, Pipe | |
from multiprocessing.pool import ThreadPool | |
from Queue import Queue | |
import os | |
import sys | |
import datetime | |
release = datetime.datetime(2008, 11, 25) # release date of 1.16 | |
def analyze(repname, conn): | |
rep = replay.Replay(repname) | |
races = [rep.players[0].race_name, rep.players[1].race_name] | |
conn.send(", ".join(races + [rep.map_name])) | |
def filterfiles(args): | |
root, fname = args | |
if '.rep' in fname and '.lock' not in fname: | |
return os.path.join(root, fname) | |
return None | |
pool = Pool() | |
flst = [] | |
for root, dirs, files in os.walk(sys.argv[1]): | |
flst += [f for f in pool.map(filterfiles, zip(repeat(root), files)) | |
if f is not None] | |
q = Queue() | |
# analyze sometimes segfaults, so a Pool will break | |
# Instead, just start a new process for each replay | |
def tpfunc(repname): | |
conn, send = Pipe() | |
t = Process(target=analyze, args=(repname, send)) | |
t.start() | |
t.join() | |
if conn.poll(5): | |
res = conn.recv() | |
if res is not None: | |
q.put(res) | |
def cb(v): | |
q.put(None) | |
# Threadpool makes sure we don't accidentally forkbomb ourselves | |
tp = ThreadPool() | |
tp.map_async(tpfunc, flst, callback=cb) | |
info = [] | |
while True: | |
item = q.get() | |
if item is None: | |
break | |
item = ''.join([i if 31 < ord(i) < 127 else '?' for i in item]) | |
info.append(item) | |
print("\n".join(info)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment