comparison paint/package.py @ 7:a7bf894c96c7

yet more stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 23 Feb 2012 17:41:54 -0800
parents 5b74792d46e4
children c1181e9c9ca2
comparison
equal deleted inserted replaced
6:6a6da56314b1 7:a7bf894c96c7
1 """ 1 """
2 package model for python PAckage INTrospection 2 package model for python PAckage INTrospection
3 """ 3 """
4 4
5 import os
6 import tarfile
5 import tempfile 7 import tempfile
8 import urllib2
9 import utils
6 10
7 class Package(object): 11 class Package(object):
12 # XXX much of this is generic resource stuff and should be split off
8 13
9 def __init__(self, src): 14 def __init__(self, src):
10 self.src = src 15 self.src = src
11 self._tmppath = None 16 self._tmppath = None
12 17
13 def path(self): 18 def path(self):
14 """filesystem path to package""" 19 """filesystem path to package"""
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
15 if utils.isURL(self.src): 28 if utils.isURL(self.src):
16 if self._tmppath: 29 tmpfile = self.fetch()
17 return self._tmppath
18 self.fetch()
19 return self._tmppath 30 return self._tmppath
31
32 # unpack if an archive
33 if self.is_archive(src):
34 self.unpack(src)
35 return self._tmppath
36
20 return self.src 37 return self.src
21 38
22 def fetch(self): 39 def fetch(self):
23 """fetch from remote source""" 40 """fetch from remote source"""
24 self._tmppath = tempfile.mkdtemp() 41
25 raise NotImplementedError 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)