annotate paint/package.py @ 11:c41a946d34af

stub out info method
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 24 Feb 2012 15:05:08 -0800
parents d63ae03cc300
children ceb8c69a16f3
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
9
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
6 import shutil
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
7 import tarfile
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
8 import tempfile
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
9 import urllib2
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
10 import utils
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
11
10
d63ae03cc300 * fix up __init__ imports
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
12 __all__ = ['Package']
d63ae03cc300 * fix up __init__ imports
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
13
4
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 class Package(object):
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
15 # 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
16
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def __init__(self, src):
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 self.src = src
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
19 self._tmppath = None
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
20
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
21 def path(self):
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
22 """filesystem path to package"""
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
23
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
24 # return cached copy if it exists
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
25 if self._tmppath:
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
26 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
27
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
28 # fetch from the web if a URL
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
29 tmpfile = None
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
30 src = self.src
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
31 if utils.isURL(self.src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
32 tmpfile = src = self.fetch()
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
33
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
34 # unpack if an archive
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
35 if self.is_archive(src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
36 try:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
37 self.unpack(src)
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
38 finally:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
39 if tmpfile:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
40 os.remove(tmpfile)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
41 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
42
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
43 return self.src
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
44
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
45 def fetch(self):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
46 """fetch from remote source to a temporary file"""
11
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
47 fd, filename = tempfile.mkstemp()
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
48 resource = urllib2.urlopen(self.src)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
49 os.write(fd, resource.read())
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
50 os.close(fd)
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
51 return filename
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
52
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
53 def unpack(self, archive):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
54 """unpack the archive to a temporary destination"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
55 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
56 # Ideally, this would be pluggable, etc
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
57 assert tarfile.is_tarfile(archive), "%s is not an archive" % self.src
9
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
58 tf = tarfile.TarFile.open(archive)
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
59 self._tmppath = tempfile.mkdtemp()
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
60 tf.extractall(path=self._tmppath)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
61
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
62 def is_archive(self, path):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
63 """returns if the filesystem path is an archive"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
64 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
65 # Ideally, this would be pluggable, etc
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
66 return tarfile.is_tarfile(path)
9
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
67
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
68 def cleanup(self):
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
69 if self._tmppath:
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
70 shutil.rmtree(self._tmppath)
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
71 self._tmppath = None
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
72
562230cc2e86 now the package class should at least extracts things. it doesnt actually do anything useful
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
73 __del__ = cleanup
11
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
74
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
75 def info(self):
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
76 path = self.path()
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
77 assert os.path.exists(os.path.join(path, 'setup.py'))
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
78
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
79 def dependencies(self):
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
80 path = self.path()