Esempio n. 1
0
def update_user_configs():
    for file_name in ['generic']:
        with (get_homedir() / 'config' / f'{file_name}.json').open() as f:
            try:
                generic_config = json.load(f)
            except Exception:
                generic_config = {}
        with (get_homedir() / 'config' /
              f'{file_name}.json.sample').open() as f:
            generic_config_sample = json.load(f)

        has_new_entry = False
        for key in generic_config_sample.keys():
            if key == '_notes':
                continue
            if generic_config.get(key) is None:
                print(f'{key} was missing in {file_name}, adding it.')
                print(f"Description: {generic_config_sample['_notes'][key]}")
                generic_config[key] = generic_config_sample[key]
                has_new_entry = True
            elif isinstance(generic_config[key], dict):
                for sub_key in generic_config_sample[key].keys():
                    if sub_key not in generic_config[key]:
                        print(
                            f'{sub_key} was missing in {key} from {file_name}, adding it.'
                        )
                        generic_config[key][sub_key] = generic_config_sample[
                            key][sub_key]
                        has_new_entry = True
        if has_new_entry:
            with (get_homedir() / 'config' /
                  f'{file_name}.json').open('w') as fw:
                json.dump(generic_config, fw, indent=2, sort_keys=True)
    return has_new_entry
Esempio n. 2
0
def validate_generic_config_file():
    sample_config = get_homedir() / 'config' / 'generic.json.sample'
    with sample_config.open() as f:
        generic_config_sample = json.load(f)
    # Check documentation
    for key in generic_config_sample.keys():
        if key == '_notes':
            continue
        if key not in generic_config_sample['_notes']:
            raise Exception(f'###### - Documentation missing for {key}')

    user_config = get_homedir() / 'config' / 'generic.json'
    if not user_config.exists():
        # The config file was never created, copy the sample.
        with user_config.open('w') as _fw:
            json.dump(generic_config_sample, _fw)

    with user_config.open() as f:
        generic_config = json.load(f)

    # Check all entries in the sample files are in the user file, and they have the same type
    for key in generic_config_sample.keys():
        if key == '_notes':
            continue
        if generic_config.get(key) is None:
            logger.warning(
                f'Entry missing in user config file: {key}. Will default to: {generic_config_sample[key]}'
            )
            continue
        if not isinstance(generic_config[key], type(
                generic_config_sample[key])):
            raise Exception(
                f'Invalid type for {key}. Got: {type(generic_config[key])} ({generic_config[key]}), expected: {type(generic_config_sample[key])} ({generic_config_sample[key]})'
            )

        if isinstance(generic_config[key], dict):
            # Check entries
            for sub_key in generic_config_sample[key].keys():
                if sub_key not in generic_config[key]:
                    raise Exception(
                        f'{sub_key} is missing in generic_config[key]. Default from sample file: {generic_config_sample[key][sub_key]}'
                    )
                if not isinstance(generic_config[key][sub_key],
                                  type(generic_config_sample[key][sub_key])):
                    raise Exception(
                        f'Invalid type for {sub_key} in {key}. Got: {type(generic_config[key][sub_key])} ({generic_config[key][sub_key]}), expected: {type(generic_config_sample[key][sub_key])} ({generic_config_sample[key][sub_key]})'
                    )

    # Make sure the user config file doesn't have entries missing in the sample config
    for key in generic_config.keys():
        if key not in generic_config_sample:
            raise Exception(
                f'{key} is missing in the sample config file. You need to compare {user_config} with {sample_config}.'
            )

    return True
Esempio n. 3
0
def main():
    # Just fail if the env isn't set.
    get_homedir()
    print('Start backend (redis)...')
    p = run(['run_backend', '--start'])
    p.check_returncode()
    print('done.')
    Popen(['build_hashes'])
    print('Start website...')
    Popen(['start_website'])
    print('done.')
Esempio n. 4
0
def main():
    get_homedir()
    p = Popen(['shutdown'])
    p.wait()
    try:
        r = Redis(unix_socket_path=get_socket_path('lookup'), db=1)
        r.delete('shutdown')
        print('Shutting down databases...')
        p_backend = run(['run_backend', '--stop'])
        p_backend.check_returncode()
        print('done.')
    except ConnectionError:
        # Already down, skip the stacktrace
        pass
Esempio n. 5
0
 def _launch_website(self):
     website_dir = get_homedir() / 'website'
     ip = get_config('generic', 'website_listen_ip')
     port = get_config('generic', 'website_listen_port')
     return Popen([
         'gunicorn', '-w', '10', '--graceful-timeout', '2', '--timeout',
         '300', '-b', f'{ip}:{port}', '--log-level', 'info', 'web:app'
     ],
                  cwd=website_dir)
Esempio n. 6
0
def run_command(command,
                expect_fail: bool = False,
                capture_output: bool = True):
    args = shlex.split(command)
    homedir = get_homedir()
    process = subprocess.run(args, cwd=homedir, capture_output=capture_output)
    if capture_output:
        print(process.stdout.decode())
    if process.returncode and not expect_fail:
        print(process.stderr.decode())
        sys.exit()
Esempio n. 7
0
def check_poetry_version():
    args = shlex.split("poetry self -V")
    homedir = get_homedir()
    process = subprocess.run(args, cwd=homedir, capture_output=True)
    poetry_version_str = process.stdout.decode()
    version = poetry_version_str.split()[2]
    version_details = tuple(int(i) for i in version.split('.'))
    if version_details < (1, 1, 0):
        print('The project requires poetry >= 1.1.0, please update.')
        print(
            'If you installed with "pip install --user poetry", run "pip install --user -U poetry"'
        )
        print(
            'If you installed via the recommended method, use "poetry self update"'
        )
        print(
            'More details: https://github.com/python-poetry/poetry#updating-poetry'
        )
        sys.exit()
Esempio n. 8
0
def shutdown_lookup(storage_directory: Optional[Path] = None):
    if not storage_directory:
        storage_directory = get_homedir()
    r = Redis(unix_socket_path=get_socket_path('lookup'))
    r.shutdown(save=True)
    print('Redis lookup database shutdown.')
Esempio n. 9
0
def launch_lookup(storage_directory: Optional[Path] = None):
    if not storage_directory:
        storage_directory = get_homedir()
    if not check_running('lookup'):
        Popen(["./run_redis.sh"], cwd=(storage_directory / 'lookup'))
Esempio n. 10
0
def compute_hash_self():
    m = hashlib.sha256()
    with (get_homedir() / 'bin' / 'update.py').open('rb') as f:
        m.update(f.read())
        return m.digest()