Skip to content

Commit

Permalink
Switch from using piptool.par to piptool_wrapper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Greiman committed Mar 23, 2018
1 parent 372d73c commit 045f37c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 54 deletions.
2 changes: 1 addition & 1 deletion python/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pip_import = repository_rule(
),
"_script": attr.label(
executable = True,
default = Label("//tools:piptool.par"),
default = Label("//tools:piptool_wrapper.py"),
cfg = "host",
),
},
Expand Down
60 changes: 10 additions & 50 deletions rules_python/piptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,76 +25,36 @@
import tempfile
import zipfile

# Note: We carefully import the following modules in a particular
# order, since these modules modify the import path and machinery.
import pkg_resources


def extract_packages(package_names):
"""Extract zipfile contents to disk and add to import path"""

# Set a safe extraction dir
extraction_tmpdir = tempfile.mkdtemp()
atexit.register(lambda: shutil.rmtree(
extraction_tmpdir, ignore_errors=True))
pkg_resources.set_extraction_path(extraction_tmpdir)

# Extract each package to disk
dirs_to_add = []
for package_name in package_names:
req = pkg_resources.Requirement.parse(package_name)
extraction_dir = pkg_resources.resource_filename(req, '')
dirs_to_add.append(extraction_dir)

# Add extracted directories to import path ahead of their zip file
# counterparts.
sys.path[0:0] = dirs_to_add
existing_pythonpath = os.environ.get('PYTHONPATH')
if existing_pythonpath:
dirs_to_add.extend(existing_pythonpath.split(':'))
os.environ['PYTHONPATH'] = ':'.join(dirs_to_add)


# Wheel, pip, and setuptools are much happier running from actual
# files on disk, rather than entries in a zipfile. Extract zipfile
# contents, add those contents to the path, then import them.
extract_packages(['pip', 'setuptools', 'wheel'])

# Defeat pip's attempt to mangle sys.path
saved_sys_path = sys.path
sys.path = sys.path[:]
import pip
sys.path = saved_sys_path

import setuptools
import wheel


def pip_main(argv):
# Extract the certificates from the PAR following the example of get-pip.py
# https://github.com/pypa/get-pip/blob/430ba37776ae2ad89/template.py#L164-L168
cert_path = os.path.join(tempfile.mkdtemp(), "cacert.pem")
with open(cert_path, "wb") as cert:
cert.write(pkgutil.get_data("pip._vendor.requests", "cacert.pem"))
argv = ["--disable-pip-version-check", "--cert", cert_path] + argv
argv = ["--disable-pip-version-check"] + argv
return pip.main(argv)

from rules_python.whl import Wheel
from whl import Wheel

parser = argparse.ArgumentParser(
description='Import Python dependencies into Bazel.')

parser.add_argument('--name', action='store',
help=('The namespace of the import.'))
help=('The namespace of the import.'),
required=True)

parser.add_argument('--input', action='store',
help=('The requirements.txt file to import.'))
help=('The requirements.txt file to import.'),
required=True)

parser.add_argument('--output', action='store',
help=('The requirements.bzl file to export.'))
help=('The requirements.bzl file to export.'),
required=True)

parser.add_argument('--directory', action='store',
help=('The directory into which to put .whl files.'))
help=('The directory into which to put .whl files.'),
required=True)

def determine_possible_extras(whls):
"""Determines the list of possible "extras" for each .whl
Expand Down
11 changes: 8 additions & 3 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0

# This is generated and updated by ./update_tools.sh
exports_files(["piptool.par", "whltool.par"])
exports_files([
"piptool.par",
"piptool_wrapper.py",
"whltool.par" +
"whltool_wrapper.py",
])

py_test(
name = "par_test",
srcs = ["par_test.py"],
data = [
"//rules_python:piptool.par",
"//rules_python:whltool.par",
":piptool.par",
":whltool.par",
"//rules_python:piptool.par",
"//rules_python:whltool.par",
],
)
Binary file modified tools/piptool.par
Binary file not shown.
Binary file modified tools/whltool.par
Binary file not shown.

0 comments on commit 045f37c

Please sign in to comment.