9
9
DEFAULT_MICROPYPATH = "~/.micropython/lib:/usr/lib/micropython"
10
10
11
11
debug = False
12
+ install_path = None
12
13
cleanup_files = []
13
14
14
15
file_buf = bytearray (512 )
@@ -155,7 +156,12 @@ def install_pkg(pkg_spec, install_path):
155
156
f1 .close ()
156
157
return meta
157
158
158
- def install (to_install , install_path ):
159
+ def install (to_install , install_path = None ):
160
+ if install_path is None :
161
+ install_path = get_install_path ()
162
+ if install_path [- 1 ] != "/" :
163
+ install_path += "/"
164
+ print ("Installing to: " + install_path )
159
165
# sets would be perfect here, but don't depend on them
160
166
installed = []
161
167
try :
@@ -177,6 +183,18 @@ def install(to_install, install_path):
177
183
print ("Error: cannot find '%s' package (or server error), packages may be partially installed" \
178
184
% pkg_spec , file = sys .stderr )
179
185
186
+ def get_install_path ():
187
+ global install_path
188
+ if install_path is None :
189
+ if hasattr (os , "getenv" ):
190
+ install_path = os .getenv ("MICROPYPATH" )
191
+ if install_path is None :
192
+ # sys.path[0] is current module's path
193
+ install_path = sys .path [1 ]
194
+ install_path = install_path .split (":" , 1 )[0 ]
195
+ install_path = expandhome (install_path )
196
+ return install_path
197
+
180
198
def cleanup ():
181
199
for fname in cleanup_files :
182
200
try :
@@ -197,6 +215,7 @@ def help():
197
215
198
216
def main ():
199
217
global debug
218
+ global install_path
200
219
install_path = None
201
220
202
221
if len (sys .argv ) < 2 or sys .argv [1 ] == "-h" or sys .argv [1 ] == "--help" :
@@ -230,23 +249,11 @@ def main():
230
249
else :
231
250
fatal ("Unknown/unsupported option: " + opt )
232
251
233
- if install_path is None :
234
- install_path = os .getenv ("MICROPYPATH" ) or DEFAULT_MICROPYPATH
235
-
236
- install_path = install_path .split (":" , 1 )[0 ]
237
-
238
- install_path = expandhome (install_path )
239
-
240
- if install_path [- 1 ] != "/" :
241
- install_path += "/"
242
-
243
- print ("Installing to: " + install_path )
244
-
245
252
to_install .extend (sys .argv [i :])
246
253
if not to_install :
247
254
help ()
248
255
249
- install (to_install , install_path )
256
+ install (to_install )
250
257
251
258
if not debug :
252
259
cleanup ()
0 commit comments