Skip to content

Commit

Permalink
[NB] Add button to launch profile environment prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
cfobel committed Oct 5, 2016
1 parent 89919bc commit c5ca0ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 8 additions & 2 deletions microdrop_launcher/bin/profile_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ..dirs import AppDirs
from ..config import create_config_directory
from ..profile import (ICON_PATH, SAVED_COLUMNS, drop_version_errors,
get_major_version, import_profile,
environment_prompt, get_major_version, import_profile,
installed_major_version, launch_profile,
load_profiles_info, profile_major_version,
verify_or_create_profile_version)
Expand Down Expand Up @@ -229,6 +229,7 @@ def short_path(path_i):

button_launch_i = gtk.Button('Launch')
button_open_i = gtk.Button('Open')
button_prompt_i = gtk.Button('Prompt')
button_remove_i = gtk.Button('Remove')

# Use `functools.partial` to bind parameters (through [currying][1])
Expand All @@ -243,15 +244,20 @@ def short_path(path_i):
on_open_clicked = ft.partial(lambda profile_path, *args:
profile_path.launch(),
ph.path(row_i.path))
on_prompt_clicked = ft.partial(lambda profile_path, *args:
environment_prompt(profile_path),
row_i.path)
on_remove_clicked = ft.partial(lambda row, *args: remove_callback(row),
row_i)

button_launch_i.connect('clicked', on_launch_clicked)
button_open_i.connect('clicked', on_open_clicked)
button_prompt_i.connect('clicked', on_prompt_clicked)
button_remove_i.connect('clicked', on_remove_clicked)
# button_remove_i.connect('clicked', lambda
for button_ij, j in zip((button_launch_i, button_open_i,
button_remove_i), range(j + 1, j + 4)):
button_prompt_i, button_remove_i),
range(j + 1, j + 5)):
table.attach(button_ij, left_attach=j, right_attach=j + 1,
**row_kwargs)

Expand Down
39 changes: 36 additions & 3 deletions microdrop_launcher/profile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pkg_resources
import platform
import re
import subprocess as sp
import sys
Expand All @@ -19,6 +20,7 @@
GUI_AVAILABLE = True
from pygtkhelpers.ui.views.command_textview import get_run_command_dialog

from . import conda_prefix
from .config import create_config_directory

cre_version = re.compile(r'^(?P<major>\d+)\.')
Expand Down Expand Up @@ -249,6 +251,35 @@ def verify_or_create_profile_version(profile_path):
output.write(pkg_resources.get_distribution('microdrop').version)


def environment_prompt(profile_path):
'''
Launch command prompt window for Python environment, with environment
variables set for MicroDrop profile and configuration paths (non-blocking).
.. versionadded:: 0.1.post64
'''
profile_path = ph.path(profile_path)
config_file = profile_path.joinpath('microdrop.ini')

env = os.environ.copy()

# Set environment variables for MicroDrop profile and configuration paths.
env['MICRODROP_PROFILE'] = str(profile_path)
env['MICRODROP_CONFIG'] = str(config_file)

# Launch command prompt
if platform.system() == 'Windows':
if conda_prefix() is not None:
command = (r'start cmd "/K" {prefix}\Scripts\activate.bat {prefix}'
.format(prefix=conda_prefix()))
else:
command = r'start cmd'
sp.call(command, shell=True, cwd=str(profile_path), env=env)
else:
raise RuntimeError('OS not currently supported: {}'
.format(platform.system()))


def launch_profile(profile_path):
profile_path = ph.path(profile_path)
verify_or_create_profile_version(profile_path)
Expand All @@ -262,12 +293,14 @@ def launch_profile(profile_path):
# file.
os.chdir(config_file.parent)
return_code = None
env = os.environ.copy()
env['MICRODROP_PROFILE'] = str(profile_path)
env['MICRODROP_CONFIG'] = str(config_file)
# Return code of `5` indicates program should be restarted.
while return_code is None or return_code == 5:
# Launch MicroDrop and save return code.
return_code = sp.call([sys.executable, '-m',
'microdrop.microdrop', '-c',
config_file])
return_code = sp.call([sys.executable, '-m', 'microdrop.microdrop',
'-c', config_file], env=env)
finally:
# Restore original working directory.
os.chdir(original_directory)
Expand Down

0 comments on commit c5ca0ae

Please sign in to comment.