1515import sys
1616from tempfile import NamedTemporaryFile
1717from subprocess import check_call
18+ import shlex
19+ import mslex
1820
1921if os .name == "nt" :
2022 IS_WINDOWS = True
2123 HOME = "USERPROFILE"
24+ lex = mslex
2225else :
2326 IS_WINDOWS = False
2427 HOME = "HOME"
28+ lex = shlex
2529
2630if "MESMERIZE_LRU_CACHE" in os .environ .keys ():
2731 MESMERIZE_LRU_CACHE = os .environ ["MESMERIZE_LRU_CACHE" ]
@@ -52,15 +56,6 @@ def fn(self, *args, **kwargs):
5256 return catcher
5357
5458
55- def validate_path (path : Union [str , Path ]):
56- if not regex .match (r"^[A-Za-z0-9@/\\:._-]*$" , str (path )):
57- raise ValueError (
58- "Paths must only contain alphanumeric characters, "
59- "hyphens ( - ), underscores ( _ ) or periods ( . )"
60- )
61- return path
62-
63-
6459def make_runfile (
6560 module_path : str , args_str : Optional [str ] = None , filename : Optional [str ] = None
6661) -> str :
@@ -87,13 +82,13 @@ def make_runfile(
8782
8883 if filename is None :
8984 if IS_WINDOWS :
90- sh_file = os .path .join (os .environ [HOME ], "run.ps1 " )
85+ filename = os .path .join (os .environ [HOME ], "run.bat " )
9186 else :
92- sh_file = os .path .join (os .environ [HOME ], "run.sh" )
87+ filename = os .path .join (os .environ [HOME ], "run.sh" )
9388 else :
9489 if IS_WINDOWS :
95- if not filename .endswith (".ps1 " ):
96- filename = filename + ".ps1 "
90+ if not filename .endswith (".bat " ):
91+ filename = filename + ".bat "
9792
9893 sh_file = filename
9994
@@ -107,13 +102,13 @@ def make_runfile(
107102
108103 if "VIRTUAL_ENV" in os .environ .keys ():
109104 f .write (
110- f'export PATH={ os .environ ["PATH" ]} \n '
111- f'export VIRTUAL_ENV={ os .environ ["VIRTUAL_ENV" ]} \n '
112- f'export LD_LIBRARY_PATH={ os .environ ["LD_LIBRARY_PATH" ]} \n '
105+ f'export PATH={ lex . quote ( os .environ ["PATH" ]) } \n '
106+ f'export VIRTUAL_ENV={ lex . quote ( os .environ ["VIRTUAL_ENV" ]) } \n '
107+ f'export LD_LIBRARY_PATH={ lex . quote ( os .environ ["LD_LIBRARY_PATH" ]) } \n '
113108 )
114109
115110 if "PYTHONPATH" in os .environ .keys ():
116- f .write (f'export PYTHONPATH={ os .environ ["PYTHONPATH" ]} \n ' )
111+ f .write (f'export PYTHONPATH={ lex . quote ( os .environ ["PYTHONPATH" ]) } \n ' )
117112
118113 # for k, v in os.environ.items(): # copy the current environment
119114 # if '\n' in v:
@@ -133,28 +128,29 @@ def make_runfile(
133128 # add command to run the python script in the conda environment
134129 # that was active at the time that this shell script was generated
135130 f .write (
136- f'{ os .environ ["CONDA_EXE" ]} run -p { os .environ ["CONDA_PREFIX" ]} python { module_path } { args_str } '
131+ f'{ lex .quote (os .environ ["CONDA_EXE" ])} run -p { lex .quote (os .environ ["CONDA_PREFIX" ])} '
132+ f"python { lex .quote (module_path )} { args_str } "
137133 )
138134 else :
139- f .write (f"python { module_path } { args_str } " ) # call the script to run
135+ f .write (
136+ f"python { lex .quote (module_path )} { args_str } "
137+ ) # call the script to run
140138
141139 else :
142140 with open (sh_file , "w" ) as f :
143141 for k , v in os .environ .items (): # copy the current environment
144142 if regex .match (r"^.*[()]" , str (k )) or regex .match (r"^.*[()]" , str (v )):
145143 continue
146- with NamedTemporaryFile (suffix = ".ps1" , delete = False ) as tmp :
147- try : # windows powershell is stupid so make sure all the env var names work
148- tmp .write (f'$env:{ k } ="{ v } ";\n ' )
149- tmp .close ()
150- check_call (f"powershell { tmp .name } " )
151- os .unlink (tmp .name )
152- except :
153- continue
154- f .write (
155- f'$env:{ k } ="{ v } ";\n '
156- ) # write only env vars that powershell likes
157- f .write (f"{ sys .executable } { module_path } { args_str } " )
144+ # with NamedTemporaryFile(suffix=".bat", delete=False, mode="w") as tmp:
145+ # try: # windows powershell is stupid so make sure all the env var names work
146+ # tmp.write(f'$env:{k}="{v}";\n')
147+ # tmp.close()
148+ # check_call(f"powershell {tmp.name}")
149+ # os.unlink(tmp.name)
150+ # except:
151+ # continue
152+ f .write (f"SET { k } ={ lex .quote (v )} ;\n " )
153+ f .write (f"{ lex .quote (sys .executable )} { lex .quote (module_path )} { args_str } " )
158154
159155 st = os .stat (sh_file )
160156 os .chmod (sh_file , st .st_mode | S_IEXEC )
0 commit comments