Skip to content

Commit 9d88c77

Browse files
committed
MacOS specific fixes for running commands
1 parent 473b18c commit 9d88c77

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

ghpythonremote/connectors.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,24 @@ def _launch_rhino(self):
338338
assert self.rhino_exe is not "" and self.rhino_exe is not None
339339
assert self.rpyc_server_py is not "" and self.rpyc_server_py is not None
340340
assert self.port is not "" and self.port is not None
341-
rhino_call = [
342-
'"' + self.rhino_exe + '"',
343-
"/nosplash",
344-
"/notemplate",
345-
'/runscript="-_RunPythonScript ""{!s}"" {!s} {!s} -_Exit "'.format(
346-
self.rpyc_server_py, self.port, self.log_level,
347-
),
348-
]
341+
if WINDOWS:
342+
rhino_call = [
343+
'"' + self.rhino_exe + '"',
344+
"/nosplash",
345+
"/notemplate",
346+
'/runscript="-_RunPythonScript ""{!s}"" {!s} {!s} -_Exit "'.format(
347+
self.rpyc_server_py, self.port, self.log_level,
348+
),
349+
]
350+
else:
351+
rhino_call = [
352+
self.rhino_exe,
353+
"-nosplash",
354+
"-notemplate",
355+
'-runscript=-_RunPythonScript "{!s}" {!s} {!s} -_Exit'.format(
356+
self.rpyc_server_py, self.port, self.log_level,
357+
),
358+
]
349359
if self.rhino_file_path:
350360
rhino_call.append(self.rhino_file_path)
351361
if WINDOWS:
@@ -393,7 +403,7 @@ def _get_connection(self):
393403
raise
394404
if i == self.timeout - 1:
395405
raise
396-
elif e is socket.error:
406+
elif e is socket.error or isinstance(e, socket.error):
397407
sleep(1)
398408

399409
def _rebuild_gh_remote(self):

ghpythonremote/helpers.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737
DEFAULT_RHINO_VERSION = 7
3838

3939

40+
# IronPython is being picky about check_output in Mono, because some arguments are not supported. Base functionallity works:
41+
def _mono_check_output(*popenargs, **kwargs):
42+
if "stdout" in kwargs:
43+
raise ValueError("stdout argument not allowed, it will be overridden.")
44+
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
45+
output, unused_err = process.communicate()
46+
retcode = process.poll()
47+
if retcode:
48+
cmd = kwargs.get("args")
49+
if cmd is None:
50+
cmd = popenargs[0]
51+
raise subprocess.CalledProcessError(retcode, cmd, output=output)
52+
return output
53+
54+
4055
def get_python_path(location=None):
4156
if location is None or location == "":
4257
if WINDOWS:
@@ -120,7 +135,7 @@ def get_python_from_windows_path():
120135

121136
def get_python_from_macos_path():
122137
try:
123-
python_exe = subprocess.check_output(["which", "python"]).split("\n")[0].strip()
138+
python_exe = _mono_check_output(["which", "python"]).split("\n")[0].strip()
124139
return python_exe
125140
except (OSError, subprocess.CalledProcessError) as e:
126141
logger.error(
@@ -131,10 +146,34 @@ def get_python_from_macos_path():
131146

132147

133148
def get_python_from_conda_env(env_name):
149+
if MACOS:
150+
# Need to find the conda exec from the .zshrc file
151+
try:
152+
output = _mono_check_output(
153+
"source ~/.zshrc; env", shell=True, executable="/bin/zsh"
154+
)
155+
new_vars_list = [line.partition("=")[::2] for line in output.split("\n")]
156+
new_vars_dict = {name: value for (name, value) in new_vars_list}
157+
conda_exe_path = new_vars_dict.get("CONDA_EXE", None)
158+
except (OSError, subprocess.CalledProcessError):
159+
logger.warning(
160+
"Could not source ~/.zshrc file when looking for $CONDA_EXE. Continuing."
161+
)
162+
if conda_exe_path is None or not (
163+
os.path.isfile(conda_exe_path) and os.access(conda_exe_path, os.X_OK)
164+
):
165+
logger.warning("Could not find $CONDA_EXE in ~/.zshrc. Continuing.")
166+
conda_exe_path = "conda"
167+
134168
try:
135-
envs = json.loads(subprocess.check_output(["conda", "env", "list", "--json"]))[
136-
"envs"
137-
]
169+
if WINDOWS:
170+
envs = json.loads(
171+
subprocess.check_output(["conda", "env", "list", "--json"])
172+
)["envs"]
173+
else:
174+
envs = json.loads(
175+
_mono_check_output([conda_exe_path, "env", "list", "--json"])
176+
)["envs"]
138177
except OSError:
139178
if WINDOWS:
140179
logger.warning(
@@ -144,7 +183,7 @@ def get_python_from_conda_env(env_name):
144183
return get_python_from_windows_path()
145184
else:
146185
logger.warning(
147-
"conda not found in your MacOS $PATH, cannot fetch environment by "
186+
"conda not found in your MacOS $PATH or ~/.zshrc, cannot fetch environment by "
148187
+ "name.\nFalling back to getting python path from MacOS $PATH.\n"
149188
)
150189
return get_python_from_macos_path()
@@ -172,7 +211,7 @@ def get_python_from_conda_env(env_name):
172211
if WINDOWS:
173212
python_exe = os.path.join(env_dir[0], "python.exe")
174213
else:
175-
python_exe = os.path.join(env_dir[0], "python")
214+
python_exe = os.path.join(env_dir[0], "bin", "python")
176215
if os.path.isfile(python_exe) and os.access(python_exe, os.X_OK):
177216
return python_exe
178217
else:
@@ -618,11 +657,11 @@ def get_rhino_windows_path(version, preferred_bitness):
618657
def get_rhino_macos_path(version, preferred_bitness):
619658
if version == 7:
620659
return os.path.join(
621-
["Applications", "Rhino 7.app", "Contents", "MacOS", "Rhinoceros"]
660+
"/Applications", "Rhino 7.app", "Contents", "MacOS", "Rhinoceros"
622661
)
623662
elif version == 6:
624663
return os.path.join(
625-
["Applications", "Rhinoceros.app", "Contents", "MacOS", "Rhinoceros"]
664+
"/Applications", "Rhinoceros.app", "Contents", "MacOS", "Rhinoceros"
626665
)
627666
else:
628667
logger.error("Unknown Rhino version {!s}.".format(version))

0 commit comments

Comments
 (0)