Skip to content

Commit

Permalink
Fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cip999 committed Jan 17, 2023
1 parent d41de48 commit 4b271f1
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 92 deletions.
8 changes: 4 additions & 4 deletions p2d/domjudge_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import sys
import tempfile
import yaml
import logging

from p2d._version import __version__
from p2d.logging_utils import logger

def generate_externalid(problem):
random_suffix = ''.join(random.choice(string.ascii_uppercase) for _ in range(6))
Expand Down Expand Up @@ -37,11 +37,11 @@ def update_problem_api(package_zip, problem_domjudge_id, credentials):
credentials)

if res.status_code != 200 or not res.json()['problem_id']:
logger.error('Error sending the package to the DOMjudge server: %s.'
logging.error('Error sending the package to the DOMjudge server: %s.'
% res.json())
return False
else:
logger.debug('Successfully sent the package to the DOMjudge server.')
logging.debug('Successfully sent the package to the DOMjudge server.')
return True

# Adds an "empty" problem to a contest.
Expand All @@ -67,7 +67,7 @@ def add_problem_to_contest_api(problem, credentials):
os.unlink(problem_yaml)

if res.status_code != 200:
logger.error('Error adding the problem to the contest: %s.' % res.json())
logging.error('Error adding the problem to the contest: %s.' % res.json())
return False

problem['domjudge_id'] = res.json()[0]
Expand Down
14 changes: 7 additions & 7 deletions p2d/generate_domjudge_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import xml.etree.ElementTree
import yaml
import zipfile
import logging

from p2d._version import __version__
from p2d.logging_utils import logger
from p2d import tex_utilities

RESOURCES_PATH = os.path.join(
Expand Down Expand Up @@ -55,12 +55,12 @@
# - problemname-solution.{tex,pdf}
# params is a dictionary with keys contest_name, hide_balloon, hide_tlml.
def generate_domjudge_package(problem, domjudge, tex_dir, params):
logger.debug('Creating the DOMjudge package directory \'%s\'.' % domjudge)
logging.debug('Creating the DOMjudge package directory \'%s\'.' % domjudge)

problem_yaml_data = {}

# Metadata
logger.debug('Writing \'domjudge-problem.ini\'.')
logging.debug('Writing \'domjudge-problem.ini\'.')
ini_file = os.path.join(domjudge, 'domjudge-problem.ini')
ini_content = [
'short-name = %s' % problem['name'],
Expand All @@ -81,7 +81,7 @@ def generate_domjudge_package(problem, domjudge, tex_dir, params):
tex_utilities.generate_solution_pdf(problem, tex_dir, params)

# Tests
logger.debug('Copying the tests in the DOMjudge package.')
logging.debug('Copying the tests in the DOMjudge package.')

sample_dir = os.path.join(domjudge, 'data', 'sample')
secret_dir = os.path.join(domjudge, 'data', 'secret')
Expand All @@ -105,7 +105,7 @@ def generate_domjudge_package(problem, domjudge, tex_dir, params):
os.path.join(domjudge, 'output_validators', 'interactor.cpp'))
elif problem['checker']['name'] is not None:
checker_name = problem['checker']['name']
logger.debug('Standard checker \'%s\'.' % checker_name)
logging.debug('Standard checker \'%s\'.' % checker_name)

checker_name_match = re.match(r'std\:\:([a-z0-9]+)\.cpp', checker_name)
assert(checker_name_match)
Expand All @@ -117,7 +117,7 @@ def generate_domjudge_package(problem, domjudge, tex_dir, params):
problem_yaml_data['validator_flags'] = \
CHECKER_POLYGON2DOMJUDGE[checker_name]
else:
logger.debug('Custom checker.')
logging.debug('Custom checker.')
problem_yaml_data['validation'] = 'custom'
pathlib.Path(domjudge, 'output_validators').mkdir()
shutil.copyfile(
Expand All @@ -141,7 +141,7 @@ def generate_domjudge_package(problem, domjudge, tex_dir, params):

# Write problem.yaml
yaml_path = os.path.join(domjudge, 'problem.yaml')
logger.debug(
logging.debug(
'Writing into \'%s\' the dictionary %s'
% (yaml_path, problem_yaml_data))
with open(yaml_path, 'w', encoding='utf-8') as f:
Expand Down
6 changes: 3 additions & 3 deletions p2d/generate_testlib_for_domjudge.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import requests
import os
import re
import logging

from p2d._version import __version__
from p2d.logging_utils import logger

HEADER_COMMENT = '''\
// Modified by a script to work with DOMjudge.
Expand Down Expand Up @@ -121,12 +121,12 @@ def replace_function(lines, function):
# The applied patch is copied from
# https://github.com/cn-xcpc-tools/testlib-for-domjudge.
def generate_testlib_for_domjudge(dst_path):
logger.debug('Downloading testlib.h from github.')
logging.debug('Downloading testlib.h from github.')
req = requests.get(
'https://raw.githubusercontent.com/MikeMirzayanov/testlib/master/testlib.h')
lines = req.text.splitlines()

logger.debug('Patching testlib.')
logging.debug('Patching testlib.')
lines = add_header(lines, HEADER_COMMENT)
for exit_code in NEW_EXIT_CODES:
lines = replace_exit_code(lines, exit_code, NEW_EXIT_CODES[exit_code])
Expand Down
37 changes: 18 additions & 19 deletions p2d/p2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import pathlib
import sys
from argparse import ArgumentParser
import logging

from p2d._version import __version__
from p2d.logging_utils import logger
from p2d import (domjudge_api,
generate_domjudge_package,
generate_testlib_for_domjudge,
parse_polygon_package,
polygon_api,
p2d_utils,
logging_utils,
p2d_utils,
tex_utilities)
RESOURCES_PATH = os.path.join(
os.path.split(os.path.realpath(__file__))[0], 'resources')
Expand All @@ -38,13 +38,13 @@ def prepare_argument_parser():


def p2d(args):
logging_utils.configure_logging(args.verbosity)
p2d_utils.configure_logging(args.verbosity)

# Downloading and patching testlib.h if necessary.
testlib_h = os.path.join(RESOURCES_PATH, 'testlib.h')
if not os.path.isfile(testlib_h) or args.update_testlib:
generate_testlib_for_domjudge.generate_testlib_for_domjudge(testlib_h)
logger.info('The file testlib.h was successfully downloaded and patched. The local version can be found at \'%s\'.' % testlib_h)
logging.info('The file testlib.h was successfully downloaded and patched. The local version can be found at \'%s\'.' % testlib_h)

contest_dir = args.contest_directory

Expand All @@ -56,7 +56,7 @@ def p2d(args):
and args.from_contest is None \
and not args.pdf_contest \
and not args.clear_dir and not args.clear_domjudge_ids:
logger.error('At least one of the flags --polygon, --convert, --domjudge, --from-contest, --contestpdf, --clear-dir, --clear-domjudge-ids is necessary.')
logging.error('At least one of the flags --polygon, --convert, --domjudge, --from-contest, --contestpdf, --clear-dir, --clear-domjudge-ids is necessary.')
exit(1)

if args.clear_dir:
Expand All @@ -66,7 +66,7 @@ def p2d(args):
p2d_utils.remove_problem_data(problem, contest_dir)

p2d_utils.save_config_yaml(config, contest_dir)
logger.info('Deleted the problems\' data from \'%s\'.' % contest_dir)
logging.info('Deleted the problems\' data from \'%s\'.' % contest_dir)

if args.clear_domjudge_ids:
for problem in config['problems']:
Expand All @@ -77,13 +77,13 @@ def p2d(args):
problem.pop('domjudge_externalid', None)

p2d_utils.save_config_yaml(config, contest_dir)
logger.info('Deleted the DOMjudge IDs from config.yaml.')
logging.info('Deleted the DOMjudge IDs from config.yaml.')

if (args.polygon or args.from_contest) \
and ('polygon' not in config
or 'key' not in config['polygon']
or 'secret' not in config['polygon']):
logger.error('The entries polygon:key and polygon:secret must be '
logging.error('The entries polygon:key and polygon:secret must be '
'present in config.yaml to access Polygon problems.')
exit(1)

Expand All @@ -92,7 +92,7 @@ def p2d(args):
or 'server' not in config['domjudge']
or 'username' not in config['domjudge']
or 'password' not in config['domjudge']):
logger.error('The entries domjudge:contest_id, domjudge:server, '
logging.error('The entries domjudge:contest_id, domjudge:server, '
'domjudge:username, domjudge:password must be present '
'in config.yaml to upload problems on DOMjudge.')
exit(1)
Expand Down Expand Up @@ -120,10 +120,10 @@ def p2d(args):

problem_selected_exists = True
print('Processing problem \033[96m' + problem['name'] + '\033[39m') # Cyan
logging_utils.Pol2DomLoggingFormatter.INDENT += 1
p2d_utils.Pol2DomLoggingFormatter.INDENT += 1

if 'label' not in problem:
logger.warning('The problem does not have a label.')
logging.warning('The problem does not have a label.')

if args.polygon:
if args.no_cache:
Expand Down Expand Up @@ -151,11 +151,10 @@ def p2d(args):
contest_dir, 'domjudge', problem['name']), problem)
p2d_utils.save_config_yaml(config, contest_dir)

logging_utils.Pol2DomLoggingFormatter.INDENT -= 1
p2d_utils.Pol2DomLoggingFormatter.INDENT -= 1

if args.problems and not problem_selected_exists:
logger.warning('The problem specified with --problem does not appear '
'in config.yaml.')
logging.warning('None of the problem names specified with --problems appears in config.yaml.')
return

if args.problems:
Expand All @@ -166,16 +165,16 @@ def p2d(args):

# Guidelines for error tracing and logging:
#
# Use logging_utils.logger everywhere for info/warning/error printing.
# Use logging everywhere for info/warning/error printing.
# Do not use print.
# Use exceptions when appropriate.
#
# For errors, use logger.error followed by exit(1) or raise an exception.
# For warnings, use logger.warning.
# For errors, use logging.error followed by exit(1) or raise an exception.
# For warnings, use logging.warning.
#
# For information:
# - Use logger.info in this p2d.py (for useful information).
# - Use logger.debug in all other files (and for not-so-useful information
# - Use logging.info in this p2d.py (for useful information).
# - Use logging.debug in all other files (and for not-so-useful information
# in this file).
def main():
args = prepare_argument_parser().parse_args()
Expand Down
Loading

0 comments on commit 4b271f1

Please sign in to comment.