/
install_distribution.py
executable file
·93 lines (74 loc) · 2.93 KB
/
install_distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from optparse import OptionParser
from urllib import urlretrieve
import subprocess
import tarfile
import tempfile
import os
import shutil
from pypiclient import XmlRpcClient, ProjectDoesNotExist
class InstallDistribution(object):
"""Main class, provides an entry point for the install_distribution script.
"""
def __init__(self):
"""Retreive options, build the XmlRpcClient and launch the script.
"""
usage = '%prog packagename [version]'
self.parser = OptionParser(usage=usage)
self.options, self.args = self.parser.parse_args()
self.client = XmlRpcClient()
def run(self):
dist_name = self.get_distribution_name(self.args)
dist_version = self.get_distribution_version(self.args, dist_name)
if dist_name and dist_version:
self.install_distribution(dist_name, dist_version)
def get_distribution_name(self, args):
"""Get the name of the package from args.
"""
if 1 <= len(args) <= 2:
return args[0]
else:
self.parser.error(
"thanks to specify the package name as the first argument")
return None
def get_distribution_version(self, args, distribution_name):
"""Get distribution version from args, or by asking the user if
needed, displaying a the list of existing versions from Pypi
XmlRpcClient.
"""
argslen = len(args)
if argslen == 2:
distribution_version = args[1]
elif 1 <= argslen < 2:
# ask for the version number
try:
available_versions = self.client.get_project_versions(distribution_name)
print "Found versions of %s:" % distribution_name
for version in available_versions:
print "\t%s" % version
distribution_version = \
raw_input("Which version do you want to install (hit " \
"enter for None)? ")
except ProjectDoesNotExist as e:
print "The specified distribution does not exists: %s" % distribution_name
return None
if distribution_version == '':
distribution_version = None
return distribution_version
def install_distribution(self, name, version):
"""Download, extract and install the distribution by calling
python setup.py install
"""
url = self.client.get_project_url(name, version)
filename, headers = urlretrieve(url)
tar = tarfile.open(filename)
temppath = tempfile.mkdtemp()
tar.extractall(path=temppath)
path = '%s/%s/' % (temppath, tar.getnames()[0])
tar.close()
os.chdir(path)
os.system('python setup.py install')
shutil.rmtree(temppath)
if __name__ == "__main__":
main = InstallDistribution()
main.run()