diff --git a/.gitignore b/.gitignore index fe7dd1f..dfda9da 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 e5ba9d5..2a81bb5 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 431a183..d53f3b4 100644 --- a/peru/runtime.py +++ b/peru/runtime.py @@ -61,26 +61,41 @@ 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.') 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) 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')) + + if args['--state-dir']: + self.state_dir = os.path.abspath(args['--state-dir']) + else: + self.state_dir = os.path.join(self.sync_dir, '.peru') + + 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 c236d2d..95b1eaa 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([