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