def IsInitialized(): """Check if the tool has been inititalized with a contest. This function checks that the current configuration file exists and it contains valid information, which includes a contest id, problem ids, problem names and middleware tokens. Returns: True if a contest has been initialized, false otherwise. """ # Check if the current configuration file exists, otherwise the contest has # not been initialized yet. current_config_path = data_manager.ParametrizeConfigPath( constants.CURRENT_CONFIG_PATH) if not os.path.isfile(current_config_path): return False # Read the current config and check that all contest-related fields exist. current_config = data_manager.ReadData() if ('cookie' not in current_config or 'middleware_tokens' not in current_config or 'contest_id' not in current_config or 'problems' not in current_config): return False # Use the contest validator to check the data, ignoring the error messages. return _ValidateContestData(current_config['middleware_tokens'], current_config['problems'])
def ClearContest(): # Erase the current configuration file if it exists and is a file, otherwise # show a warning if the configuration is not a regular file (should not happen # under normal conditions). try: current_config_path = data_manager.ParametrizeConfigPath( constants.CURRENT_CONFIG_PATH) if os.path.isfile(current_config_path): os.remove(current_config_path) elif os.path.exists(current_config_path): sys.stderr.write( 'Warning: Cannot erase current configuration file "{0}" ' 'because it is not a regular file.\n'.format( current_config_path)) except OSError as e: raise error.InternalError( 'OS error happened while deleting file "{0}": ' '{1}.\n'.format(filename, e))
def Initialize(tournament_id, contest_id, password=None): """Initialize configuration for the specified tournament or contest. This function initializes the tool for a contest. If the contest is None, the tool will be initialized for the current contest of the specified tournament. Either one of tournament_id or contest_id must be not None. The retrieved data is stored in the current configuration file. Args: tournament_id: ID of the tournament whose current contest must be initialized. contest_id: ID of the contest to initialize. If None, the server will ask for the current contest of the specified tournament. password: Password specified by the user, if any. Raises: error.ConfigurationError: If the contest data is invalid or incomplete. error.UserError: If no contest was specified and there is no running contest for the specified tournament. """ # Reset the current configuration file with the one provided by the user and # renew the cookie, so the middleware tokens are retrieved correctly. try: user_config_path = data_manager.ParametrizeConfigPath( constants.USER_CONFIG_PATH) current_config_path = data_manager.ParametrizeConfigPath( constants.CURRENT_CONFIG_PATH) shutil.copy(user_config_path, current_config_path) code_jam_login.Login(password) except OSError as e: raise error.InternalError( 'Configuration file {0} could not be created: ' '{1}.\n'.format(current_config_path, e)) # Read the current configuration file and extract the host and the cookie. try: contest_data = data_manager.ReadData() host = contest_data['host'] cookie = contest_data['cookie'] except KeyError as e: # Indicate that no host or cookie was configured and exit with error. raise error.ConfigurationError('No host or login cookie found in the ' 'configuration file: {0}.\n'.format(e)) # Get the current contest if no contest id was specified. If there is no # running contest show an error to the user. if contest_id is None: contest_id = GetCurrentContestId(host, cookie, tournament_id) if contest_id is None: raise error.UserError( 'No contest is running for tournament %s and no ' 'contest id was specified.\n' % tournament_id) sys.stdout.write( 'Initializing tool for current contest with id %s.\n' % contest_id) # Retrieve the problem list and validate the extracted contest data and exit # if there is any error. problems = _GetProblems(host, cookie, contest_id) _ValidateContestDataOrRaise(contest_data['middleware_tokens'], problems) # Add the contest id, the problems and the middleware tokens to the contest # data. contest_data['contest_id'] = contest_id contest_data['problems'] = problems # Finally, write the contest data to the current data file, and then # renew the cookie stored in the configuration. data_manager.WriteData(contest_data) sys.stdout.write('Contest {0} initialized successfully, {1} problem(s) ' 'retrieved.\n'.format(contest_id, len(problems)))