<commit_before>import os
import json
import collections
import shutil
from shutil import ignore_patterns
import errno
import datetime
import time
import sys
import subprocess
from subprocess import check_output
import random
import string
import re
import jwrite
import electionUtils

'''
The script can be called with 1 to 3 arguments

[1] argument should be a either "mockElection" or "customElection",
    depending on whether it should be a simple test election without many arguments or not.
    To avoid misspelling, a mock election will be created as long as the argument contains the string "mock".

[2] argument (not needed for mock elections) should be a stringified JSON object containing the following elements
    (which are all included in a proper ElectionManifest.json):
        "title": string
        "description": string
        "startTime": string of format: "yy-mm-dd hh:mm UTC+0000", example: "2014-01-01 15:00 UTC+0000"
        "endTime": string of format: "yy-mm-dd hh:mm UTC+0000", example: "2018-11-10 16:00 UTC+0000"
        "voters": array/list of strings  
        "publishListOfVoters": true or false (boolean)
        "minChoicesPerVoter": positive integer
        "maxChoicesPerVoter": positive integer no higher than the amount of choices
        "question": string
        "choices": array/list of strings

[3] argument (optional) should be a stringified JSON object, with optional keys:
        "password": string encrypted with '<PASSWORD>'
        "userChosenRandomness" true or false (boolean)
        "hidden": true or false (boolean)
        "subdomain": a subdomain all the servers will be on, this will replace the ELS,
                therefore two election with the same subdomain should not be created (no check yet).
        "keys": array/list containing 1+<numberOfMixServers> (usually three mix servers) JSON objects
                containing 4 corresponding keypairs: {encryption_key, verification_key, signing_key, decryption_key}
'''

def copy(src, dest):
    try:
        shutil.copytree(src, dest, symlinks=False, ignore=ignore_patterns("*.py", "00", "01", "02", "tests"))
    except OSError as e:
        # source is a file, not a directory
        if e.errno == errno.ENOTDIR:
            shutil.copy(src, dest)
        else:
            print("Directory not copied. Error: %s" % e)

def getTime():
    utcTime = datetime.datetime.utcnow()
    return utcTime

def addSec(tm, secs):
    fulldate = tm + datetime.timedelta(seconds=secs)
    return fulldate

def setConfigFiles():
    global rootDirProject
    
    global sElectDir
    global electionConfig
    global electionInfo
    global electionInfoHidden
    global defaultManifest
    global electionURI
    global nginxConf
    global passList
    global nginxLog
    
    global manifest
    global collectingConf
    global bulletinConf
    global votingManifest
    global votingConf
    global authManifest
    global authConf
    global mixConf

    # the root dir is three folders back
    rootDirProject = os.path.realpath(__file__)
    for i in range(2):
        rootDirProject=os.path.split(rootDirProject)[0]
    
    # absolute paths
    sElectDir = rootDirProject + "/sElect"
    electionConfig = rootDirProject + "/_configFiles_/handlerConfigFile.json"
    electionInfo = rootDirProject + "/_configFiles_/electionInfo.json"
    defaultManifest = rootDirProject + "/_configFiles_/ElectionManifest.json"
    electionURI = rootDirProject + "/_configFiles_/electionsURI.json"
    electionInfoHidden = rootDirProject + "/elections_hidden/electionInfo.json"
    nginxConf =  rootDirProject + "/nginx_config/nginx_select.conf"
    passList =  rootDirProject + "/ElectionHandler/_data_/pwd.json"
    nginxLog = rootDirProject + "/nginx_config/log"
    
    # sElect (partial) mixFiles path
    manifest = "/_sElectConfigFiles_/ElectionManifest.json"
    collectingConf = "/CollectingServer/config.json"
    bulletinConf = "/BulletinBoard/config.json"
    votingManifest = "/VotingBooth/ElectionManifest.json"     
    votingConf = "/VotingBooth/config.json" 
    authManifest = "/Authenticator/ElectionManifest.json"     
    authConf = "/Authenticator/config.json" 
    mixConf = []
    
def getConfigData():
    #get duration and deployment status from handlerConfigFile
    global deployment
    global serverAddr
    global votingTime
    global mockVoters
    global numMix
    global createdElections
    global nginxPort
    global minChoices
    global maxChoices
    
    global elecTitle
    global elecDescr
    global elecQuestion
    global eleChoices
    global publish
    
    deployment = False
    serverAddr = False
    try:
        jsonFile = open(electionConfig, 'r')
        jsonData = json.load(jsonFile, object_pairs_hook=collections.OrderedDict)
        votingTime = jsonData["electionDurationInHours"]*60*60    #hours to seconds
        mockVoters = jsonData["numberOfMockVoters"]
        numMix = jsonData["numberOfMixServers"]
        createdElections = jsonData["electionsCreated"]
        nginxPort = jsonData["nginx-port"]
        if jsonData["deployment"] is True:
            serverAddr = rootDirProject + "/deployment/serverAddresses.json"
            deployment = True
        jsonFile.close()
    except IOError:<commit_msg>added global variable<commit_after>import os
import json
import collections
import shutil
from shutil import ignore_patterns
import errno
import datetime
import time
import sys
import subprocess
from subprocess import check_output
import random
import string
import re
import jwrite
import electionUtils

'''
The script can be called with 1 to 3 arguments

[1] argument should be a either "mockElection" or "customElection",
    depending on whether it should be a simple test election without many arguments or not.
    To avoid misspelling, a mock election will be created as long as the argument contains the string "mock".

[2] argument (not needed for mock elections) should be a stringified JSON object containing the following elements
    (which are all included in a proper ElectionManifest.json):
        "title": string
        "description": string
        "startTime": string of format: "yy-mm-dd hh:mm UTC+0000", example: "2014-01-01 15:00 UTC+0000"
        "endTime": string of format: "yy-mm-dd hh:mm UTC+0000", example: "2018-11-10 16:00 UTC+0000"
        "voters": array/list of strings  
        "publishListOfVoters": true or false (boolean)
        "minChoicesPerVoter": positive integer
        "maxChoicesPerVoter": positive integer no higher than the amount of choices
        "question": string
        "choices": array/list of strings

[3] argument (optional) should be a stringified JSON object, with optional keys:
        "password": string encrypted with '<PASSWORD>'
        "userChosenRandomness" true or false (boolean)
        "hidden": true or false (boolean)
        "subdomain": a subdomain all the servers will be on, this will replace the ELS,
                therefore two election with the same subdomain should not be created (no check yet).
        "keys": array/list containing 1+<numberOfMixServers> (usually three mix servers) JSON objects
                containing 4 corresponding keypairs: {encryption_key, verification_key, signing_key, decryption_key}
'''

def copy(src, dest):
    try:
        shutil.copytree(src, dest, symlinks=False, ignore=ignore_patterns("*.py", "00", "01", "02", "tests"))
    except OSError as e:
        # source is a file, not a directory
        if e.errno == errno.ENOTDIR:
            shutil.copy(src, dest)
        else:
            print("Directory not copied. Error: %s" % e)

def getTime():
    utcTime = datetime.datetime.utcnow()
    return utcTime

def addSec(tm, secs):
    fulldate = tm + datetime.timedelta(seconds=secs)
    return fulldate

def setConfigFiles():
    global rootDirProject
    
    global sElectDir
    global electionConfig
    global electionInfo
    global electionInfoHidden
    global defaultManifest
    global electionURI
    global nginxConf
    global passList
    global nginxLog
    
    global manifest
    global collectingConf
    global bulletinConf
    global votingManifest
    global votingConf
    global authManifest
    global authConf
    global mixConf

    # the root dir is three folders back
    rootDirProject = os.path.realpath(__file__)
    for i in range(2):
        rootDirProject=os.path.split(rootDirProject)[0]
    
    # absolute paths
    sElectDir = rootDirProject + "/sElect"
    electionConfig = rootDirProject + "/_configFiles_/handlerConfigFile.json"
    electionInfo = rootDirProject + "/_configFiles_/electionInfo.json"
    defaultManifest = rootDirProject + "/_configFiles_/ElectionManifest.json"
    electionURI = rootDirProject + "/_configFiles_/electionsURI.json"
    electionInfoHidden = rootDirProject + "/elections_hidden/electionInfo.json"
    nginxConf =  rootDirProject + "/nginx_config/nginx_select.conf"
    passList =  rootDirProject + "/ElectionHandler/_data_/pwd.json"
    nginxLog = rootDirProject + "/nginx_config/log"
    
    # sElect (partial) mixFiles path
    manifest = "/_sElectConfigFiles_/ElectionManifest.json"
    collectingConf = "/CollectingServer/config.json"
    bulletinConf = "/BulletinBoard/config.json"
    votingManifest = "/VotingBooth/ElectionManifest.json"     
    votingConf = "/VotingBooth/config.json" 
    authManifest = "/Authenticator/ElectionManifest.json"     
    authConf = "/Authenticator/config.json" 
    mixConf = []
    
def getConfigData():
    #get duration and deployment status from handlerConfigFile
    global deployment
    global serverAddr
    global votingTime
    global mockVoters
    global numMix
    global createdElections
    global nginxPort
    global minChoices
    global maxChoices
    
    global elecTitle
    global elecDescr
    global elecQuestion
    global furtherInfo
    global eleChoices
    global publish
    
    deployment = False
    serverAddr = False
    try:
        jsonFile = open(electionConfig, 'r')
        jsonData = json.load(jsonFile, object_pairs_hook=collections.OrderedDict)
        votingTime = jsonData["electionDurationInHours"]*60*60    #hours to seconds
        mockVoters = jsonData["numberOfMockVoters"]
        numMix = jsonData["numberOfMixServers"]
        createdElections = jsonData["electionsCreated"]
        nginxPort = jsonData["nginx-port"]
        if jsonData["deployment"] is True:
            serverAddr = rootDirProject + "/deployment/serverAddresses.json"
            deployment = True
        jsonFile.close()
    except IOError:
{'fraction_non_alphanumeric': 0.07611672490320144, 'fraction_numerical': 0.011143639626027008, 'mean_word_length': 3.254720771394134, 'pattern_counts': {'":': 28, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '315734', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2959, 'n_words': 927}