churchyard / rpms / python3

Forked from rpms/python3 6 years ago
Clone

Blame 00189-add-rewheel-module.patch

ed631df
diff -Nur Python-3.4.1/Lib/ensurepip/__init__.py Python-3.4.1-rewheel/Lib/ensurepip/__init__.py
ed631df
--- Python-3.4.1/Lib/ensurepip/__init__.py	2014-08-21 10:49:30.792695824 +0200
ed631df
+++ Python-3.4.1-rewheel/Lib/ensurepip/__init__.py	2014-08-21 10:10:41.958341726 +0200
Matej Stuchlik 11fb599
@@ -1,8 +1,10 @@
Matej Stuchlik 11fb599
 import os
Matej Stuchlik 11fb599
 import os.path
Matej Stuchlik 11fb599
 import pkgutil
Matej Stuchlik 11fb599
+import shutil
Matej Stuchlik 11fb599
 import sys
Matej Stuchlik 11fb599
 import tempfile
Matej Stuchlik 11fb599
+from ensurepip import rewheel
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
 __all__ = ["version", "bootstrap"]
ed631df
@@ -38,6 +40,8 @@
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
     # Install the bundled software
Matej Stuchlik 11fb599
     import pip
Matej Stuchlik 11fb599
+    if args[0] in ["install", "list", "wheel"]:
Matej Stuchlik 11fb599
+        args.append('--pre')
Matej Stuchlik 11fb599
     pip.main(args)
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
 
ed631df
@@ -87,20 +91,39 @@
Matej Stuchlik 11fb599
         # omit pip and easy_install
Matej Stuchlik 11fb599
         os.environ["ENSUREPIP_OPTIONS"] = "install"
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
+    whls = []
Matej Stuchlik 11fb599
+    rewheel_dir = None
Matej Stuchlik 11fb599
+    # try to see if we have system-wide versions of _PROJECTS
Matej Stuchlik 11fb599
+    dep_records = rewheel.find_system_records([p[0] for p in _PROJECTS])
Matej Stuchlik 11fb599
+    # TODO: check if system-wide versions are the newest ones
Matej Stuchlik 11fb599
+    # if --upgrade is used?
Matej Stuchlik 11fb599
+    if all(dep_records):
Matej Stuchlik 11fb599
+        # if we have all _PROJECTS installed system-wide, we'll recreate
Matej Stuchlik 11fb599
+        # wheels from them and install those
Matej Stuchlik 11fb599
+        rewheel_dir = tempfile.TemporaryDirectory()
Matej Stuchlik 11fb599
+        for dr in dep_records:
Matej Stuchlik 11fb599
+            new_whl = rewheel.rewheel_from_record(dr, rewheel_dir.name)
Matej Stuchlik 11fb599
+            whls.append(os.path.join(rewheel_dir.name, new_whl))
Matej Stuchlik 11fb599
+    else:
Matej Stuchlik 11fb599
+        # if we don't have all the _PROJECTS installed system-wide,
Matej Stuchlik 11fb599
+        # let's just fall back to bundled wheels
ed631df
+        for project, version in _PROJECTS:
Matej Stuchlik 11fb599
+            whl = os.path.join(
Matej Stuchlik 11fb599
+                os.path.dirname(__file__),
Matej Stuchlik 1014fca
+                "_bundled",
Matej Stuchlik 11fb599
+                "{}-{}-py2.py3-none-any.whl".format(project, version)
ed631df
+            )
ed631df
+            whls.append(whl)
ed631df
+
ed631df
     with tempfile.TemporaryDirectory() as tmpdir:
ed631df
         # Put our bundled wheels into a temporary directory and construct the
ed631df
         # additional paths that need added to sys.path
ed631df
         additional_paths = []
ed631df
-        for project, version in _PROJECTS:
ed631df
-            wheel_name = "{}-{}-py2.py3-none-any.whl".format(project, version)
ed631df
-            whl = pkgutil.get_data(
ed631df
-                "ensurepip",
ed631df
-                "_bundled/{}".format(wheel_name),
ed631df
-            )
Matej Stuchlik 11fb599
-            with open(os.path.join(tmpdir, wheel_name), "wb") as fp:
Matej Stuchlik 11fb599
-                fp.write(whl)
ed631df
-
Matej Stuchlik 11fb599
-            additional_paths.append(os.path.join(tmpdir, wheel_name))
Matej Stuchlik 11fb599
+        for whl in whls:
Matej Stuchlik 11fb599
+            shutil.copy(whl, tmpdir)
Matej Stuchlik 11fb599
+            additional_paths.append(os.path.join(tmpdir, os.path.basename(whl)))
Matej Stuchlik 11fb599
+        if rewheel_dir:
Matej Stuchlik 11fb599
+            rewheel_dir.cleanup()
Matej Stuchlik 11fb599
 
Matej Stuchlik 11fb599
         # Construct the arguments to be passed to the pip command
Matej Stuchlik 11fb599
         args = ["install", "--no-index", "--find-links", tmpdir]
ed631df
diff -Nur Python-3.4.1/Lib/ensurepip/rewheel/__init__.py Python-3.4.1-rewheel/Lib/ensurepip/rewheel/__init__.py
ed631df
--- Python-3.4.1/Lib/ensurepip/rewheel/__init__.py	1970-01-01 01:00:00.000000000 +0100
ed631df
+++ Python-3.4.1-rewheel/Lib/ensurepip/rewheel/__init__.py	2014-08-21 10:11:22.560320121 +0200
ed631df
@@ -0,0 +1,143 @@
Matej Stuchlik 11fb599
+import argparse
ed631df
+import codecs
Matej Stuchlik 11fb599
+import csv
Matej Stuchlik 11fb599
+import email.parser
Matej Stuchlik 11fb599
+import os
Matej Stuchlik 11fb599
+import io
Matej Stuchlik 11fb599
+import re
Matej Stuchlik 11fb599
+import site
Matej Stuchlik 11fb599
+import subprocess
Matej Stuchlik 11fb599
+import sys
Matej Stuchlik 11fb599
+import zipfile
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+def run():
Matej Stuchlik 11fb599
+    parser = argparse.ArgumentParser(description='Recreate wheel of package with given RECORD.')
Matej Stuchlik 11fb599
+    parser.add_argument('record_path',
Matej Stuchlik 11fb599
+                        help='Path to RECORD file')
Matej Stuchlik 11fb599
+    parser.add_argument('-o', '--output-dir',
Matej Stuchlik 11fb599
+                        help='Dir where to place the wheel, defaults to current working dir.',
Matej Stuchlik 11fb599
+                        dest='outdir',
Matej Stuchlik 11fb599
+                        default=os.path.curdir)
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    ns = parser.parse_args()
Matej Stuchlik 11fb599
+    retcode = 0
Matej Stuchlik 11fb599
+    try:
Matej Stuchlik 11fb599
+        print(rewheel_from_record(**vars(ns)))
Matej Stuchlik 11fb599
+    except BaseException as e:
Matej Stuchlik 11fb599
+        print('Failed: {}'.format(e))
Matej Stuchlik 11fb599
+        retcode = 1
Matej Stuchlik 11fb599
+    sys.exit(1)
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+def find_system_records(projects):
Matej Stuchlik 11fb599
+    """Return list of paths to RECORD files for system-installed projects.
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    If a project is not installed, the resulting list contains None instead
Matej Stuchlik 11fb599
+    of a path to its RECORD
Matej Stuchlik 11fb599
+    """
Matej Stuchlik 11fb599
+    records = []
Matej Stuchlik 11fb599
+    # get system site-packages dirs
Matej Stuchlik 11fb599
+    sys_sitepack = site.getsitepackages([sys.base_prefix, sys.base_exec_prefix])
Matej Stuchlik 11fb599
+    sys_sitepack = [sp for sp in sys_sitepack if os.path.exists(sp)]
Matej Stuchlik 11fb599
+    # try to find all projects in all system site-packages
Matej Stuchlik 11fb599
+    for project in projects:
Matej Stuchlik 11fb599
+        path = None
Matej Stuchlik 11fb599
+        for sp in sys_sitepack:
ace4eac
+            dist_info_re = os.path.join(sp, project) + r'-[^\{0}]+\.dist-info'.format(os.sep)
Matej Stuchlik 11fb599
+            candidates = [os.path.join(sp, p) for p in os.listdir(sp)]
Matej Stuchlik 11fb599
+            # filter out candidate dirs based on the above regexp
Matej Stuchlik 11fb599
+            filtered = [c for c in candidates if re.match(dist_info_re, c)]
Matej Stuchlik 11fb599
+            # if we have 0 or 2 or more dirs, something is wrong...
Matej Stuchlik 11fb599
+            if len(filtered) == 1:
Matej Stuchlik 11fb599
+                path = filtered[0]
Matej Stuchlik cfa9e52
+        if path is not None:
Matej Stuchlik cfa9e52
+            records.append(os.path.join(path, 'RECORD'))
Matej Stuchlik cfa9e52
+        else:
Matej Stuchlik cfa9e52
+            records.append(None)
Matej Stuchlik 11fb599
+    return records
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+def rewheel_from_record(record_path, outdir):
Matej Stuchlik 11fb599
+    """Recreates a whee of package with given record_path and returns path
Matej Stuchlik 11fb599
+    to the newly created wheel."""
Matej Stuchlik 11fb599
+    site_dir = os.path.dirname(os.path.dirname(record_path))
Matej Stuchlik 11fb599
+    record_relpath = record_path[len(site_dir):].strip(os.path.sep)
Matej Stuchlik 11fb599
+    to_write, to_omit = get_records_to_pack(site_dir, record_relpath)
Matej Stuchlik 11fb599
+    new_wheel_name = get_wheel_name(record_path)
Matej Stuchlik 11fb599
+    new_wheel_path = os.path.join(outdir, new_wheel_name + '.whl')
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    new_wheel = zipfile.ZipFile(new_wheel_path, mode='w', compression=zipfile.ZIP_DEFLATED)
Matej Stuchlik 11fb599
+    # we need to write a new record with just the files that we will write,
Matej Stuchlik 11fb599
+    # e.g. not binaries and *.pyc/*.pyo files
Matej Stuchlik 11fb599
+    new_record = io.StringIO()
Matej Stuchlik 11fb599
+    writer = csv.writer(new_record)
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    # handle files that we can write straight away
Matej Stuchlik 11fb599
+    for f, sha_hash, size in to_write:
Matej Stuchlik 11fb599
+        new_wheel.write(os.path.join(site_dir, f), arcname=f)
Matej Stuchlik 11fb599
+        writer.writerow([f, sha_hash,size])
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    # rewrite the old wheel file with a new computed one
Matej Stuchlik 11fb599
+    writer.writerow([record_relpath, '', ''])
Matej Stuchlik 11fb599
+    new_wheel.writestr(record_relpath, new_record.getvalue())
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    new_wheel.close()
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    return new_wheel.filename
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+def get_wheel_name(record_path):
Matej Stuchlik 11fb599
+    """Return proper name of the wheel, without .whl."""
ed631df
+
Matej Stuchlik 11fb599
+    wheel_info_path = os.path.join(os.path.dirname(record_path), 'WHEEL')
ed631df
+    with codecs.open(wheel_info_path, encoding='utf-8') as wheel_info_file:
ed631df
+        wheel_info = email.parser.Parser().parsestr(wheel_info_file.read())
ed631df
+
Matej Stuchlik 11fb599
+    metadata_path = os.path.join(os.path.dirname(record_path), 'METADATA')
ed631df
+    with codecs.open(metadata_path, encoding='utf-8') as metadata_file:
ed631df
+        metadata = email.parser.Parser().parsestr(metadata_file.read())
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    # construct name parts according to wheel spec
Matej Stuchlik 11fb599
+    distribution = metadata.get('Name')
Matej Stuchlik 11fb599
+    version = metadata.get('Version')
Matej Stuchlik 11fb599
+    build_tag = '' # nothing for now
Matej Stuchlik 11fb599
+    lang_tag = []
Matej Stuchlik 11fb599
+    for t in wheel_info.get_all('Tag'):
Matej Stuchlik 11fb599
+        lang_tag.append(t.split('-')[0])
Matej Stuchlik 11fb599
+    lang_tag = '.'.join(lang_tag)
Matej Stuchlik 11fb599
+    abi_tag, plat_tag = wheel_info.get('Tag').split('-')[1:3]
Matej Stuchlik 11fb599
+    # leave out build tag, if it is empty
Matej Stuchlik 11fb599
+    to_join = filter(None, [distribution, version, build_tag, lang_tag, abi_tag, plat_tag])
Matej Stuchlik 11fb599
+    return '-'.join(list(to_join))
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+def get_records_to_pack(site_dir, record_relpath):
Matej Stuchlik 11fb599
+    """Accepts path of sitedir and path of RECORD file relative to it.
Matej Stuchlik 11fb599
+    Returns two lists:
Matej Stuchlik 11fb599
+    - list of files that can be written to new RECORD straight away
Matej Stuchlik 11fb599
+    - list of files that shouldn't be written or need some processing
Matej Stuchlik 11fb599
+      (pyc and pyo files, scripts)
Matej Stuchlik 11fb599
+    """
ed631df
+    record_file_path = os.path.join(site_dir, record_relpath)
ed631df
+    with codecs.open(record_file_path, encoding='utf-8') as record_file:
ed631df
+        record_contents = record_file.read()
Matej Stuchlik 11fb599
+    # temporary fix for https://github.com/pypa/pip/issues/1376
Matej Stuchlik 11fb599
+    # we need to ignore files under ".data" directory
Matej Stuchlik 11fb599
+    data_dir = os.path.dirname(record_relpath).strip(os.path.sep)
Matej Stuchlik 11fb599
+    data_dir = data_dir[:-len('dist-info')] + 'data'
Matej Stuchlik 11fb599
+
Matej Stuchlik 11fb599
+    to_write = []
Matej Stuchlik 11fb599
+    to_omit = []
Matej Stuchlik 11fb599
+    for l in record_contents.splitlines():
Matej Stuchlik 11fb599
+        spl = l.split(',')
Matej Stuchlik 11fb599
+        if len(spl) == 3:
Matej Stuchlik 11fb599
+            # new record will omit (or write differently):
Matej Stuchlik 11fb599
+            # - abs paths, paths with ".." (entry points),
Matej Stuchlik 11fb599
+            # - pyc+pyo files
Matej Stuchlik 11fb599
+            # - the old RECORD file
Matej Stuchlik 11fb599
+            # TODO: is there any better way to recognize an entry point?
Matej Stuchlik 11fb599
+            if os.path.isabs(spl[0]) or spl[0].startswith('..') or \
Matej Stuchlik 11fb599
+               spl[0].endswith('.pyc') or spl[0].endswith('.pyo') or \
Matej Stuchlik 11fb599
+               spl[0] == record_relpath or spl[0].startswith(data_dir):
Matej Stuchlik 11fb599
+                to_omit.append(spl)
Matej Stuchlik 11fb599
+            else:
Matej Stuchlik 11fb599
+                to_write.append(spl)
Matej Stuchlik 11fb599
+        else:
Matej Stuchlik 11fb599
+            pass # bad RECORD or empty line
Matej Stuchlik 11fb599
+    return to_write, to_omit
ed631df
diff -Nur Python-3.4.1/Makefile.pre.in Python-3.4.1-rewheel/Makefile.pre.in
ed631df
--- Python-3.4.1/Makefile.pre.in	2014-08-21 10:49:31.512695040 +0200
ed631df
+++ Python-3.4.1-rewheel/Makefile.pre.in	2014-08-21 10:10:41.961341722 +0200
ed631df
@@ -1145,7 +1145,7 @@
Matej Stuchlik 11fb599
 		test/test_asyncio \
Matej Stuchlik 11fb599
 		collections concurrent concurrent/futures encodings \
Matej Stuchlik 11fb599
 		email email/mime test/test_email test/test_email/data \
Matej Stuchlik 11fb599
-		ensurepip ensurepip/_bundled \
Matej Stuchlik 11fb599
+		ensurepip ensurepip/_bundled ensurepip/rewheel \
Matej Stuchlik 11fb599
 		html json test/test_json http dbm xmlrpc \
Matej Stuchlik 11fb599
 		sqlite3 sqlite3/test \
Matej Stuchlik 11fb599
 		logging csv wsgiref urllib \