diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-10-19 09:37:46 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-10-19 09:37:46 +0200 |
commit | 5421d3e0ce9b3e49a833d40454b2a37ab8bbe74d (patch) | |
tree | 59c91b72aed3cdc985bad8ca9f6d6336a91e1ae5 /src/hydrilla/mitmproxy_launcher | |
parent | 14eeee3fbc0a839d918149765d2134d05cd14601 (diff) | |
download | haketilo-hydrilla-5421d3e0ce9b3e49a833d40454b2a37ab8bbe74d.tar.gz haketilo-hydrilla-5421d3e0ce9b3e49a833d40454b2a37ab8bbe74d.zip |
[proxy] use XDG_STATE_HOME to determine the directory for Haketilo to store its data in
Diffstat (limited to 'src/hydrilla/mitmproxy_launcher')
-rw-r--r-- | src/hydrilla/mitmproxy_launcher/launch.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/hydrilla/mitmproxy_launcher/launch.py b/src/hydrilla/mitmproxy_launcher/launch.py index 4fe31db..2f706fd 100644 --- a/src/hydrilla/mitmproxy_launcher/launch.py +++ b/src/hydrilla/mitmproxy_launcher/launch.py @@ -32,6 +32,7 @@ import subprocess as sp import typing as t from pathlib import Path +from shutil import copytree # The following import requires at least Python 3.8. There is no point adding # a workaround for Python 3.7 because mitmproxy itself (which we're loading # here) relies on Python 3.9+. This does not affect the Hydrilla server and @@ -44,6 +45,10 @@ from .. import _version from ..translations import smart_gettext as _ +xdg_state_home = os.environ.get('XDG_STATE_HOME', '.local/state') +default_dir = str(Path.home() / xdg_state_home / 'haketilo') +old_default_dir_path = Path.home() / '.haketilo/' + addon_script_text = ''' from hydrilla.proxy.addon import HaketiloAddon @@ -57,9 +62,9 @@ addons = [HaketiloAddon()] help=_('cli_opt.haketilo.port')) @click.option('-L/-l', '--launch-browser/--no-launch-browser', default=True, help=_('cli_opt.haketilo.launch_browser')) -@click.option('-d', '--directory', default='~/.haketilo/', +@click.option('-d', '--directory', default=default_dir, type=click.Path(file_okay=False), - help=_('cli_opt.haketilo.dir')) + help=_('cli_opt.haketilo.dir_defaults_to_{}').format(default_dir)) @click.version_option(version=_version.version, prog_name='Haketilo proxy', message=_('%(prog)s_%(version)s_license'), help=_('cli_opt.haketilo.version')) @@ -67,6 +72,15 @@ def launch(listen_host: str, port: int, launch_browser: bool, directory: str) \ -> t.NoReturn: directory_path = Path(os.path.expanduser(directory)).resolve() + # Before we started using XDG_STATE_HOME, we were storing files by default + # under ~/.haketilo. Let's make sync state from there to our new default + # state directory + if directory == default_dir and \ + old_default_dir_path.exists() and \ + not directory_path.exists(): + directory_path.parent.mkdir(parents=True, exist_ok=True) + copytree(old_default_dir_path, directory_path, symlinks=True) + directory_path.mkdir(parents=True, exist_ok=True) script_path = directory_path / 'addon.py' |