Try something like this:
#!/usr/bin/env python
import os
import os.path
import subprocess
import sys
import tempfile
def distributions(path):
# Extrapolate from paths which distributions presently exist.
(parent, child) = os.path.split(path)
while child is not '' and not child.startswith('python'):
(parent, child) = os.path.split(parent)
if len(child) > 0:
rel = os.path.relpath(path, os.path.join(parent, child))
ret = []
for distro in os.listdir(parent):
if distro.startswith('python'):
dir = os.path.join(os.path.join(parent, distro), rel)
if os.path.isdir(dir):
ret.append((distro, dir))
ret.sort()
return ret
return []
def packages(dir):
return [pkg.split('-')[0] for pkg in os.listdir(dir)]
def migrate(old, new):
print 'moving all packages found in ' + old[0] + ' (' + old[1] + ') to ' + new[0] + ' (' + new[1] + ')'
f = tempfile.TemporaryFile(mode = 'w+')
result = subprocess.call(
['which', 'easy_install'], stdout = f, stderr = subprocess.PIPE)
f.seek(0)
easy_install = f.readline().strip()
f.close()
if os.path.isfile(easy_install):
pkgs = packages(old[1])
success = []
failure = []
for pkg in pkgs:
# Invoke easy_install on the package
print 'installing "' + pkg + '" for ' + new[0]
result = subprocess.call(
[new[0], easy_install, pkg])
if result != 0:
failure.append(pkg)
print 'failed'
else:
success.append(pkg)
print 'success'
print str(len(success)) + ' of ' + str(len(pkgs)) + ' succeeded'
else:
print 'Unable to locate easy_install script'
if __name__ == '__main__':
package_path = sys.path[-1]
distros = distributions(package_path)
migrate(distros[0], distros[1])
list(package_path)