churchyard / rpms / python3

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