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

we need to cd to the final destination even if source fails #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
48 changes: 33 additions & 15 deletions aactivator.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,38 @@ def security_check(path):
)


def commands(commands, always=None):
"""
run a set of commands, stopping on the first error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this docstring seems false, this function does not "run a set of commands, stopping on the first error"

The `always` is run whether there was an error or not
"""
cmd = ' &&\n'.join(commands)
if always:
cmd += '\n' + always
return cmd


def command_for_path(cmd, path, pwd):
if path == pwd:
return cmd
else:
return ' &&\n'.join((
'OLDPWD_bak="$OLDPWD"',
'cd ' + quote(path),
cmd,
'cd "$OLDPWD_bak"',
'cd ' + quote(pwd),
'unset OLDPWD_bak',
))
return commands(
(
'OLDPWD_bak="$OLDPWD"',
'cd ' + quote(path),
cmd,
),
always=commands((
'cd "$OLDPWD_bak"',
'cd ' + quote(pwd),
'unset OLDPWD_bak',
))
)


def aactivate(path, pwd):
return command_for_path(
' &&\n'.join((
commands((
'aactivator security-check ' + ACTIVATE,
'source ./' + ACTIVATE,
'export %s=%s' % (ENVIRONMENT_VARIABLE, quote(path)),
Expand All @@ -278,15 +293,18 @@ def deaactivate(path, pwd):

if os.path.exists(deactivate_path):
return command_for_path(
' &&\n'.join((
'aactivator security-check ' + DEACTIVATE,
'source ./' + DEACTIVATE,
)) + '\n' + unset,
commands(
(
'aactivator security-check ' + DEACTIVATE,
'source ./' + DEACTIVATE,
),
always=unset,
),
path,
pwd,
)
else:
return ' &&\n'.join((
return commands((
unset,
error_command('Cannot deactivate. File missing: {0}'.format(deactivate_path))
))
Expand Down Expand Up @@ -315,7 +333,7 @@ def get_output(environ, pwd='.', get_input=sys.stdin.readline, arg0='/path/to/aa
result.append(deaactivate(activated_env, pwd))
if activate_path:
result.append(aactivate(activate_path, pwd))
return ' &&\n'.join(result)
return commands(result)


def aactivator(args, env):
Expand Down