New config/data path management (WIP)

This commit is contained in:
caryoscelus 2024-04-04 12:37:47 +00:00
parent 33436f7eb6
commit bbe577310c
No known key found for this signature in database
GPG key ID: 254EDDB85B66CB1F
2 changed files with 15 additions and 16 deletions

View file

@ -76,7 +76,7 @@ class Config:
if '--portable' in self.argv or self.build_type == 'portable': if '--portable' in self.argv or self.build_type == 'portable':
return '.' 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: if os.path.isdir(f'{here}/data') and not '--no-portable' in self.argv:
print('WARNING: found data in current directory') print('WARNING: found data in current directory')
print(' It used to be default behaviour to store data alongside project 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('--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('--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('--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") 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('--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('--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('--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', 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-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") self.parser.add_argument('--trackers-proxy', help='Force use proxy to connect to trackers (disable, tor, ip:port)', default="disable")

View file

@ -4,6 +4,7 @@ import stat
import time import time
import logging import logging
from util.compat import * from util.compat import *
from pathlib import Path
startup_errors = [] startup_errors = []
def startupError(msg): def startupError(msg):
@ -51,31 +52,28 @@ def importBundle(bundle):
map(lambda f: removeprefix(f, prefix).split('/')[0], all_files)))) map(lambda f: removeprefix(f, prefix).split('/')[0], all_files))))
for d in top_2: for d in top_2:
if isValidAddress(d): 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): for fname in filter(lambda f: f.startswith(prefix+d) and not f.endswith('/'), all_files):
tgt = config.data_dir + '/' + removeprefix(fname, prefix) tgt = removeprefix(fname, prefix)
logging.info(f'-- {fname} --> {tgt}') print(f'-- {fname} --> {tgt}')
info = zf.getinfo(fname) info = zf.getinfo(fname)
info.filename = tgt info.filename = tgt
zf.extract(info) zf.extract(info, path=config.data_dir)
logging.info(f'add site {d}') logging.info(f'add site {d}')
sites[d] = {} sites[d] = {}
else: 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: with open(sites_json_path, 'w') as f:
json.dump(sites, f) json.dump(sites, f)
def init_dirs(): def init_dirs():
data_dir = config.data_dir data_dir = Path(config.data_dir)
has_data_dir = os.path.isdir(data_dir) need_bootstrap = (config.bootstrap
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 and not config.offline
and (not data_dir.is_dir() or not (data_dir / 'sites.json').is_file()))
if not has_data_dir: if not data_dir.is_dir():
os.mkdir(data_dir) data_dir.mkdir(parents=True, exist_ok=True)
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 need_bootstrap: if need_bootstrap:
import requests import requests