annotate paint/package.py @ 32:f2e31ac03bb3

flushing this out
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 30 Mar 2012 13:40:32 -0700
parents 5fb1844db0b2
children 5efb61fde8aa
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
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
6 import pip
29
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
7 import pypi
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
8 import shutil
19
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
9 import subprocess
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
10 import sys
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
11 import tarfile
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
12 import tempfile
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
13 import urllib2
21
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
14 import urlparse
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
15 import utils
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
16
19
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
17 try:
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
18 from subprocess import check_call as call
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
19 except ImportError:
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
20 from subporcess import call
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
21
10
d63ae03cc300 * fix up __init__ imports
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
22 __all__ = ['Package']
d63ae03cc300 * fix up __init__ imports
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
23
4
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 class Package(object):
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 def __init__(self, src):
e230ac76aaa4 stub package model
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.src = src
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
28
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
29 # ephemeral data
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
30 self._tmppath = None
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
31 self._egg_info_path = None
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
32
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
33 # TODO: list of temporary files/directories to be deleted
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
34
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
35 def path(self):
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
36 """filesystem path to package directory"""
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
37
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
38 # return cached copy if it exists
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
39 if self._tmppath:
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
40 return self._tmppath
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 # fetch from the web if a URL
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
43 tmpfile = None
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
44 src = self.src
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
45 if utils.isURL(self.src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
46 tmpfile = src = self.fetch()
7
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 # unpack if an archive
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
49 if self._is_archive(src):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
50 try:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
51 self.unpack(src)
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
52 finally:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
53 if tmpfile:
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
54 os.remove(tmpfile)
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
55 return self._tmppath
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
56
5
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
57 return self.src
5b74792d46e4 stub package model;
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
58
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
59 def fetch(self, filename=None):
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
60 """fetch from remote source to a temporary file"""
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
61 if filename is None:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
62 fd, filename = tempfile.mkstemp()
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
63 os.close(fd)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
64 fp = file(filename, 'w')
15
8c8b7482772f fix a few failures but still failing
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
65 resource = urllib2.urlopen(self.src)
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
66 fp.write(resource.read())
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
67 fp.close()
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
68 return filename
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
69
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
70 def unpack(self, archive):
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
71 """unpack the archive to a temporary destination"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
72 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
73 # Ideally, this would be pluggable, etc
8
c1181e9c9ca2 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
74 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
75 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
76 self._tmppath = tempfile.mkdtemp()
19
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
77 members = tf.getmembers()
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
78
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
79 # cut off the top level directory
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
80 assert not [i for i in members if not os.path.sep in i.name]
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
81 tld = set()
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
82 for member in members:
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
83 directory, member.name = member.name.split(os.path.sep, 1)
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
84 tld.add(directory)
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
85 assert len(tld) == 1
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
86
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
87 # extract
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
88 for member in members:
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
89 tf.extract(member, path=self._tmppath)
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
90 tf.close()
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
91
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
92 def _is_archive(self, path):
7
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
93 """returns if the filesystem path is an archive"""
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
94 # TODO: should handle zipfile additionally at least
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
95 # Ideally, this would be pluggable, etc
a7bf894c96c7 yet more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
96 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
97
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
98 def _cleanup(self):
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
99 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
100 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
101 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
102
18
4c4d1e194bf7 leave in debugging code for now
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
103 # __del__ = cleanup
11
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
104
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
105 ### python-package-specific functionality
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
106
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
107 def _egg_info(self):
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
108 """build the egg_info directory"""
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
109
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
110 if self._egg_info_path:
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
111 # return cached copy
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
112 return self._egg_info_path
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
113
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
114 directory = self.path()
16
e6cdc58fc4f5 more helpful debugging
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
115 setup_py = os.path.join(directory, 'setup.py')
e6cdc58fc4f5 more helpful debugging
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
116 if not os.path.exists(setup_py):
17
e74fc40e1f64 interpolate string
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
117 raise AssertionError("%s does not exist" % setup_py)
11
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
118
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
119 # setup the egg info
19
c54db80d6e7f fix test
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
120 call([sys.executable, 'setup.py', 'egg_info'], cwd=directory, stdout=subprocess.PIPE)
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
121
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
122 # get the .egg-info directory
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
123 egg_info = [i for i in os.listdir(directory)
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
124 if i.endswith('.egg-info')]
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
125 assert len(egg_info) == 1, 'Expected one .egg-info directory in %s, got: %s' % (directory, egg_info)
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
126 egg_info = os.path.join(directory, egg_info[0])
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
127 assert os.path.isdir(egg_info), "%s is not a directory" % egg_info
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
128
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
129 # cache it
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
130 self._egg_info_path = egg_info
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
131 return self._egg_info_path
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
132
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
133 def info(self):
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
134 """return info dictionary for package"""
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
135 # could use pkginfo
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
136
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
137 egg_info = self._egg_info()
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
138
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
139 # read the package information
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
140 pkg_info = os.path.join(egg_info, 'PKG-INFO')
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
141 info_dict = {}
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
142 for line in file(pkg_info).readlines():
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
143 if not line or line[0].isspace():
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
144 continue # XXX neglects description
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
145 assert ':' in line
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
146 key, value = [i.strip() for i in line.split(':', 1)]
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
147 info_dict[key] = value
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
148
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
149 # return the information
20
11bc01a089e7 return only the dictionary
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
150 return info_dict
13
0dd1f8f83be2 moving towards some sort of organization
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
151
11
c41a946d34af stub out info method
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
152 def dependencies(self):
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
153 """return the dependencies"""
32
f2e31ac03bb3 flushing this out
Jeff Hammel <jhammel@mozilla.com>
parents: 31
diff changeset
154 # TODO: should probably have a more detailed dict:
f2e31ac03bb3 flushing this out
Jeff Hammel <jhammel@mozilla.com>
parents: 31
diff changeset
155 # {'mozinfo': {'version': '>= 0.2',
f2e31ac03bb3 flushing this out
Jeff Hammel <jhammel@mozilla.com>
parents: 31
diff changeset
156 # 'url': 'http://something.com/'}}
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
157
21
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
158 # get the egg_info directory
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
159 egg_info = self._egg_info()
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
160
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
161 # read the dependencies
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
162 requires = os.path.join(egg_info, 'requires.txt')
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
163 if os.path.exists(requires):
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
164 dependencies = [i.strip() for i in file(requires).readlines() if i.strip()]
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
165 else:
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
166 dependencies = []
21
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
167 dependencies = dict([(i, None) for i in dependencies])
14
6d27c2136163 a decent structure; need to reuse more
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
168
21
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
169 # read the dependency links
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
170 dependency_links = os.path.join(egg_info, 'dependency_links.txt')
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
171 if os.path.exists(dependency_links):
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
172 links = [i.strip() for i in file(dependency_links).readlines() if i.strip()]
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
173 for link in links:
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
174 # XXX pretty ghetto
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
175 assert '#egg=' in link
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
176 url, dep = link.split('#egg=', 1)
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
177 if dep in dependencies:
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
178 dependencies[dep] = link
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
179
4df3e715817d now have passing tests again
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
180 return dependencies
24
54c1e7e43a54 stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
181
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
182 def extension(self):
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
183 """filename extension"""
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
184
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
185 def repackage(self):
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
186 """
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
187 repackage the package to ensure its actually in the right form
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
188 """
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
189
24
54c1e7e43a54 stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
190 def download(self, directory):
54c1e7e43a54 stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
191 """download a package and all its dependencies using pip"""
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
192 if not os.path.exists(directory):
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
193 os.makedirs(directory)
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
194 assert os.path.isdir(directory)
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
195 pip.main(['install', '--download', directory, self.src])
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
196
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
197 def pypi(self, directory):
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
198 """
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
199 download packages for a pypi directory structure
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
200 http://k0s.org/portfolio/pypi.html
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
201 """
29
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
202 if not os.path.exists(directory):
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
203 os.makedirs(directory)
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
204 assert os.path.isdir(directory)
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
205 tempdir = tempfile.mkdtemp()
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
206 try:
29
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
207 self.download(tempdir)
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
208 for package in os.listdir(tempdir):
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
209
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
210 # full path
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
211 src = os.path.join(tempdir, package)
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
212
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
213 package = Package(src)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
214
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
215 # # get destination dirname, filename
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
216 # try:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
217 # dirname, filename = pypi.pypi_path(src)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
218 # except ValueError:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
219 # # PKG-INFO not found
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
220 # pass # TODO
29
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
221
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
222 # # make the directory if it doesn't exist
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
223 # subdir = os.path.join(directory, dirname)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
224 # if not os.path.exists(subdir):
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
225 # os.makedirs(subdir)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
226 # assert os.path.isdir(subdir)
29
59524b6d25c0 implementing making pypi directory strcuture from a package
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
227
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
228 # # move the file
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
229 # shutil.move(src, os.path.join(subdir, filename))
28
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
230 finally:
c44fb4b6918b CLI sorta works
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
231 shutil.rmtree(tempdir)
31
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
232
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
233 def pypi_path(self, path):
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
234 """
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
235 returns subpath 2-tuple appropriate for pypi path structure:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
236 http://k0s.org/portfolio/pypi.html
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
237 """
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
238 info = self.info()
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
239 # sdist = pkginfo.sdist.SDist(path)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
240
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
241 # determine the extension (XXX hacky)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
242 extensions = ('.tar.gz', '.zip', '.tar.bz2')
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
243 for ext in extensions:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
244 import pdb; pdb.set_trace()
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
245 if sdist.filename.endswith(ext):
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
246 break
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
247 else:
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
248 raise Exception("Extension %s not found: %s" % (extensions, sdist.filename))
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
249
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
250 # get the filename destination
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
251 filename = '%s-%s%s' % (info['name'], ext)
5fb1844db0b2 back to the drawring broad
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
252 return sdist.name, filename