This function downloads the given package / dependency and extracts the raw contents into a folder. Afterwards, it returns a tuple with the type of distribution obtained, and the temporary folder it extracted to. It is the caller's responsibility to delete the retur
(dependency)
| 249 | |
| 250 | |
| 251 | def get_package_as_folder(dependency): |
| 252 | """ This function downloads the given package / dependency and extracts |
| 253 | the raw contents into a folder. |
| 254 | |
| 255 | Afterwards, it returns a tuple with the type of distribution obtained, |
| 256 | and the temporary folder it extracted to. It is the caller's |
| 257 | responsibility to delete the returned temp folder after use. |
| 258 | |
| 259 | Examples of returned values: |
| 260 | |
| 261 | ("source", "/tmp/pythonpackage-venv-e84toiwjw") |
| 262 | ("wheel", "/tmp/pythonpackage-venv-85u78uj") |
| 263 | |
| 264 | What the distribution type will be depends on what pip decides to |
| 265 | download. |
| 266 | """ |
| 267 | |
| 268 | venv_parent = tempfile.mkdtemp( |
| 269 | prefix="pythonpackage-venv-" |
| 270 | ) |
| 271 | try: |
| 272 | # Create a venv to install into: |
| 273 | try: |
| 274 | if int(sys.version.partition(".")[0]) < 3: |
| 275 | # Python 2.x has no venv. |
| 276 | subprocess.check_output([ |
| 277 | sys.executable, # no venv conflict possible, |
| 278 | # -> no need to use system python |
| 279 | "-m", "virtualenv", |
| 280 | "--python=" + _get_system_python_executable(), |
| 281 | os.path.join(venv_parent, 'venv') |
| 282 | ], cwd=venv_parent) |
| 283 | else: |
| 284 | # On modern Python 3, use venv. |
| 285 | subprocess.check_output([ |
| 286 | _get_system_python_executable(), "-m", "venv", |
| 287 | os.path.join(venv_parent, 'venv') |
| 288 | ], cwd=venv_parent) |
| 289 | except subprocess.CalledProcessError as e: |
| 290 | output = e.output.decode('utf-8', 'replace') |
| 291 | raise ValueError( |
| 292 | 'venv creation unexpectedly ' + |
| 293 | 'failed. error output: ' + str(output) |
| 294 | ) |
| 295 | venv_path = os.path.join(venv_parent, "venv") |
| 296 | |
| 297 | # Update pip and wheel in venv for latest feature support: |
| 298 | try: |
| 299 | filenotfounderror = FileNotFoundError |
| 300 | except NameError: # Python 2. |
| 301 | filenotfounderror = OSError |
| 302 | try: |
| 303 | subprocess.check_output([ |
| 304 | os.path.join(venv_path, "bin", "pip"), |
| 305 | "install", "-U", "pip", "wheel", |
| 306 | ]) |
| 307 | except filenotfounderror: |
| 308 | raise RuntimeError( |