1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

More work on the build-standalone script.

This commit is contained in:
Jannis Leidel 2010-07-30 18:56:27 +02:00
parent 90b9e7228f
commit 029d626d7c

View file

@ -1,55 +1,55 @@
#!/usr/bin/env python
import os
import glob
import os
import pip
import shutil
import sys
import tempfile
import zipfile
try:
import zlib
compression = zipfile.ZIP_DEFLATED
COMPRESSION = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
COMPRESSION = zipfile.ZIP_STORED
MAIN_PY_CONTENT = """\
MAIN_PY = """\
#!/usr/bin/env python
import pip
import sys
import pip, sys
sys.exit(pip.main())
"""
def main():
file_name = 'pipx'
root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
os.chdir(root_dir)
file_name = os.path.join(os.getcwd(), 'pipx')
temp_dir = tempfile.mkdtemp('-standalone', 'pip-')
dst = os.path.join(temp_dir, 'pip')
src = os.path.dirname(os.path.abspath(pip.__file__))
shutil.copytree(src, dst)
with open(os.path.join(temp_dir, '__main__.py'), 'w') as main_py:
main_py.write(MAIN_PY)
os.chdir(temp_dir)
zf = zipfile.PyZipFile(file_name, mode='w', compression=COMPRESSION)
try:
main_py = open('__main__.py', 'w')
main_py.write(MAIN_PY_CONTENT)
main_py.close()
zf = zipfile.PyZipFile(file_name, mode='w', compression=compression)
try:
zf.debug = 4
for package in ['pip', '__main__.py']:
zf.writepy(package)
finally:
zf.close()
pip_zip = open(file_name, 'r')
content = pip_zip.read()
pip_zip.close()
pip_zip = open(file_name, 'w')
pip_zip.write("#!/usr/bin/env python\n"+content)
pip_zip.close()
if hasattr(os, 'chmod'):
oldmode = os.stat(file_name).st_mode & 07777
newmode = (oldmode | 0555) & 07777
os.chmod(file_name, newmode)
print 'Made resulting file executable. Use it as `./%s`.' % file_name
zf.debug = 3
zf.writepy('pip')
zf.writepy('__main__.py')
finally:
for path in glob.glob('__main__.py*'):
os.remove(path)
zf.close()
with open(file_name, 'r') as pipz:
content = pipz.read()
with open(file_name, 'w') as pipz:
pipz.write("#!/usr/bin/env python\n"+content)
if hasattr(os, 'chmod'):
oldmode = os.stat(file_name).st_mode & 07777
newmode = (oldmode | 0555) & 07777
os.chmod(file_name, newmode)
print 'Made resulting file %s executable.' % file_name
if __name__ == '__main__':
main()