def commandline(): import optparse op = optparse.OptionParser() op.add_option('-b', '--basedir') op.add_option('-a', '--add-replays', action='store_true') op.add_option('-l', '--list', action='store_true') op.add_option('-t', '--test', action='store_true') op.add_option('-v', '--verbose', action='store_true') op.add_option('--dump-gamedata', action='store_true') op.add_option('--rebuild', action='store_true') options, args = op.parse_args() rdb = ReplayDB(options.basedir) if options.test: test(rdb) if options.rebuild: replay_ids = rdb.get_replay_ids() targets = args for n, replay_id in enumerate(replay_ids): print 'making data for %s (%d/%d)' % ( replay_id, n+1, len(replay_ids)) rdb.make_data(replay_id, targets) if options.add_replays: for replay_path in args: rp_data = file(replay_path).read() replay_id = libw3g.get_replay_id(rp_data) print 'adding replay %s...' % replay_id try: rdb.add_replay(rp_data, { 'file_timestamp': libw3g.get_timestamp(replay_path)}) errors = rdb.get_errors(replay_id) if errors: print 'errors: %s' % errors except DuplicateReplayException: print 'skipped (already exists)' if options.list: for replay_id in rdb.get_replay_ids(): print replay_id, ('error' if replay_id not in rdb.replays else '') if options.dump_gamedata: for replay in search_replays(args, rdb.replays): dump_gamedata(replay, options.verbose)
def add_replay(self, data, metadata, force=False): replay_id = metadata['replay_id'] = libw3g.get_replay_id(data) replay_path = self.rp_path(replay_id, FN_REPLAY) if not exists(dirname(replay_path)): os.makedirs(dirname(replay_path)) elif not force: raise DuplicateReplayException() self.rp_file(replay_id, FN_REPLAY, 'w').write(data) try: self.set_metadata(replay_id, metadata) self.make_replaydata(replay_id) self.make_gamedata(replay_id) self.load_replay(replay_id) errors = None except: errors = traceback.format_exc() finally: self.set_errors(replay_id, errors)
def exists(self, path): replay_id = libw3g.get_replay_id(file(path, 'rb').read()) response = urllib2.urlopen( self.cgiurl+'/exists/%s' % replay_id).read().strip() return str(response)=='True'