def __init__(self, path, contest_id, force=False): self.path = path self.contest_id = contest_id self.force = force self.file_cacher = FileCacher() self.loader = YamlLoader(self.file_cacher, False, None, None)
def __init__(self, contest_id, spool_dir): self.contest_id = contest_id self.spool_dir = spool_dir self.upload_dir = os.path.join(self.spool_dir, "upload") self.contest = None self.submissions = None self.file_cacher = FileCacher()
def __init__(self, shard): logger.initialize(ServiceCoord("Worker", shard)) Service.__init__(self, shard, custom_logger=logger) self.file_cacher = FileCacher(self) self.task_type = None self.work_lock = threading.Lock() self.session = None
def __init__(self, drop, import_source, only_files, no_files): self.drop = drop self.only_files = only_files self.no_files = no_files self.import_source = import_source self.import_dir = import_source self.file_cacher = FileCacher()
def __init__(self, drop, modif, path, user_number): self.drop = drop self.modif = modif self.path = path self.user_number = user_number self.file_cacher = FileCacher() self.loader = YamlLoader(self.file_cacher, drop, modif, user_number)
def __init__(self, drop, import_source, only_files, no_files, no_submissions): self.drop = drop self.only_files = only_files self.no_files = no_files self.no_submissions = no_submissions self.import_source = import_source self.import_dir = import_source self.file_cacher = FileCacher() configure_mappers()
def __init__(self, shard): logger.initialize(ServiceCoord("TestFileCacher", shard)) TestService.__init__(self, shard, custom_logger=logger) # Assume we store the cache in "./cache/fs-cache-TestFileCacher-0/" self.cache_base_path = os.path.join(config.cache_dir, "fs-cache-TestFileCacher-0") self.cache_path = None self.content = None self.fake_content = None self.digest = None self.file_obj = None self.file_cacher = FileCacher(self)
def __init__(self, contest_id, export_target, skip_submissions, skip_user_tests, light): self.contest_id = contest_id self.skip_submissions = skip_submissions self.skip_user_tests = skip_user_tests self.light = light # If target is not provided, we use the contest's name. if export_target == "": with SessionGen(commit=False) as session: contest = Contest.get_from_id(self.contest_id, session) self.export_target = "dump_%s.tar.gz" % contest.name else: self.export_target = export_target self.file_cacher = FileCacher()
def extract_complexity(task_id, file_lengther=None): """Extract the complexity of all submissions of the task. The results are stored in a file task_<id>.info task_id (int): the id of the task we are interested in. file_lengther (class): a File-like object that tell the dimension of the input (see example above for how to write one). return (int): 0 if operation was successful. """ with SessionGen() as session: task = Task.get_from_id(task_id, session) if task is None: return -1 # Extracting the length of the testcase. file_cacher = FileCacher() testcases_lengths = [ file_length(testcase.input, file_cacher, file_lengther) for testcase in task.testcases ] file_cacher.purge_cache() # Compute the complexity of the solutions. with open("task_%s.info" % task_id, "wt") as info: for submission in task.contest.get_submissions(): if submission.task_id == task_id and \ submission.evaluated(): print submission.user.username result = extract_complexity_submission( testcases_lengths, submission) if result[1] is None: continue info.write("Submission: %s" % submission.id) info.write(" - user: %15s" % submission.user.username) info.write(" - task: %s" % task.name) if result[0] is not None: info.write(" - score: %6.2lf" % result[0]) info.write(" - complexity: %20s" % complexity_to_string(result[1])) if result[2] is not None: info.write(" - confidence %5.1lf" % result[2]) info.write("\n") return 0
def file_length(digest, file_cacher=None, file_lengther=None): """Compute the length of the file identified by digest. digest (string): the digest of the file. file_cacher (FileCacher): the cacher to use, or None. file_lengther (class): a File-like object that tell the dimension of the input (see example above for how to write one). return (int): the length of the tile. """ if file_cacher is None: file_cacher = FileCacher() if file_lengther is None: file_lengther = FileLengther lengther = file_lengther() file_cacher.get_file(digest, file_obj=lengther) return lengther.tell()
def run(contest_id): session = Session() contest = Contest.get_from_id(contest_id, session) task_by_team = set() task_by_lang = set() task_dir = os.path.join(os.path.dirname(__file__), "tasks") for t in os.listdir(task_dir): if t.endswith('.json'): task = t[:-5] task_path = os.path.join(task_dir, t) with open(task_path) as task_file: data = json.load(task_file) if "teams" in data: for team, v in data["teams"].iteritems(): for lang in v: task_by_team.add((task, lang, team)) if "langs" in data: for lang, v in data["langs"].iteritems(): for team in v: task_by_lang.add((task, lang, team)) if task_by_team != task_by_lang: print "ERROR: data in 'tasks' is not self-consistent" print repr(task_by_team - task_by_lang) print repr(task_by_lang - task_by_team) return team_by_task = set() team_by_lang = set() team_dir = os.path.join(os.path.dirname(__file__), "teams") for t in os.listdir(team_dir): if t.endswith('.json'): team = t[:-5] team_path = os.path.join(team_dir, t) with open(team_path) as team_file: data = json.load(team_file) if "tasks" in data: for task, v in data["tasks"].iteritems(): for lang in v: team_by_task.add((task, lang, team)) if "langs" in data: for lang, v in data["langs"].iteritems(): for task in v: team_by_lang.add((task, lang, team)) if team_by_task != team_by_lang: print "ERROR: data in 'teams' is not self-consistent" print repr(team_by_task - team_by_lang) print repr(team_by_lang - team_by_task) return if task_by_team != team_by_task: print "ERROR: data in 'tasks' and 'teams' is different" print repr(task_by_team - team_by_task) print repr(team_by_task - task_by_team) return data_by_lang = set() data_by_team = set() data_dir = os.path.join(os.path.dirname(__file__), "data") for task in os.listdir(data_dir): if os.path.isdir(os.path.join(data_dir, task)): for f in os.listdir(os.path.join(data_dir, task, "by_lang")): # f == "lang (team).pdf" lang, team = re.findall( "^([A-Za-z0-9_]+) \(([A-Za-z0-9_]+)\)\.pdf$", f)[0] data_by_lang.add((task, lang, team)) for f in os.listdir(os.path.join(data_dir, task, "by_team")): # f == "team (lang).pdf" team, lang = re.findall( "^([A-Za-z0-9_]+) \(([A-Za-z0-9_]+)\)\.pdf$", f)[0] data_by_team.add((task, lang, team)) if data_by_lang != data_by_team: print "ERROR: PDF files in 'data' are not complete" print repr(data_by_lang - data_by_team) print repr(data_by_team - data_by_lang) return if task_by_team != data_by_lang: print "ERROR: PDF files in 'data' do not match JSON data" print repr(task_by_team - data_by_lang) print repr(data_by_lang - task_by_team) return print "Hooray! Data is consistent!" # Pick one at random: they're all equal. translations = task_by_team # Determine language codes used in CMS. codes = dict() # Read JSON files in 'tasks' again as it provides data already # grouped as we need it, and not simply as a list of tuples. for t in os.listdir(task_dir): if t.endswith('.json'): task = t[:-5] task_path = os.path.join(task_dir, t) with open(task_path) as task_file: data = json.load(task_file) if "langs" in data: for lang, v in data["langs"].iteritems(): if len(v) == 0: pass elif len(v) == 1 and v[0] != official_team: for team in v: codes[(task, lang, team)] = "%s" % lang else: for team in v: codes[(task, lang, team)] = "%s_%s" % (lang, ioi_to_iso2[team]) # Store the files as Statement objects. file_cacher = FileCacher() for task, lang, team in translations: if team == official_team: assert lang == "en" digest = file_cacher.put_file( path=os.path.join(data_dir, task, "by_lang", "%s (%s).pdf" % (lang, team)), description="Statement for task %s" % task) else: digest = file_cacher.put_file( path=os.path.join(data_dir, task, "by_lang", "%s (%s).pdf" % (lang, team)), description= "Statement for task %s, translated into %s (%s) by %s (%s)" % (task, langs[lang], lang, teams[team], team)) s = Statement(codes[(task, lang, team)], digest, task=contest.get_task(task)) session.add(s) session.commit() primary = dict() # Retrieve the statements selected by each team. for t in os.listdir(team_dir): if t.endswith('.json'): team = t[:-5] team_path = os.path.join(team_dir, t) with open(team_path) as team_file: data = json.load(team_file) for team2, lang, task in data.get("selected", []): # A team could have selected a statement that later got removed. if (task, lang, team2) in codes: primary.setdefault(team, {}).setdefault( task, []).append(codes[(task, lang, team2)]) # Add the ones they uploaded themselves. for task, lang, team in translations: # Don't worry about duplicates, CWS filters them out. primary.setdefault(team, {}).setdefault(task, []).append(codes[(task, lang, team)]) # Set the primary statements for tasks (i.e. the ones of the official team) for task, primary2 in primary.get(official_team, {}).iteritems(): contest.get_task(task).primary_statements = json.dumps(primary2) # Set the primary statements for teams for team, primary2 in primary.iteritems(): session.execute( "UPDATE users SET primary_statements = '%s' WHERE username LIKE '%s%%';" % (json.dumps(primary2), team)) session.commit() print "Statements stored in the DB!"