mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
Merge pull request #1798 from dstufft/base85-get-pip
Use base85 instead of base64 for the bundled pip
This commit is contained in:
commit
fb46902a11
|
@ -40,7 +40,7 @@ def installer(installer_path=os.path.join(paths.CONTRIB, "get-pip.py")):
|
||||||
print("[generate.installer] Generating installer")
|
print("[generate.installer] Generating installer")
|
||||||
|
|
||||||
# Define our wrapper script
|
# Define our wrapper script
|
||||||
WRAPPER_SCRIPT = b"""
|
WRAPPER_SCRIPT = """
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#
|
#
|
||||||
# Hi There!
|
# Hi There!
|
||||||
|
@ -67,13 +67,61 @@ ZIPFILE = b\"\"\"
|
||||||
{zipfile}
|
{zipfile}
|
||||||
\"\"\"
|
\"\"\"
|
||||||
|
|
||||||
import base64
|
|
||||||
import os.path
|
import os.path
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
import struct
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
# Useful for very coarse version differentiation.
|
||||||
|
PY2 = sys.version_info[0] == 2
|
||||||
|
PY3 = sys.version_info[0] == 3
|
||||||
|
|
||||||
|
if PY3:
|
||||||
|
iterbytes = iter
|
||||||
|
else:
|
||||||
|
def iterbytes(buf):
|
||||||
|
return (ord(byte) for byte in buf)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from base64 import b85decode
|
||||||
|
except ImportError:
|
||||||
|
_b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{{|}}~")
|
||||||
|
|
||||||
|
def b85decode(b):
|
||||||
|
_b85dec = [None] * 256
|
||||||
|
for i, c in enumerate(iterbytes(_b85alphabet)):
|
||||||
|
_b85dec[c] = i
|
||||||
|
|
||||||
|
padding = (-len(b)) % 5
|
||||||
|
b = b + b'~' * padding
|
||||||
|
out = []
|
||||||
|
packI = struct.Struct('!I').pack
|
||||||
|
for i in range(0, len(b), 5):
|
||||||
|
chunk = b[i:i + 5]
|
||||||
|
acc = 0
|
||||||
|
try:
|
||||||
|
for c in iterbytes(chunk):
|
||||||
|
acc = acc * 85 + _b85dec[c]
|
||||||
|
except TypeError:
|
||||||
|
for j, c in enumerate(iterbytes(chunk)):
|
||||||
|
if _b85dec[c] is None:
|
||||||
|
raise ValueError('bad base85 character at position %d'
|
||||||
|
% (i + j))
|
||||||
|
raise
|
||||||
|
try:
|
||||||
|
out.append(packI(acc))
|
||||||
|
except struct.error:
|
||||||
|
raise ValueError('base85 overflow in hunk starting at byte %d'
|
||||||
|
% i)
|
||||||
|
|
||||||
|
result = b''.join(out)
|
||||||
|
if padding:
|
||||||
|
result = result[:-padding]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def bootstrap(tmpdir=None):
|
def bootstrap(tmpdir=None):
|
||||||
# Import pip so we can use it to install pip and maybe setuptools too
|
# Import pip so we can use it to install pip and maybe setuptools too
|
||||||
|
@ -131,7 +179,7 @@ def main():
|
||||||
# Unpack the zipfile into the temporary directory
|
# Unpack the zipfile into the temporary directory
|
||||||
pip_zip = os.path.join(tmpdir, "pip.zip")
|
pip_zip = os.path.join(tmpdir, "pip.zip")
|
||||||
with open(pip_zip, "wb") as fp:
|
with open(pip_zip, "wb") as fp:
|
||||||
fp.write(base64.decodestring(ZIPFILE))
|
fp.write(b85decode(ZIPFILE.replace(b"\\n", b"")))
|
||||||
|
|
||||||
# Add the zipfile to sys.path so that we can import it
|
# Add the zipfile to sys.path so that we can import it
|
||||||
sys.path.insert(0, pip_zip)
|
sys.path.insert(0, pip_zip)
|
||||||
|
@ -188,8 +236,15 @@ if __name__ == "__main__":
|
||||||
"[generate.installer] Write the wrapper script with the bundled zip "
|
"[generate.installer] Write the wrapper script with the bundled zip "
|
||||||
"file"
|
"file"
|
||||||
)
|
)
|
||||||
with open(installer_path, "wb") as fp:
|
|
||||||
fp.write(WRAPPER_SCRIPT.format(zipfile=base64.encodestring(data)))
|
zipdata = base64.b85encode(data).decode("utf8")
|
||||||
|
chunked = []
|
||||||
|
|
||||||
|
for i in range(0, len(zipdata), 79):
|
||||||
|
chunked.append(zipdata[i:i + 79])
|
||||||
|
|
||||||
|
with open(installer_path, "w") as fp:
|
||||||
|
fp.write(WRAPPER_SCRIPT.format(zipfile="\n".join(chunked)))
|
||||||
|
|
||||||
# Ensure the permissions on the newly created file
|
# Ensure the permissions on the newly created file
|
||||||
oldmode = os.stat(installer_path).st_mode & 0o7777
|
oldmode = os.stat(installer_path).st_mode & 0o7777
|
||||||
|
|
Loading…
Reference in a new issue