Skip to content

Commit

Permalink
create function for activate env
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetSaroha committed Jan 20, 2024
1 parent b3326ba commit bf994db
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/makim/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def __init__(self):
self.verbose = False

def _call_shell_app(self, cmd):
if self.global_data.get('virtual-environment'):
self._activate_conda_environment(
self.global_data['virtual-environment']
)

fd, filepath = tempfile.mkstemp(suffix='.makim', text=True)

with open(filepath, 'w') as f:
Expand Down Expand Up @@ -119,6 +124,50 @@ def _call_shell_app(self, cmd):
os._exit(MakimError.SH_KEYBOARD_INTERRUPT.value)
os.close(fd)

def _activate_conda_environment(self, env_data: dict):
app = env_data.get('application', '')
env_name = env_data.get('name', '')

if app not in ['conda', 'mamba']:
self._print_error(
f'[EE] Unsupported virtual environment application: {app}.'
)
os._exit(MakimError.SH_ERROR_RETURN_CODE.value)

sh.Command('xonsh')(
'-c',
f'{app} init xonsh',
_in=sys.stdin,
_out=sys.stdout,
_err=sys.stderr,
_bg=False,
_bg_exc=False,
_no_err=True,
_env=os.environ,
_new_session=False,
)

activate_command = f'{app} activate {env_name}'
try:
sh.Command('xonsh')(
'-c',
activate_command,
_in=sys.stdin,
_out=sys.stdout,
_err=sys.stderr,
_bg=False,
_bg_exc=False,
_no_err=True,
_env=os.environ,
_new_session=False,
)
except sh.ErrorReturnCode as e:
self._print_error(
f'[EE] Failed to activate environment: {env_name}. '
f'Error: {e!s}'
)
os._exit(MakimError.SH_ERROR_RETURN_CODE.value)

def _check_makim_file(self):
return Path(self.file).exists()

Expand Down

0 comments on commit bf994db

Please sign in to comment.