annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 package model for python PAckage INTrospection
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
5 import os
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
6 import tarfile
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
7 import tempfile
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
8 import urllib2
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
9 import utils
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
10
4
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 class Package(object):
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
12 # XXX much of this is generic resource stuff and should be split off
4
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 def __init__(self, src):
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 self.src = src
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
16 self._tmppath = None
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
17
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
18 def path(self):
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
19 """filesystem path to package"""
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
20
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
21 # return cached copy if it exists
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
22 if self._tmppath:
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
23 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
24
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
25 # fetch from the web if a URL
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
26 tmpfile = None
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
27 src = self.src
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
28 if utils.isURL(self.src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
29 tmpfile = src = self.fetch()
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
30
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
31 # unpack if an archive
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
32 if self.is_archive(src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
33 try:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
34 self.unpack(src)
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
35 finally:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
36 if tmpfile:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
37 os.remove(tmpfile)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
38 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
39
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
40 return self.src
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
41
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
42 def fetch(self):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
43 """fetch from remote source to a temporary file"""
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
44 fd, filename = tmpfile.mkstemp()
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
45 resource = urllib.urlopen(self.src)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
46 os.write(fd, resource.read())
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
47 os.close(fd)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
48 return filename
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
49
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
50 def unpack(self, archive):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
51 """unpack the archive to a temporary destination"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
52 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
53 # Ideally, this would be pluggable, etc
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
54 assert tarfile.is_tarfile(archive), "%s is not an archive" % self.src
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
55 tarfile.TarFile.open(archive)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
56
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
57 def is_archive(self, path):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
58 """returns if the filesystem path is an archive"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
59 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
60 # Ideally, this would be pluggable, etc
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
61 return tarfile.is_tarfile(path)