Example #1
0
def unpack(wheel_path, dest_directory):
    # TODO(): don't use unsupported wheel library
    with wheelfile.WheelFile(wheel_path) as wheel_file:
        distribution_name = wheel_file.parsed_filename.group("name")
        library_name = util.normalize_distribution_name(distribution_name)
        package_directory = os.path.join(dest_directory, library_name)
        wheel_file.extractall(package_directory)

    try:
        return next(pkg_resources.find_distributions(package_directory))
    except StopIteration:
        raise DistributionNotFoundError(package_directory)
Example #2
0
 def setUp(self):
     super(TestPackagingWheels, self).setUp()
     self.useFixture(TestRepo(self.package_dir))
     # Build the wheel
     self.run_setup('bdist_wheel', allow_fail=False)
     # Slowly construct the path to the generated whl
     dist_dir = os.path.join(self.package_dir, 'dist')
     relative_wheel_filename = os.listdir(dist_dir)[0]
     absolute_wheel_filename = os.path.join(dist_dir,
                                            relative_wheel_filename)
     wheel_file = wheelfile.WheelFile(absolute_wheel_filename)
     wheel_name = wheel_file.parsed_filename.group('namever')
     # Create a directory path to unpack the wheel to
     self.extracted_wheel_dir = os.path.join(dist_dir, wheel_name)
     # Extract the wheel contents to the directory we just created
     wheel_file.extractall(self.extracted_wheel_dir)
     wheel_file.close()
Example #3
0
def get_package_info(package):
    package = str(package)
    name, description, long_description = '', '', ''
    if package.endswith('.whl'):
        wf = wheelfile.WheelFile(package)
        ef = wf.open(posixpath.join(wf.dist_info_path, 'METADATA'))
        pkg_info = pkginfo.read_pkg_info_bytes(ef.read())
        name = pkg_info['Name']
        description = pkg_info['Summary']
        long_description = pkg_info.get_payload()
        ef.close()
        wf.close()
    if package.endswith('.egg'):
        with zipfile.ZipFile(package) as zf:
            with zf.open(posixpath.join('EGG-INFO', 'PKG-INFO')) as fp:
                value = fp.read().decode('utf-8')
                pkg_info = email.parser.Parser().parsestr(value)
                name = pkg_info['Name']
                description = pkg_info['Summary']
                long_description = pkg_info['Description']
                long_description = '\n'.join((line.strip() for line in StringIO(long_description)))
    return name, description, long_description
Example #4
0
path = Path('~/git/spacy_conda/cymem').expanduser()
pkg = path / 'pkg/cymem-2.0.2-py37_0.tar.bz2'
whl = path / 'whl/cymem-2.0.2-cp37-cp37m-manylinux1_x86_64.whl'
pkg.exists(), whl.exists()

zpkg = tarfile.open(pkg, "r:bz2")

lpkg = zpkg.getmembers()
[(i, o.name) for i, o in enumerate(lpkg) if o.name.startswith('info/')]

f = (zpkg.extractfile('info/about.json').read().decode())
print(f)
fn.name

fwhl = wheelfile.WheelFile(whl)

fwhl.parsed_filename.groupdict()

fwhl.dist_info_path

fwhl.record_path

read_python_record(whl.parent, fwhl.record_path, '3.7')['paths_data'].paths

fwhl.namelist()


meta = fwhl.read(f'{fwhl.dist_info_path}/METADATA').decode()

m = Parser().parsestr(meta)
def parse_wheel(path):
    fsize = os.path.getsize(path)

    # open up wheel file (it's actually a zip)
    p = wheelfile.WheelFile(path)

    # look for the files we care about in the '<wheelname>.dist-info' directory
    metadata_file = None
    metadata_wheel = None
    for zipfile in p.filelist:
        parts = os.path.split(zipfile.filename)
        if len(parts) == 2 and parts[0].endswith(".dist-info"):
            if parts[1] == "METADATA":
                metadata_file = zipfile
            elif parts[1] == "WHEEL":
                metadata_wheel = zipfile

    assert (metadata_file), "METADATA file not found"
    assert (metadata_wheel), "WHEEL file not found"

    metadata_data = message_from_string(
        p.read(metadata_file.filename).decode("UTF-8"))
    wheel_data = message_from_string(
        p.read(metadata_wheel.filename).decode("UTF-8"))

    # get version and whatnot from the pkginfo. there will be multiple Tags with the same python and api, but
    # there can be varying platforms.
    python_versions = set()
    python_apis = set()
    python_platforms = set()

    for tag in wheel_data.get_all("Tag"):
        python_version, python_api, python_platform = tag.split(
            "-")  # ['py3', 'none', 'any']
        python_versions.update([python_version])
        python_apis.update([python_api])
        python_platforms.update([python_platform])

    assert (len(python_apis) == 1
            ), "wheel metadata python api list has other than 1 unique entry"

    # generate final platforming string
    python_version = '.'.join(sorted(list(python_versions), key=natural_keys))
    python_api = python_apis.pop()
    python_platform = '.'.join(sorted(list(python_platforms),
                                      key=natural_keys))

    buildtag = wheel_data["Build"]
    name_parts = [
        metadata_data["Name"], metadata_data["Version"], python_version,
        python_api, python_platform
    ]
    if buildtag:
        name_parts.insert(2, buildtag)

    assert (None not in name_parts), "Required metadata field missing"

    # construct filename, verify it matches what was submitted
    fname_parts = name_parts[:]
    fname_parts[0] = fname_parts[0].replace(
        "-", "_")  # replaces dashes in dist name with underscore
    wheelname = "-".join(fname_parts) + ".whl"

    return {
        "fields": {
            "dist": name_parts[0],
            "version": name_parts[1],
            "build": buildtag,
            "python": python_version,
            "api": python_api,
            "platform": python_platform
        },
        "wheel": wheel_data.items(),
        "metadata": metadata_data.items(),
        "description": metadata_data.get_payload(),
        "wheelname": wheelname,
        "size": fsize
    }