<commit_before>import glob
import json
import logging
import os
import requests
import time
import uuid


class User:
    def __init__(self, config):
        self.config = config
        # check for login file
        if os.path.isfile(config['server']['login_file']):
            with open(config['server']['login_file']) as login_fh:
                login_details = json.load(login_fh)
                self.username = login_details.get('username')
                self.password = login_details.get('password')
                self.auth     = (self.username, self.password)
        else:
            self.create_user()

    def request(self, slug):
        url = "%s/%s" % (self.config['server']['server_url'], slug)
        req = requests.get(url, auth=self.auth,
                           proxies=self.config['proxy']['proxy'],
                           verify="centinel/data/gd_bundle-g2-g1.crt")
        req.raise_for_status()

        return req.json()

    @property
    def recommended_version(self):
        return int(self.request("version")["version"])

    @property
    def experiments(self):
        return self.request("experiments")["experiments"]

    @property
    def results(self):
        return self.request("results")

    @property
    def clients(self):
        return self.request("clients")

    def submit_result(self, file_name):
        logging.info("Uploading result file - %s", file_name)

        with open(file_name) as result_file:
            files = {'result': result_file}
            url   = "%s/%s" % (self.config['server']['server_url'], "results")
            req   = requests.post(url, proxies=self.config['proxy']['proxy'],
                                  files=files, auth=self.auth,
                                  timeout=self.config['server']['req_timeout'],
                                  verify="centinel/data/gd_bundle-g2-g1.crt")

        req.raise_for_status()
        os.remove(file_name)

    def download_experiment(self, name):
        logging.info("Downloading experiment - %s", name)

        url = "%s/%s/%s" % (self.config['server']['server_url'],
                            "experiments", name)
        req = requests.get(url, proxies=self.config['proxy']['proxy'],
                           verify="centinel/data/gd_bundle-g2-g1.crt",
                           auth=self.auth)
        req.raise_for_status()

        name = "%s.py" % name
        with open(os.path.join(self.config['dirs']['experiments_dir'], name),
                  "w") as exp_fh:
            exp_fh.write(req.content)

    def register(self, username, password):
        logging.info("Registering new user %s" % (username))

        url     = "%s/%s" % (self.config['server']['server_url'], "register")
        payload = {'username': username, 'password': password,
                   'is_vpn': self.config['user'].get('is_vpn')}
        headers = {'content-type': 'application/json'}
        req     = requests.post(url, data=json.dumps(payload),
                                proxies=self.config['proxy']['proxy'],
                                headers=headers,
                                verify="centinel/data/gd_bundle-g2-g1.crt")

        req.raise_for_status()

    def create_user(self):
        self.username = str(uuid.uuid4())
        self.password = os.urandom(64).encode('base-64')
        self.auth     = (self.username, self.password)

        try:
            self.register(self.username, self.password)
            with open(self.config['server']['login_file'], "w") as login_fh:
                login_details = {'username': self.username,
                                 'password': self.password}
                json.dump(login_details, login_fh)
        except Exception as e:
            logging.error("Unable to register: %s" % str(e))
            raise e


def sync(config):
    logging.info("Starting sync with %s", config['server']['server_url'])
<commit_msg>updated bundle location independent of cur dir<commit_after>import glob
import json
import logging
import os
import os.path
import requests
import time
import uuid

CERT_BUNDLE = os.path.join(os.path.dirname(__file__),
                           "data/gd_bundle-g2-g1.crt")


class User:
    def __init__(self, config):
        self.config = config
        # check for login file
        if os.path.isfile(config['server']['login_file']):
            with open(config['server']['login_file']) as login_fh:
                login_details = json.load(login_fh)
                self.username = login_details.get('username')
                self.password = login_details.get('password')
                self.auth     = (self.username, self.password)
        else:
            self.create_user()

    def request(self, slug):
        url = "%s/%s" % (self.config['server']['server_url'], slug)
        req = requests.get(url, auth=self.auth,
                           proxies=self.config['proxy']['proxy'],
                           verify=CERT_BUNDLE)
        req.raise_for_status()

        return req.json()

    @property
    def recommended_version(self):
        return int(self.request("version")["version"])

    @property
    def experiments(self):
        return self.request("experiments")["experiments"]

    @property
    def results(self):
        return self.request("results")

    @property
    def clients(self):
        return self.request("clients")

    def submit_result(self, file_name):
        logging.info("Uploading result file - %s", file_name)

        with open(file_name) as result_file:
            files = {'result': result_file}
            url   = "%s/%s" % (self.config['server']['server_url'], "results")
            req   = requests.post(url, proxies=self.config['proxy']['proxy'],
                                  files=files, auth=self.auth,
                                  timeout=self.config['server']['req_timeout'],
                                  verify=CERT_BUNDLE)

        req.raise_for_status()
        os.remove(file_name)

    def download_experiment(self, name):
        logging.info("Downloading experiment - %s", name)

        url = "%s/%s/%s" % (self.config['server']['server_url'],
                            "experiments", name)
        req = requests.get(url, proxies=self.config['proxy']['proxy'],
                           verify=CERT_BUNDLE,
                           auth=self.auth)
        req.raise_for_status()

        name = "%s.py" % name
        with open(os.path.join(self.config['dirs']['experiments_dir'], name),
                  "w") as exp_fh:
            exp_fh.write(req.content)

    def register(self, username, password):
        logging.info("Registering new user %s" % (username))

        url     = "%s/%s" % (self.config['server']['server_url'], "register")
        payload = {'username': username, 'password': password,
                   'is_vpn': self.config['user'].get('is_vpn')}
        headers = {'content-type': 'application/json'}
        req     = requests.post(url, data=json.dumps(payload),
                                proxies=self.config['proxy']['proxy'],
                                headers=headers,
                                verify=CERT_BUNDLE)

        req.raise_for_status()

    def create_user(self):
        self.username = str(uuid.uuid4())
        self.password = os.urandom(64).encode('base-64')
        self.auth     = (self.username, self.password)

        try:
            self.register(self.username, self.password)
            with open(self.config['server']['login_file'], "w") as login_fh:
                login_details = {'username': self.username,
                                 'password': self.password}
                json.dump(login_details, login_fh)
        except Exception as e:
            logging.error("Unable to register: %s" % str(e))
            raise e


def sync(config):
    logging.info("Starting sync with %s", config['server']['server_url'])

{'fraction_non_alphanumeric': 0.13661546210022665, 'fraction_numerical': 0.0025182573659027953, 'mean_word_length': 1.9793698424606152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6230909', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2185, 'n_words': 421}