annotate 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
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):
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
29 tmpfile = self.fetch()
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
30 return self._tmppath
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
31
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
32 # unpack if an archive
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
33 if self.is_archive(src):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
34 self.unpack(src)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
35 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
36
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
37 return self.src
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
38
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
39 def fetch(self):
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
40 """fetch from remote source"""
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
41
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
42 fd, filename = tmpfile.mkstemp()
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
43 resource = urllib.urlopen(self.src)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
44 os.write(fd, resource.read())
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
45 os.close(fd)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
46 return filename
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
47
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
48 def unpack(self, archive):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
49 """unpack the archive to a temporary destination"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
50 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
51 # Ideally, this would be pluggable, etc
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
52 assert tarfile.is_tarfile
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
53 tarfile.TarFile.open(
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
54
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
55 def is_archive(self, path):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
56 """returns if the filesystem path is an archive"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
57 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
58 # Ideally, this would be pluggable, etc
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
59 return tarfile.is_tarfile(path)