Mercurial > hg > PaInt
annotate paint/package.py @ 9:562230cc2e86
now the package class should at least extracts things. it doesnt actually do anything useful
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Thu, 23 Feb 2012 17:49:05 -0800 |
| parents | c1181e9c9ca2 |
| children | d63ae03cc300 |
| 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 |
| 4 | 12 class Package(object): |
| 7 | 13 # XXX much of this is generic resource stuff and should be split off |
| 4 | 14 |
| 15 def __init__(self, src): | |
| 16 self.src = src | |
| 5 | 17 self._tmppath = None |
| 18 | |
| 19 def path(self): | |
| 20 """filesystem path to package""" | |
| 7 | 21 |
| 22 # return cached copy if it exists | |
| 23 if self._tmppath: | |
| 24 return self._tmppath | |
| 25 | |
| 26 # fetch from the web if a URL | |
| 27 tmpfile = None | |
| 28 src = self.src | |
| 5 | 29 if utils.isURL(self.src): |
| 8 | 30 tmpfile = src = self.fetch() |
| 7 | 31 |
| 32 # unpack if an archive | |
| 33 if self.is_archive(src): | |
| 8 | 34 try: |
| 35 self.unpack(src) | |
| 36 finally: | |
| 37 if tmpfile: | |
| 38 os.remove(tmpfile) | |
| 7 | 39 return self._tmppath |
| 40 | |
| 5 | 41 return self.src |
| 42 | |
| 43 def fetch(self): | |
| 8 | 44 """fetch from remote source to a temporary file""" |
| 7 | 45 fd, filename = tmpfile.mkstemp() |
| 46 resource = urllib.urlopen(self.src) | |
| 47 os.write(fd, resource.read()) | |
| 48 os.close(fd) | |
| 49 return filename | |
| 50 | |
| 51 def unpack(self, archive): | |
| 52 """unpack the archive to a temporary destination""" | |
| 53 # TODO: should handle zipfile additionally at least | |
| 54 # Ideally, this would be pluggable, etc | |
| 8 | 55 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
|
56 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
|
57 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
|
58 tf.extractall(path=self._tmppath) |
| 7 | 59 |
| 60 def is_archive(self, path): | |
| 61 """returns if the filesystem path is an archive""" | |
| 62 # TODO: should handle zipfile additionally at least | |
| 63 # Ideally, this would be pluggable, etc | |
| 64 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
|
65 |
|
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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 |
|
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 __del__ = cleanup |
