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