Mercurial > hg > PaInt
annotate paint/info.py @ 58:13767ee2ddf4
start adding an egg_info based information thingy
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 23 Jan 2013 16:59:36 -0800 |
parents | d5e5c7496784 |
children | acee5e882768 |
rev | line source |
---|---|
54
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 interfaces to get information from a package |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 |
57 | 5 import imp |
54
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 import os |
58
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
7 import subprocess |
54
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import sys |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
57 | 10 from subprocess import check_call as call |
11 | |
54
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 class PackageInfo(object): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 """abstract base class of package info""" |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def __init__(self, path): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 - path : path to setup.py or its directory |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 if os.path.isdir(path): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 path = os.path.join(path, 'setup.py') |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 assert os.path.exists(path), "'%s' not found" % path |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 self.setup_py = os.path.abspath(path) |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 def __call__(self): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 """returns dictionary of package info""" |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 raise NotImplementedError("abstract base class") |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 class SetupOverridePackageInfo(PackageInfo): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 gather setup.py information by overriding the function |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 """ |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 def __call__(self): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 setuptools = sys.modules.get('setuptools') |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 sys.modules['setuptools'] = sys.modules[__name__] |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 globals()['setup'] = self._setup |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 try: |
57 | 37 module = imp.load_source('setup', self.setup_py) |
56 | 38 finally: |
39 sys.modules.pop('setuptools') | |
40 if setuptools: | |
41 sys.modules['setuptools'] = setuptools | |
42 globals().pop('setup') | |
54
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 return self.__dict__.pop('_info') |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 def _setup(self, **kwargs): |
a8236b97abd3
separate methods for gathering package information
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 self._info = kwargs |
57 | 47 |
48 class EggInfo(PackageInfo): | |
49 """ | |
50 use `python setup.py egg_info` to gather package information | |
51 """ | |
52 | |
53 def __call__(self): | |
58
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
54 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
55 info = self.read_pkg_info(self._pkg_info()) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
56 # TODO: install_requires |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
57 return info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
58 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
59 @classmethod |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
60 def read_pkg_info(cls, path): |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
61 """reads PKG-INFO and returns a dict""" |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
62 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
63 # read the package information |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
64 info_dict = {} |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
65 for line in file(path).readlines(): |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
66 if not line or line[0].isspace(): |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
67 continue # XXX neglects description |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
68 assert ':' in line |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
69 key, value = [i.strip() for i in line.split(':', 1)] |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
70 info_dict[key] = value |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
71 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
72 # return the information |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
73 return info_dict |
57 | 74 |
75 def _egg_info(self): | |
76 """build the egg_info directory""" | |
77 | |
78 if self._egg_info_path: | |
79 return self._egg_info_path | |
80 | |
58
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
81 directory = os.path.dirname(self.setup_py) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
82 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
83 # setup the egg info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
84 try: |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
85 call([sys.executable, 'setup.py', 'egg_info'], cwd=directory, stdout=subprocess.PIPE) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
86 except Exception: |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
87 print "Failure to generate egg_info: %s" % self.setup_py |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
88 raise |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
89 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
90 # get the .egg-info directory |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
91 egg_info = [i for i in os.listdir(directory) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
92 if i.endswith('.egg-info')] |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
93 assert len(egg_info) == 1, 'Expected one .egg-info directory in %s, got: %s' % (directory, egg_info) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
94 egg_info = os.path.join(directory, egg_info[0]) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
95 assert os.path.isdir(egg_info), "%s is not a directory" % egg_info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
96 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
97 # cache it |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
98 self._egg_info_path = egg_info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
99 return self._egg_info_path |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
100 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
101 def _pkg_info(self): |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
102 """returns path to PKG-INFO file""" |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
103 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
104 if self._pkg_info_path: |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
105 # return cached value |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
106 return self._pkg_info_path |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
107 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
108 try: |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
109 egg_info = self._egg_info() |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
110 except Exception, exception: |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
111 # try to get the package info from a file |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
112 path = os.path.dirname(self.setup_py) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
113 pkg_info = os.path.join(path, 'PKG-INFO') |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
114 if os.path.exists(pkg_info): |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
115 self._pkg_info_path = pkg_info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
116 return self._pkg_info_path |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
117 raise Exception("Cannot find or generate PKG-INFO") |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
118 |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
119 pkg_info = os.path.join(egg_info, 'PKG-INFO') |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
120 assert os.path.exists(pkg_info) |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
121 self._pkg_info_path = pkg_info |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
122 return self._pkg_info_path |
13767ee2ddf4
start adding an egg_info based information thingy
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
123 |