Skip to content

Commit bf63593

Browse files
committed
upip: Factor out deducing default installation path to a function.
Make this deducing friendly to baremetal systems by falling back to sys.path if os.getenv() is not available (sys.path[1] is used as an install path in this case).
1 parent c1bfa36 commit bf63593

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

upip/upip.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
DEFAULT_MICROPYPATH = "~/.micropython/lib:/usr/lib/micropython"
1010

1111
debug = False
12+
install_path = None
1213
cleanup_files = []
1314

1415
file_buf = bytearray(512)
@@ -155,7 +156,12 @@ def install_pkg(pkg_spec, install_path):
155156
f1.close()
156157
return meta
157158

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)
159165
# sets would be perfect here, but don't depend on them
160166
installed = []
161167
try:
@@ -177,6 +183,18 @@ def install(to_install, install_path):
177183
print("Error: cannot find '%s' package (or server error), packages may be partially installed" \
178184
% pkg_spec, file=sys.stderr)
179185

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+
180198
def cleanup():
181199
for fname in cleanup_files:
182200
try:
@@ -197,6 +215,7 @@ def help():
197215

198216
def main():
199217
global debug
218+
global install_path
200219
install_path = None
201220

202221
if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
@@ -230,23 +249,11 @@ def main():
230249
else:
231250
fatal("Unknown/unsupported option: " + opt)
232251

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-
245252
to_install.extend(sys.argv[i:])
246253
if not to_install:
247254
help()
248255

249-
install(to_install, install_path)
256+
install(to_install)
250257

251258
if not debug:
252259
cleanup()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy