Mercurial > hg > PaInt
annotate paint/package.py @ 12:ceb8c69a16f3
dependencies should consume info
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 24 Feb 2012 15:21:28 -0800 |
parents | c41a946d34af |
children | 0dd1f8f83be2 |
rev | line source |
---|---|
4 | 1 """ |
2 package model for python PAckage INTrospection | |
3 """ | |
4 | |
7 | 5 import os |
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
|
6 import shutil |
7 | 7 import tarfile |
5 | 8 import tempfile |
7 | 9 import urllib2 |
10 import utils | |
5 | 11 |
10 | 12 __all__ = ['Package'] |
13 | |
4 | 14 class Package(object): |
7 | 15 # XXX much of this is generic resource stuff and should be split off |
4 | 16 |
17 def __init__(self, src): | |
18 self.src = src | |
5 | 19 self._tmppath = None |
20 | |
21 def path(self): | |
22 """filesystem path to package""" | |
7 | 23 |
24 # return cached copy if it exists | |
25 if self._tmppath: | |
26 return self._tmppath | |
27 | |
28 # fetch from the web if a URL | |
29 tmpfile = None | |
30 src = self.src | |
5 | 31 if utils.isURL(self.src): |
8 | 32 tmpfile = src = self.fetch() |
7 | 33 |
34 # unpack if an archive | |
35 if self.is_archive(src): | |
8 | 36 try: |
37 self.unpack(src) | |
38 finally: | |
39 if tmpfile: | |
40 os.remove(tmpfile) | |
7 | 41 return self._tmppath |
42 | |
5 | 43 return self.src |
44 | |
45 def fetch(self): | |
8 | 46 """fetch from remote source to a temporary file""" |
11 | 47 fd, filename = tempfile.mkstemp() |
48 resource = urllib2.urlopen(self.src) | |
7 | 49 os.write(fd, resource.read()) |
50 os.close(fd) | |
51 return filename | |
52 | |
53 def unpack(self, archive): | |
54 """unpack the archive to a temporary destination""" | |
55 # TODO: should handle zipfile additionally at least | |
56 # Ideally, this would be pluggable, etc | |
8 | 57 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
|
58 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
|
59 self._tmppath = tempfile.mkdtemp() |
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
|
60 tf.extractall(path=self._tmppath) |
7 | 61 |
62 def is_archive(self, path): | |
63 """returns if the filesystem path is an archive""" | |
64 # TODO: should handle zipfile additionally at least | |
65 # Ideally, this would be pluggable, etc | |
66 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
|
67 |
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
|
68 def cleanup(self): |
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
|
69 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
|
70 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
|
71 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
|
72 |
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
|
73 __del__ = cleanup |
11 | 74 |
75 def info(self): | |
12
ceb8c69a16f3
dependencies should consume info
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
76 """return info dictionary for package""" |
11 | 77 path = self.path() |
78 assert os.path.exists(os.path.join(path, 'setup.py')) | |
79 | |
80 def dependencies(self): | |
12
ceb8c69a16f3
dependencies should consume info
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
81 info = self.info() |