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