This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathfetch.py
More file actions
60 lines (54 loc) · 2.55 KB
/
Copy pathfetch.py
File metadata and controls
60 lines (54 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import tempfile
import subprocess
import os
import shutil
import logging
import pip
import pip.status_codes
log = logging.getLogger(__name__)
def fetch_dependency(module_name: str, specifier: str, install_path: str):
"""Shells out to PIP in order to download and unzip the named package into
the specified path. This method only runs `pip download`, NOT `pip
install`, so it's presumably safe.
:param module_name: the name of the package to download
:param specifier: the version specifier for the package
:param install_path: the path in which to install the downloaded package
"""
with tempfile.TemporaryDirectory() as download_folder:
log.info("Attempting to download package %s to %s",
module_name, download_folder, exc_info=True)
# TODO: check the result status
index_url = os.environ.get('INDEX_URL')
if index_url is not None:
result = pip.main(["download", "--no-deps", "-i", index_url,
"-d", download_folder, module_name + specifier])
else:
result = pip.main(["download", "--no-deps", "-d",
download_folder, module_name + specifier])
if result != pip.status_codes.SUCCESS:
log.error("Unable to fetch package %s", module_name)
return
for thing in os.listdir(download_folder):
thing_abs = os.path.join(download_folder, thing)
if os.path.isdir(thing_abs):
log.debug("Moving %s to %s", thing,
install_path, exc_info=True)
shutil.move(thing_abs, install_path)
elif thing.endswith(".whl") or thing.endswith(".zip"):
log.debug("Unzipping %s to %s", thing,
install_path, exc_info=True)
result = subprocess.run(
["unzip", "-o", "-d", install_path, thing_abs])
elif thing.endswith(".tar.gz"):
log.debug("Untarring %s to %s", thing,
install_path, exc_info=True)
result = subprocess.run(
["tar", "-C", install_path, "-xzf", thing_abs])
elif thing.endswith(".tar.bz2"):
log.debug("Untarring %s to %s", thing,
install_path, exc_info=True)
result = subprocess.run(
["tar", "-C", install_path, "-xjf", thing_abs])
else:
log.warning("Unrecognized package file: %s",
thing, exc_info=True)