From 1e407695b20b8705f160624945764c7f8feb533b Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Fri, 17 Feb 2023 19:13:33 +0200 Subject: [PATCH 1/3] allow using either `--file` or `--sync-dir` --- .gitignore | 1 + README.md | 6 ++---- peru/runtime.py | 11 ++++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index fe7dd1f7..dfda9dab 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__ peru.egg-info build/ dist/ +.venv/ diff --git a/README.md b/README.md index e5ba9d5d..2a81bb50 100644 --- a/README.md +++ b/README.md @@ -332,13 +332,11 @@ control where peru puts things. Flags always take precedence. - `--file=`: The path to your peru YAML file. By default peru looks for `peru.yaml` in the current directory or one of its parents. - This setting tells peru to use a specific file. If set, `--sync-dir` - must also be set. + This setting tells peru to use a specific file. - `--sync-dir=`: The path that all `imports` are interpreted relative to. That is, if you import a module to `./`, the contents of that module go directly in the sync dir. By default this is the - directory containing your `peru.yaml` file. If set, `--file` must also - be set. + directory containing your `peru.yaml` file. - `--state-dir=`: The directory where peru stashes all of its state metadata, and also the parent of the cache dir. By default this is `.peru` inside the sync dir. You should not share this directory diff --git a/peru/runtime.py b/peru/runtime.py index 431a183b..78b91d1b 100644 --- a/peru/runtime.py +++ b/peru/runtime.py @@ -70,9 +70,14 @@ def _set_paths(self, args, env): if explicit_peru_file and explicit_sync_dir: self.peru_file = explicit_peru_file self.sync_dir = explicit_sync_dir - elif explicit_peru_file or explicit_sync_dir: - raise CommandLineError('If the --file or --sync-dir is set, ' - 'the other must also be set.') + elif explicit_peru_file and not explicit_sync_dir: + self.peru_file = explicit_peru_file + self.sync_dir = os.path.dirname(self.peru_file) + elif not explicit_peru_file and explicit_sync_dir: + basename = explicit_basename or parser.DEFAULT_PERU_FILE_NAME + self.peru_file = find_project_file(os.getcwd(), basename) + self.sync_dir = explicit_sync_dir + else: basename = explicit_basename or parser.DEFAULT_PERU_FILE_NAME self.peru_file = find_project_file(os.getcwd(), basename) From 50d55404ef7977aa408f8a9499d90734c1ec96a3 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Fri, 17 Feb 2023 20:42:24 +0200 Subject: [PATCH 2/3] allow using relative path in args affects the following args: [ file, sync-dir,, state-dir, cache-dir ] --- peru/runtime.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/peru/runtime.py b/peru/runtime.py index 78b91d1b..d8e24107 100644 --- a/peru/runtime.py +++ b/peru/runtime.py @@ -61,9 +61,12 @@ async def _init_cache(self): self.cache = await cache.Cache(self.cache_dir) def _set_paths(self, args, env): - explicit_peru_file = args['--file'] - explicit_sync_dir = args['--sync-dir'] explicit_basename = args['--file-basename'] + explicit_peru_file = os.path.abspath( + args['--file']) if args['--file'] else None + explicit_sync_dir = os.path.abspath( + args['--sync-dir']) if args['--sync-dir'] else None + if explicit_peru_file and explicit_basename: raise CommandLineError( 'Cannot use both --file and --file-basename at the same time.') @@ -77,15 +80,16 @@ def _set_paths(self, args, env): basename = explicit_basename or parser.DEFAULT_PERU_FILE_NAME self.peru_file = find_project_file(os.getcwd(), basename) self.sync_dir = explicit_sync_dir - else: basename = explicit_basename or parser.DEFAULT_PERU_FILE_NAME self.peru_file = find_project_file(os.getcwd(), basename) self.sync_dir = os.path.dirname(self.peru_file) - self.state_dir = (args['--state-dir'] - or os.path.join(self.sync_dir, '.peru')) - self.cache_dir = (args['--cache-dir'] or env.get('PERU_CACHE_DIR') - or os.path.join(self.state_dir, 'cache')) + + self.state_dir = os.path.abspath( + args['--state-dir']) if args['--state-dir'] else os.path.join(self.sync_dir, '.peru') + + self.cache_dir = os.path.abspath( + args['--cache-dir']) if args['--cache-dir'] else os.path.join(self.state_dir, 'cache') def tmp_dir(self): dir = tempfile.TemporaryDirectory(dir=self._tmp_root) From 7e27bd3dda381905584cb8793225c24a96051b0e Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Thu, 23 Feb 2023 18:37:56 +0200 Subject: [PATCH 3/3] fix failing tests * remove `test_peru_file_and_sync_dir_must_be_set_together` since that rule is removed * fix the behavior and formatting of `--cache-dir` and `--state-dir` params --- peru/runtime.py | 14 ++++++++++---- tests/test_paths.py | 5 ----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/peru/runtime.py b/peru/runtime.py index d8e24107..d53f3b4d 100644 --- a/peru/runtime.py +++ b/peru/runtime.py @@ -85,11 +85,17 @@ def _set_paths(self, args, env): self.peru_file = find_project_file(os.getcwd(), basename) self.sync_dir = os.path.dirname(self.peru_file) - self.state_dir = os.path.abspath( - args['--state-dir']) if args['--state-dir'] else os.path.join(self.sync_dir, '.peru') + if args['--state-dir']: + self.state_dir = os.path.abspath(args['--state-dir']) + else: + self.state_dir = os.path.join(self.sync_dir, '.peru') - self.cache_dir = os.path.abspath( - args['--cache-dir']) if args['--cache-dir'] else os.path.join(self.state_dir, 'cache') + if args['--cache-dir']: + self.cache_dir = os.path.abspath(args['--cache-dir']) + elif env.get('PERU_CACHE_DIR'): + self.cache_dir = env.get('PERU_CACHE_DIR') + else: + self.cache_dir = os.path.join(self.state_dir, 'cache') def tmp_dir(self): dir = tempfile.TemporaryDirectory(dir=self._tmp_root) diff --git a/tests/test_paths.py b/tests/test_paths.py index c236d2d4..95b1eaaa 100644 --- a/tests/test_paths.py +++ b/tests/test_paths.py @@ -46,11 +46,6 @@ def test_unmodified_sync(self): shared.run_peru_command(['sync'], self.cwd) self.assert_success(self.project_dir, self.state_dir, self.cache_dir) - def test_peru_file_and_sync_dir_must_be_set_together(self): - for command in [['--sync-dir=junk', 'sync'], ['--file=junk', 'sync']]: - with self.assertRaises(CommandLineError): - shared.run_peru_command(command, cwd=self.cwd) - def test_file_and_file_basename_incompatible(self): with self.assertRaises(CommandLineError): shared.run_peru_command([