From bbe577310c971c06d360970fd9beef67d9c60332 Mon Sep 17 00:00:00 2001 From: caryoscelus Date: Thu, 4 Apr 2024 12:37:47 +0000 Subject: [PATCH] New config/data path management (WIP) --- src/Config.py | 5 +++-- src/main.py | 26 ++++++++++++-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Config.py b/src/Config.py index bf482d93..7c13d4d7 100644 --- a/src/Config.py +++ b/src/Config.py @@ -76,7 +76,7 @@ class Config: if '--portable' in self.argv or self.build_type == 'portable': return '.' - here = os.path.dirname(os.path.abspath(__file__).replace("\\", "/")) + here = os.path.dirname(os.path.abspath(__file__).replace("\\", "/")).rstrip('/src') if os.path.isdir(f'{here}/data') and not '--no-portable' in self.argv: print('WARNING: found data in current directory') print(' It used to be default behaviour to store data alongside project directory,') @@ -253,6 +253,7 @@ class Config: self.parser.add_argument('--batch', help="Batch mode (No interactive input for commands)", action='store_true') + self.parser.add_argument('--portable', action=argparse.BooleanOptionalAction) self.parser.add_argument('--start-dir', help='Path of working dir for variable content (data, log, .conf)', default=self.start_dir, metavar="path") self.parser.add_argument('--config-file', help='Path of config file', default=config_file, metavar="path") self.parser.add_argument('--data-dir', help='Path of data directory', default=data_dir, metavar="path") @@ -300,7 +301,7 @@ class Config: self.parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port') self.parser.add_argument('--bind', help='Bind outgoing sockets to this address', metavar='ip') self.parser.add_argument('--bootstrap-url', help='URL of file with link to bootstrap bundle', default='https://raw.githubusercontent.com/zeronet-conservancy/zeronet-conservancy/master/bootstrap.url', type=str) - self.parser.add_argument('--disable-bootstrap', help='Disable downloading bootstrap information from clearnet', action='store_true') + self.parser.add_argument('--bootstrap', help='Enable downloading bootstrap information from clearnet', action=argparse.BooleanOptionalAction, default=True) self.parser.add_argument('--trackers', help='Bootstraping torrent trackers', default=[], metavar='protocol://address', nargs='*') self.parser.add_argument('--trackers-file', help='Load torrent trackers dynamically from a file (using Syncronite by default)', default=['{data_dir}/15CEFKBRHFfAP9rmL6hhLmHoXrrgmw4B5o/cache/1/Syncronite.html'], metavar='path', nargs='*') self.parser.add_argument('--trackers-proxy', help='Force use proxy to connect to trackers (disable, tor, ip:port)', default="disable") diff --git a/src/main.py b/src/main.py index 4176459b..b899fbcc 100644 --- a/src/main.py +++ b/src/main.py @@ -4,6 +4,7 @@ import stat import time import logging from util.compat import * +from pathlib import Path startup_errors = [] def startupError(msg): @@ -51,31 +52,28 @@ def importBundle(bundle): map(lambda f: removeprefix(f, prefix).split('/')[0], all_files)))) for d in top_2: if isValidAddress(d): - logging.info(f'unpack {d} into {config.data_dir}') + print(f'Unpacking {d} into {config.data_dir}') for fname in filter(lambda f: f.startswith(prefix+d) and not f.endswith('/'), all_files): - tgt = config.data_dir + '/' + removeprefix(fname, prefix) - logging.info(f'-- {fname} --> {tgt}') + tgt = removeprefix(fname, prefix) + print(f'-- {fname} --> {tgt}') info = zf.getinfo(fname) info.filename = tgt - zf.extract(info) + zf.extract(info, path=config.data_dir) logging.info(f'add site {d}') sites[d] = {} else: - logging.info(f'Warning: unknown file in a bundle: {prefix+d}') + print(f'Warning: unknown file in a bundle: {prefix+d}') with open(sites_json_path, 'w') as f: json.dump(sites, f) def init_dirs(): - data_dir = config.data_dir - has_data_dir = os.path.isdir(data_dir) - need_bootstrap = not config.disable_bootstrap and (not has_data_dir or not os.path.isfile(f'{data_dir}/sites.json')) and not config.offline + data_dir = Path(config.data_dir) + need_bootstrap = (config.bootstrap + and not config.offline + and (not data_dir.is_dir() or not (data_dir / 'sites.json').is_file())) - if not has_data_dir: - os.mkdir(data_dir) - try: - os.chmod(data_dir, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) - except Exception as err: - startupError(f"Can't change permission of {data_dir}: {err}") + if not data_dir.is_dir(): + data_dir.mkdir(parents=True, exist_ok=True) if need_bootstrap: import requests