def list_ids(config, changes_path, action, ids_path=None): ''' Executes print actions related to the listing of election ids from the changes file. Available actions are 'tree', 'edges', 'leaves' and 'all'. ''' changes = get_changes_config(changes_path) tree = get_changes_tree(changes) l = [] def add_others(l, ids_path): other_ids = [] with open(ids_path, mode='r', encoding="utf-8", errors='strict') as f: for line in f: line = line.strip() other_ids.append(line) l = list(set(other_ids + l)) l.sort() return l if action == 'tree': print(serialize(tree)) return elif action == 'leaves_ancestors': list_leaves(tree, l) l = add_others(l, ids_path) ancestors = dict() for election_id in l: ancestors[election_id] = get_ancestors(tree, election_id) + [election_id] for election_id in l: print(",".join(ancestors[election_id])) return if action == 'edges': list_edges(tree, l) elif action == 'leaves': list_leaves(tree, l) elif action == 'all_with_others': list_all(tree, l) l = add_others(l, ids_path) elif action == 'all': list_all(tree, l) l.sort() for election_id in l: print(election_id)
def check_diff_changes(elections_path, election_id, calculated_election_config): election_config = get_election_config(elections_path, election_id) if serialize(election_config) != serialize(calculated_election_config): print("calculated election config differs for election %s. showing diff(config, calculated)" % election_id) print(diff(election_config, calculated_election_config)) print(serialize(calculated_election_config))
def write_agora_results_files(config, changes_path, elections_path, ids_path): ''' Checks that the configuration set ''' # get changes and tree data changes = get_changes_config(changes_path) tree = get_changes_tree(changes) node_changes = get_node_changes(tree, changes) election_ids = get_list(tree, list_all) final_ids = get_list(tree, list_leaves) parse_parity_config(config) # get all the ids other_ids = [] if ids_path is not None: with open(ids_path, mode='r', encoding="utf-8", errors='strict') as f: for line in f: line = line.strip() other_ids.append(line) # add to the tree the id as a root leave if not present if line not in election_ids: tree[line] = dict() else: print("WARNING: ids_path not supplied") # join both list of ids, removing duplicates, and sorting the list final_ids = list(set(final_ids + other_ids)) final_ids.sort() election_ids = list(set(election_ids + other_ids)) election_ids.sort() # calculate a configuration for each election, including middle steps, so # that later on the answers between election can be tracked and collected # by hash hashed_election_configs = dict() for election_id in election_ids: election_changes, ancestors = get_changes_chain_for_election_id( election_id, config, tree, node_changes) election_config = get_election_config(elections_path, ancestors[0]) apply_changes_hashing( election_id, election_config, election_changes, ancestors, elections_path) hashed_election_configs[election_id] = dict( config=election_config, ancestors=ancestors ) # iterate and process the results config for all final elections for election_id in final_ids: election_config = hashed_election_configs[election_id]['config'] ancestors = hashed_election_configs[election_id]['ancestors'] results_config = copy.deepcopy(config['agora_results_config']) # apply first global_prechanges for change, kwargs in config['global_prechanges']: apply_results_config_prechanges( change, kwargs, ancestors, election_id, election_config, results_config) post_process_results_config( config, elections_path, ancestors, election_id, results_config, election_config, hashed_election_configs) epath = os.path.join(elections_path, "%s.config.results.json" % election_id) with open(epath, mode='w', encoding="utf-8", errors='strict') as f: f.write(serialize(results_config))
if not args.admin_format: output_path = os.path.join( args.output_path, str(election['id']) + ".config.json") else: output_path = os.path.join(args.output_path, str(i) + ".json") auth_config_path = os.path.join( args.output_path, str(i) + ".config.json") auth_config = config['authapi']['event_config'] with open(auth_config_path, mode='w', encoding="utf-8", errors='strict') as f: f.write(serialize(auth_config)) auth_census_path = os.path.join( args.output_path, str(i) + ".census.json") with open(auth_census_path, mode='w', encoding="utf-8", errors='strict') as f: f.write("[]") with open(output_path, mode='w', encoding="utf-8", errors='strict') as f: f.write(serialize(election))