Mercurial > hg > PaInt
annotate paint/package.py @ 46:bb3c646e9516
python 2.4 compatability
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 02 Apr 2012 10:43:56 -0700 |
parents | bb0813966bff |
children | 213cf933366d |
rev | line source |
---|---|
4 | 1 """ |
2 package model for python PAckage INTrospection | |
3 """ | |
4 | |
36 | 5 # TODO: use pkginfo.sdist more |
6 | |
7 | 7 import os |
28 | 8 import pip |
9
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
9 import shutil |
19 | 10 import subprocess |
11 import sys | |
7 | 12 import tarfile |
5 | 13 import tempfile |
7 | 14 import urllib2 |
21
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
15 import urlparse |
7 | 16 import utils |
5 | 17 |
19 | 18 try: |
19 from subprocess import check_call as call | |
20 except ImportError: | |
43
5e2e74cc5ad8
misspellings that affect things on ancient python
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
21 from subprocess import call |
19 | 22 |
10 | 23 __all__ = ['Package'] |
24 | |
4 | 25 class Package(object): |
33 | 26 """ |
27 class for python package introspection. | |
28 constructor takes the package 'src' | |
29 """ | |
4 | 30 |
31 def __init__(self, src): | |
32 self.src = src | |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
33 |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
34 # ephemeral data |
5 | 35 self._tmppath = None |
28 | 36 self._egg_info_path = None |
33 | 37 self._build_path = None |
40
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
38 self._pkg_info_path = None |
5 | 39 |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
40 # TODO: list of temporary files/directories to be deleted |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
41 |
34 | 42 def _path(self): |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
43 """filesystem path to package directory""" |
7 | 44 |
45 # return cached copy if it exists | |
46 if self._tmppath: | |
47 return self._tmppath | |
48 | |
49 # fetch from the web if a URL | |
50 tmpfile = None | |
51 src = self.src | |
5 | 52 if utils.isURL(self.src): |
8 | 53 tmpfile = src = self.fetch() |
7 | 54 |
55 # unpack if an archive | |
28 | 56 if self._is_archive(src): |
8 | 57 try: |
37 | 58 self._unpack(src) |
8 | 59 finally: |
60 if tmpfile: | |
61 os.remove(tmpfile) | |
7 | 62 return self._tmppath |
63 | |
5 | 64 return self.src |
65 | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
66 def fetch(self, filename=None): |
8 | 67 """fetch from remote source to a temporary file""" |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
68 if filename is None: |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
69 fd, filename = tempfile.mkstemp() |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
70 os.close(fd) |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
71 fp = file(filename, 'w') |
15
8c8b7482772f
fix a few failures but still failing
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
72 resource = urllib2.urlopen(self.src) |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
73 fp.write(resource.read()) |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
74 fp.close() |
7 | 75 return filename |
76 | |
37 | 77 def _unpack(self, archive): |
7 | 78 """unpack the archive to a temporary destination""" |
79 # TODO: should handle zipfile additionally at least | |
80 # Ideally, this would be pluggable, etc | |
8 | 81 assert tarfile.is_tarfile(archive), "%s is not an archive" % self.src |
9
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
82 tf = tarfile.TarFile.open(archive) |
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
83 self._tmppath = tempfile.mkdtemp() |
19 | 84 members = tf.getmembers() |
85 | |
86 # cut off the top level directory | |
37 | 87 members = [i for i in members if os.path.sep in i.name] |
19 | 88 tld = set() |
89 for member in members: | |
90 directory, member.name = member.name.split(os.path.sep, 1) | |
91 tld.add(directory) | |
92 assert len(tld) == 1 | |
93 | |
94 # extract | |
95 for member in members: | |
96 tf.extract(member, path=self._tmppath) | |
97 tf.close() | |
7 | 98 |
28 | 99 def _is_archive(self, path): |
7 | 100 """returns if the filesystem path is an archive""" |
101 # TODO: should handle zipfile additionally at least | |
102 # Ideally, this would be pluggable, etc | |
103 return tarfile.is_tarfile(path) | |
9
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
104 |
28 | 105 def _cleanup(self): |
9
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
106 if self._tmppath: |
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
107 shutil.rmtree(self._tmppath) |
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
108 self._tmppath = None |
562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
109 |
18
4c4d1e194bf7
leave in debugging code for now
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
110 # __del__ = cleanup |
11 | 111 |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
112 ### python-package-specific functionality |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
113 |
28 | 114 def _egg_info(self): |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
115 """build the egg_info directory""" |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
116 |
28 | 117 if self._egg_info_path: |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
118 # return cached copy |
28 | 119 return self._egg_info_path |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
120 |
34 | 121 directory = self._path() |
16 | 122 setup_py = os.path.join(directory, 'setup.py') |
123 if not os.path.exists(setup_py): | |
17 | 124 raise AssertionError("%s does not exist" % setup_py) |
11 | 125 |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
126 # setup the egg info |
39
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
127 exception = None |
38 | 128 try: |
39
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
129 code = call([sys.executable, 'setup.py', 'egg_info'], cwd=directory, stdout=subprocess.PIPE) |
45 | 130 except Exception, exception: |
39
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
131 pass |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
132 if code or exception: |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
133 message = """Failure to generate egg_info |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
134 - src: %s |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
135 - directory: %s |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
136 """ % (self.src, directory) |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
137 if exception: |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
138 sys.stderr.write(message) |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
139 raise exception |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
140 else: |
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
141 raise Exception(message) |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
142 |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
143 # get the .egg-info directory |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
144 egg_info = [i for i in os.listdir(directory) |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
145 if i.endswith('.egg-info')] |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
146 assert len(egg_info) == 1, 'Expected one .egg-info directory in %s, got: %s' % (directory, egg_info) |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
147 egg_info = os.path.join(directory, egg_info[0]) |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
148 assert os.path.isdir(egg_info), "%s is not a directory" % egg_info |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
149 |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
150 # cache it |
28 | 151 self._egg_info_path = egg_info |
152 return self._egg_info_path | |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
153 |
40
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
154 def _pkg_info(self): |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
155 """returns path to PKG-INFO file""" |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
156 |
39
cc25499e9485
note TODO....and cry a little
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
157 if self._pkg_info_path: |
40
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
158 # return cached value |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
159 return self._pkg_info_path |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
160 |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
161 try: |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
162 egg_info = self._egg_info() |
45 | 163 except Exception, exception: |
40
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
164 # try to get the package info from a file |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
165 path = self._path() |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
166 pkg_info = os.path.join(path, 'PKG-INFO') |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
167 if os.path.exists(pkg_info): |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
168 self._pkg_info_path = pkg_info |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
169 return self._pkg_info_path |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
170 raise Exception("Cannot find or generate PKG-INFO") |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
171 |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
172 pkg_info = os.path.join(egg_info, 'PKG-INFO') |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
173 assert os.path.exists(pkg_info) |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
174 self._pkg_info_path = pkg_info |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
175 return self._pkg_info_path |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
176 |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
177 def info(self): |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
178 """return info dictionary for package""" |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
179 # could use pkginfo module |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
180 |
06f21791eba1
add yet another indirection layer
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
181 pkg_info = self._pkg_info() |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
182 |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
183 # read the package information |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
184 info_dict = {} |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
185 for line in file(pkg_info).readlines(): |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
186 if not line or line[0].isspace(): |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
187 continue # XXX neglects description |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
188 assert ':' in line |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
189 key, value = [i.strip() for i in line.split(':', 1)] |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
190 info_dict[key] = value |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
191 |
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
192 # return the information |
20
11bc01a089e7
return only the dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
19
diff
changeset
|
193 return info_dict |
13
0dd1f8f83be2
moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
194 |
11 | 195 def dependencies(self): |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
196 """return the dependencies""" |
32 | 197 # TODO: should probably have a more detailed dict: |
198 # {'mozinfo': {'version': '>= 0.2', | |
199 # 'url': 'http://something.com/'}} | |
21
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
200 # get the egg_info directory |
28 | 201 egg_info = self._egg_info() |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
202 |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
203 # read the dependencies |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
204 requires = os.path.join(egg_info, 'requires.txt') |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
205 if os.path.exists(requires): |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
206 dependencies = [i.strip() for i in file(requires).readlines() if i.strip()] |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
207 else: |
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
208 dependencies = [] |
21
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
209 dependencies = dict([(i, None) for i in dependencies]) |
14
6d27c2136163
a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
210 |
21
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
211 # read the dependency links |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
212 dependency_links = os.path.join(egg_info, 'dependency_links.txt') |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
213 if os.path.exists(dependency_links): |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
214 links = [i.strip() for i in file(dependency_links).readlines() if i.strip()] |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
215 for link in links: |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
216 # XXX pretty ghetto |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
217 assert '#egg=' in link |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
218 url, dep = link.split('#egg=', 1) |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
219 if dep in dependencies: |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
220 dependencies[dep] = link |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
221 |
4df3e715817d
now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
222 return dependencies |
24 | 223 |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
224 def extension(self): |
35 | 225 """filename extension of the package""" |
226 | |
227 package = self.package() | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
228 |
35 | 229 # determine the extension (XXX hacky) |
230 extensions = ('.tar.gz', '.zip', '.tar.bz2') | |
231 for ext in extensions: | |
36 | 232 if package.endswith(ext): |
35 | 233 return ext |
234 | |
235 raise Exception("Extension %s not found: %s" % (extensions, package)) | |
236 | |
237 def package(self, destination=None): | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
238 """ |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
239 repackage the package to ensure its actually in the right form |
34 | 240 and return the path to the destination |
241 - destination: if given, path to put the build in [TODO] | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
242 """ |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
243 |
33 | 244 if self._build_path: |
34 | 245 if destination: |
246 shutil.copy(self._build_path, destination) | |
247 return os.path.abspath(destination) | |
248 | |
249 # return cached copy | |
33 | 250 return self._build_path |
251 | |
34 | 252 path = self._path() |
253 dist = os.path.join(path, 'dist') | |
254 if os.path.exists(dist): | |
255 shutil.rmtree(dist) | |
256 | |
257 call([sys.executable, 'setup.py', 'sdist'], cwd=path, stdout=subprocess.PIPE) | |
258 | |
259 assert os.path.exists(dist) | |
260 contents = os.listdir(dist) | |
261 assert len(contents) == 1 | |
262 | |
263 self._build_path = os.path.join(dist, contents[0]) | |
264 | |
265 # destination | |
266 # use an evil recursive trick | |
267 if destination: | |
36 | 268 return self.package(destination=destination) |
34 | 269 |
270 return self._build_path | |
271 | |
24 | 272 def download(self, directory): |
273 """download a package and all its dependencies using pip""" | |
28 | 274 if not os.path.exists(directory): |
275 os.makedirs(directory) | |
276 assert os.path.isdir(directory) | |
277 pip.main(['install', '--download', directory, self.src]) | |
278 | |
279 def pypi(self, directory): | |
280 """ | |
281 download packages for a pypi directory structure | |
282 http://k0s.org/portfolio/pypi.html | |
283 """ | |
29
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
284 if not os.path.exists(directory): |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
285 os.makedirs(directory) |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
286 assert os.path.isdir(directory) |
28 | 287 tempdir = tempfile.mkdtemp() |
288 try: | |
29
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
289 self.download(tempdir) |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
290 for package in os.listdir(tempdir): |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
291 |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
292 # full path |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
293 src = os.path.join(tempdir, package) |
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
294 |
35 | 295 # make a package of the thing |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
296 package = Package(src) |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
297 |
36 | 298 # get destination dirname, filename |
299 dirname, filename = package.pypi_path() | |
29
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
300 |
36 | 301 # make the directory if it doesn't exist |
302 subdir = os.path.join(directory, dirname) | |
303 if not os.path.exists(subdir): | |
304 os.makedirs(subdir) | |
305 assert os.path.isdir(subdir) | |
29
59524b6d25c0
implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
306 |
36 | 307 # move the file |
308 self.package(destination=os.path.join(subdir, filename)) | |
28 | 309 finally: |
310 shutil.rmtree(tempdir) | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
311 |
36 | 312 def pypi_path(self): |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
313 """ |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
314 returns subpath 2-tuple appropriate for pypi path structure: |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
315 http://k0s.org/portfolio/pypi.html |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
316 """ |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
317 info = self.info() |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
318 |
36 | 319 # determine the extension |
320 extension = self.extension() | |
31
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
321 |
5fb1844db0b2
back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
322 # get the filename destination |
36 | 323 name = info['Name'] |
324 version = info['Version'] | |
325 filename = '%s-%s%s' % (name, version, extension) | |
326 return name, filename |