diff --git a/rules_python/whl.py b/rules_python/whl.py index 3e9d4f88fa..111fe7b067 100644 --- a/rules_python/whl.py +++ b/rules_python/whl.py @@ -18,6 +18,7 @@ import os import pkg_resources import re +import shutil import zipfile @@ -54,6 +55,12 @@ def _dist_info(self): # google_cloud-0.27.0.dist-info return '{}-{}.dist-info'.format(self.distribution(), self.version()) + def _data(self): + # Return the name of the data directory within the .whl file. + # e.g. google_cloud-0.27.0-py2.py3-none-any.whl -> + # google_cloud-0.27.0.data + return '{}-{}.data'.format(self.distribution(), self.version()) + def metadata(self): # Extract the structured data from metadata.json in the WHL's dist-info # directory. @@ -105,11 +112,40 @@ def expand(self, directory): with zipfile.ZipFile(self.path(), 'r') as whl: whl.extractall(directory) + # Find any lib directories, and move them to the top level. + try: + data_contents = os.listdir(self._data()) + except: + data_contents = [] + + # TODO: This is probably wrong. These have different targets, and probably both need to be installed. + if 'purelib' in data_contents: + source = os.path.join(self._data(), 'purelib') + elif 'platlib' in data_contents: + source = os.path.join(self._data(), 'platlib') + else: + source = None + + if source: + for f in os.listdir(source): + shutil.move(os.path.join(source, f), directory) + # _parse_metadata parses METADATA files according to https://www.python.org/dev/peps/pep-0314/ def _parse_metadata(self, content): # TODO: handle fields other than just name name_pattern = re.compile('Name: (.*)') - return { 'name': name_pattern.search(content).group(1) } + name = name_pattern.search(content).group(1) + if name in ["matplotlib"]: + run_requires_pattern = re.compile('Requires-Dist: (.*)') + run_requires = [{ + 'requires': [x for x in run_requires_pattern.findall(content)] + }] + else: + run_requires = [] + return { + 'name': name, + 'run_requires': run_requires + } parser = argparse.ArgumentParser( diff --git a/tools/piptool.par b/tools/piptool.par index 15cb46a098..e535a07ac3 100755 Binary files a/tools/piptool.par and b/tools/piptool.par differ diff --git a/tools/whltool.par b/tools/whltool.par index 72e8409e14..b2c08b4ba8 100755 Binary files a/tools/whltool.par and b/tools/whltool.par differ