4
|
1 """
|
|
2 package model for python PAckage INTrospection
|
|
3 """
|
|
4
|
7
|
5 import os
|
|
6 import tarfile
|
5
|
7 import tempfile
|
7
|
8 import urllib2
|
|
9 import utils
|
5
|
10
|
4
|
11 class Package(object):
|
7
|
12 # XXX much of this is generic resource stuff and should be split off
|
4
|
13
|
|
14 def __init__(self, src):
|
|
15 self.src = src
|
5
|
16 self._tmppath = None
|
|
17
|
|
18 def path(self):
|
|
19 """filesystem path to package"""
|
7
|
20
|
|
21 # return cached copy if it exists
|
|
22 if self._tmppath:
|
|
23 return self._tmppath
|
|
24
|
|
25 # fetch from the web if a URL
|
|
26 tmpfile = None
|
|
27 src = self.src
|
5
|
28 if utils.isURL(self.src):
|
7
|
29 tmpfile = self.fetch()
|
5
|
30 return self._tmppath
|
7
|
31
|
|
32 # unpack if an archive
|
|
33 if self.is_archive(src):
|
|
34 self.unpack(src)
|
|
35 return self._tmppath
|
|
36
|
5
|
37 return self.src
|
|
38
|
|
39 def fetch(self):
|
|
40 """fetch from remote source"""
|
7
|
41
|
|
42 fd, filename = tmpfile.mkstemp()
|
|
43 resource = urllib.urlopen(self.src)
|
|
44 os.write(fd, resource.read())
|
|
45 os.close(fd)
|
|
46 return filename
|
|
47
|
|
48 def unpack(self, archive):
|
|
49 """unpack the archive to a temporary destination"""
|
|
50 # TODO: should handle zipfile additionally at least
|
|
51 # Ideally, this would be pluggable, etc
|
|
52 assert tarfile.is_tarfile
|
|
53 tarfile.TarFile.open(
|
|
54
|
|
55 def is_archive(self, path):
|
|
56 """returns if the filesystem path is an archive"""
|
|
57 # TODO: should handle zipfile additionally at least
|
|
58 # Ideally, this would be pluggable, etc
|
|
59 return tarfile.is_tarfile(path)
|