comparison paint/package.py @ 8:c1181e9c9ca2

yet more stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 23 Feb 2012 17:45:47 -0800
parents a7bf894c96c7
children 562230cc2e86
comparison
equal deleted inserted replaced
7:a7bf894c96c7 8:c1181e9c9ca2
24 24
25 # fetch from the web if a URL 25 # fetch from the web if a URL
26 tmpfile = None 26 tmpfile = None
27 src = self.src 27 src = self.src
28 if utils.isURL(self.src): 28 if utils.isURL(self.src):
29 tmpfile = self.fetch() 29 tmpfile = src = self.fetch()
30 return self._tmppath
31 30
32 # unpack if an archive 31 # unpack if an archive
33 if self.is_archive(src): 32 if self.is_archive(src):
34 self.unpack(src) 33 try:
34 self.unpack(src)
35 finally:
36 if tmpfile:
37 os.remove(tmpfile)
35 return self._tmppath 38 return self._tmppath
36 39
37 return self.src 40 return self.src
38 41
39 def fetch(self): 42 def fetch(self):
40 """fetch from remote source""" 43 """fetch from remote source to a temporary file"""
41
42 fd, filename = tmpfile.mkstemp() 44 fd, filename = tmpfile.mkstemp()
43 resource = urllib.urlopen(self.src) 45 resource = urllib.urlopen(self.src)
44 os.write(fd, resource.read()) 46 os.write(fd, resource.read())
45 os.close(fd) 47 os.close(fd)
46 return filename 48 return filename
47 49
48 def unpack(self, archive): 50 def unpack(self, archive):
49 """unpack the archive to a temporary destination""" 51 """unpack the archive to a temporary destination"""
50 # TODO: should handle zipfile additionally at least 52 # TODO: should handle zipfile additionally at least
51 # Ideally, this would be pluggable, etc 53 # Ideally, this would be pluggable, etc
52 assert tarfile.is_tarfile 54 assert tarfile.is_tarfile(archive), "%s is not an archive" % self.src
53 tarfile.TarFile.open( 55 tarfile.TarFile.open(archive)
54 56
55 def is_archive(self, path): 57 def is_archive(self, path):
56 """returns if the filesystem path is an archive""" 58 """returns if the filesystem path is an archive"""
57 # TODO: should handle zipfile additionally at least 59 # TODO: should handle zipfile additionally at least
58 # Ideally, this would be pluggable, etc 60 # Ideally, this would be pluggable, etc