Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow using either --file or --sync-dir + allow use of relative path in args #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__
peru.egg-info
build/
dist/
.venv/
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,11 @@ control where peru puts things. Flags always take precedence.

- `--file=<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=<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=<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
Expand Down
33 changes: 24 additions & 9 deletions peru/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down