Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
dario2994 committed Jun 9, 2022
1 parent 297a2bb commit 1f59a90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ It must be a valid `yaml` file containing the following top-level keys:
Some more keys are added (and managed) by `p2d` for caching purposes. Namely each problem will also contains the additional keys: `polygon_version`, `domjudge_local_version`, `domjudge_server_version`.
These additional keys are managed by `p2d` and should not be touched by the user. In order to clear entirely the keys related to caching, use the flag `--clear-dir` (which will also clear the directory of the contest).

Moreover, each problem (after being uploaded for the first time on DOMjudge) will also have the keys `domjudge_id` and `domjudge_externalid`.
These keys can be deleted only by modifying the file (which may be necessary if, for example, the DOMjudge instance changes).
Moreover, each problem (after being uploaded for the first time on DOMjudge) is assigned a `domjudge_id` (which corresponds to the external ID of the problem in DOMjudge).
This key can be deleted only by modifying the file (which may be necessary if, for example, the DOMjudge instance changes).

See `examples/config.yaml` for a valid `config.yaml` file.

Expand Down
32 changes: 2 additions & 30 deletions p2d/domjudge_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ def update_problem_api(package_zip, problem_id, credentials):

# Adds an "empty" problem to a contest.
# Returns true if the problem was successfully added. In such case, it set
# the 'externalid' of the problem.
# the 'domjudge_id' of the problem.
# credentials is a dictionary with keys contest_id, server, username, password.
def add_problem_to_contest_api(problem, credentials):
api_address = '/api/v4/contests/%s/problems/add-data' % credentials['contest_id']
externalid = generate_externalid(problem)


with tempfile.NamedTemporaryFile(delete=False, suffix='.yaml', mode='w',
encoding='utf-8') as f:
problem_yaml = f.name
Expand All @@ -74,33 +73,6 @@ def add_problem_to_contest_api(problem, credentials):
logging.error('Error adding the problem to the contest: %s.' % res.json())
return False

problem['domjudge_id'] = res.json()[0]
problem['domjudge_externalid'] = externalid
problem['domjudge_id'] = externalid

return True


# TODO: Put this somewhere appropriate
# def debug_requests_on():
# HTTPConnection.debuglevel = 1
# logging.basicConfig()
# logging.getLogger().setLevel(logging.DEBUG)
# requests_log = logging.getLogger("requests.packages.urllib3")
# requests_log.setLevel(logging.DEBUG)
# requests_log.propagate = True

# def debug_requests_off():
# HTTPConnection.debuglevel = 0
# root_logger = logging.getLogger()
# root_logger.setLevel(logging.WARNING)
# root_logger.handlers = []
# requests_log = logging.getLogger("requests.packages.urllib3")
# requests_log.setLevel(logging.WARNING)
# requests_log.propagate = False

# @contextlib.contextmanager
# def debug_requests():
# '''Use with 'with'!'''
# debug_requests_on()
# yield
# debug_requests_off()
35 changes: 22 additions & 13 deletions p2d/p2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
RESOURCES_PATH = os.path.join(
os.path.split(os.path.realpath(__file__))[0], 'resources')

# TODO
# OK_SYMBOL = u'\u2705'
# NEUTRAL_SYMBOL = ' '
# ERROR_SYMBOL = u'\u274C'


def manage_download(config, polygon_dir, problem):
if 'polygon_id' not in problem:
Expand Down Expand Up @@ -192,21 +187,21 @@ def manage_domjudge(config, domjudge_dir, problem):
return

# Sending the problem package to the server.
assert('domjudge_id' in problem and 'domjudge_externalid' in problem)
assert('domjudge_id' in problem)
zip_file = os.path.join(domjudge_dir, problem['name'] + '.zip')
zip_file_copy = os.path.join(domjudge_dir,
problem['domjudge_externalid'] + '.zip')
problem['domjudge_id'] + '.zip')
shutil.copyfile(zip_file, zip_file_copy)

if not domjudge_api.update_problem_api(
zip_file_copy, problem['domjudge_externalid'], config['domjudge']):
zip_file_copy, problem['domjudge_id'], config['domjudge']):
logging.error('There was an error while updating the problem '
'in the DOMjudge server.')
return

problem['domjudge_server_version'] = local_version

logging.info('Updated the DOMjudge package on the server \'%s\', with externalid = \'%s\'.' % (config['domjudge']['server'], problem['domjudge_externalid']))
logging.info('Updated the DOMjudge package on the server \'%s\', with externalid = \'%s\'.' % (config['domjudge']['server'], problem['domjudge_id']))

def prepare_argument_parser():
parser = ArgumentParser(description='Utility script to import a whole contest from polygon into DOMjudge.')
Expand All @@ -218,7 +213,8 @@ def prepare_argument_parser():
parser.add_argument('--verbosity', choices=['debug', 'info', 'warning'],
default='info', help='Verbosity of the logs.')
parser.add_argument('--no-cache', action='store_true', help='If set, the various steps (polygon, convert, domjudge) are run even if they would not be necessary (according to the caching mechanism).')
parser.add_argument('--clear-dir', action='store_true', help='Whether to remove all the files and directory, apart from \'config.yaml\' from the contest directory (as a consequence, all the cache is deleted).')
parser.add_argument('--clear-dir', action='store_true', help='If set, all files and directories, apart from \'config.yaml\', in the contest directory are deleted (as a consequence, the cache is deleted).')
parser.add_argument('--clear-domjudge-ids', action='store_true', help='If set, the domjudge IDs saved in config.yaml (for the problems that were uploaded to the DOMjudge server) are deleted. As a consequence, next time the flag `--domjudge` is passed, the problems will be uploaded as new problems to DOMjudge. This should be used either if the DOMjudge server changed, if the DOMjudge contest changed, or if the problems were deleted in the DOMjudge server.')
parser.add_argument('--update-testlib', action='store_true', help='Whether to update the local version of testlib (syncing it with the latest version from the official github repository and patching it for DOMjudge).')

return parser
Expand All @@ -243,11 +239,16 @@ def prepare_argument_parser():
# problemname-statement.pdf
# problemname-solution.pdf
def p2d(args):
# Configuring logging
logging.basicConfig(
stream=sys.stdout,
format='%(levelname)s: %(message)s',
level=eval('logging.' + args.verbosity.upper())
)

requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# Downloading and patching testlib.h if necessary.
testlib_h = os.path.join(RESOURCES_PATH, 'testlib.h')
Expand Down Expand Up @@ -289,6 +290,15 @@ def save_config_yaml():
shutil.rmtree(dir_path)

logging.info('Deleted the content of \'%s\' apart from config.yaml.' % contest_dir)

if args.clear_domjudge_ids:
for problem in config['problems']:
problem['domjudge_server_version'] = -1
problem.pop('domjudge_id', None)

save_config_yaml()

logging.info('Deleted the DOMjudge IDs from config.yaml.')

if args.polygon and ('polygon' not in config
or 'key' not in config['polygon']
Expand All @@ -308,7 +318,7 @@ def save_config_yaml():
exit(1)

if not args.polygon and not args.convert and not args.domjudge \
and not args.clear_dir:
and not args.clear_dir and not args.clear_domjudge_ids:
logging.error('At least one of the flags --polygon, --convert, --domjudge, --clear is necessary.')
exit(1)

Expand Down Expand Up @@ -417,6 +427,5 @@ def main():


# TODO: Everything should be tested appropriately.
# TODO: Polish the documentation.
# TODO: Polish the documentation (reread README.md).
# TODO: The colors belong to what list?
# TODO: --clear-domjudge-ids.

0 comments on commit 1f59a90

Please sign in to comment.