Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pip bundle from pip2pi #19

Merged
merged 15 commits into from
Dec 4, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 28 additions & 39 deletions libpip2pi/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import cgi
import shutil
import atexit
import zipfile
#import zipfile
import tempfile
import textwrap
import functools
from subprocess import check_call
import pkg_resources
import glob

def dedent(text):
return textwrap.dedent(text.lstrip("\n"))
Expand Down Expand Up @@ -55,6 +56,28 @@ def file_to_package(file, basedir=None):
raise ValueError(msg)
return (split[0], pkg_resources.safe_name(split[1]))

def archive_pip_packages(path, package_cmds):
use_pip_main = False
try:
import pip
pip_dist = pkg_resources.get_distribution('pip')
version = pip_dist.version

if version < '1.1':
raise RuntimeError('pip >= 1.1 required, %s installed' % version)

use_pip_main = True
except ImportError:
print '\n===\nWARNING:\nCannot import `pip` - falling back to using the pip executable.'
print '(This will be deprecated in a future release.)\n===\n'

if use_pip_main:
cmds = ['install', '-d', path]
cmds.extend(package_cmds)
pip.main(cmds)
else:
check_call(["pip", "install", "-d", path] + package_cmds)

def dir2pi(argv=sys.argv):
if len(argv) != 2:
print(dedent("""
Expand Down Expand Up @@ -145,45 +168,11 @@ def pip2tgz(argv=sys.argv):
if not os.path.exists(outdir):
os.mkdir(outdir)

tempdir = os.path.join(outdir, "_pip2tgz_temp")
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
os.mkdir(tempdir)

bundle_zip = os.path.join(tempdir, "bundle.zip")
build_dir = os.path.join(tempdir, "build")
check_call(["pip", "bundle", "-b", build_dir, bundle_zip] + argv[2:])

os.chdir(tempdir)
if os.path.exists(build_dir):
zipfile.ZipFile("bundle.zip").extract("pip-manifest.txt")
else:
# Older versions of pip delete the "build" directory after they
# are done with it... So extract the entire bundle.zip
zipfile.ZipFile("bundle.zip").extractall()

num_pakages = 0
for line in open("pip-manifest.txt"):
line = line.strip()
if not line or line.startswith("#"):
continue
pkg_version = line.split("==")
if len(pkg_version) != 2:
bundle_file = os.path.abspath("pip-manifest.txt")
raise ValueError("surprising line in %r: %r"
%(bundle_file, line, ))
pkg, version = pkg_version
version = version.replace("-", "_")
old_input_dir = os.path.join("build/", pkg)
new_input_dir = "%s-%s" %(pkg, version)
os.rename(old_input_dir, new_input_dir)
output_name = os.path.join("..", new_input_dir + ".tar.gz")
check_call(["tar", "-czf", output_name, new_input_dir])
num_pakages += 1

archive_pip_packages(outdir, argv[2:])
os.chdir(outdir)
shutil.rmtree(tempdir)
print("%s .tar.gz saved to %r" %(num_pakages, argv[1]))
num_pakages = len(glob.glob('./*.tar.*'))

print("\nDone. %s archives currently saved in %r." %(num_pakages, argv[1]))
return 0

def pip2pi(argv=sys.argv):
Expand Down