comparison 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
comparison
equal deleted inserted replaced
57:d5e5c7496784 58:13767ee2ddf4
2 interfaces to get information from a package 2 interfaces to get information from a package
3 """ 3 """
4 4
5 import imp 5 import imp
6 import os 6 import os
7 import subprocess
7 import sys 8 import sys
8 9
9 from subprocess import check_call as call 10 from subprocess import check_call as call
10 11
11 class PackageInfo(object): 12 class PackageInfo(object):
48 """ 49 """
49 use `python setup.py egg_info` to gather package information 50 use `python setup.py egg_info` to gather package information
50 """ 51 """
51 52
52 def __call__(self): 53 def __call__(self):
53 raise NotImplementedError("TODO") 54
55 info = self.read_pkg_info(self._pkg_info())
56 # TODO: install_requires
57 return info
58
59 @classmethod
60 def read_pkg_info(cls, path):
61 """reads PKG-INFO and returns a dict"""
62
63 # read the package information
64 info_dict = {}
65 for line in file(path).readlines():
66 if not line or line[0].isspace():
67 continue # XXX neglects description
68 assert ':' in line
69 key, value = [i.strip() for i in line.split(':', 1)]
70 info_dict[key] = value
71
72 # return the information
73 return info_dict
54 74
55 def _egg_info(self): 75 def _egg_info(self):
56 """build the egg_info directory""" 76 """build the egg_info directory"""
57 77
58 if self._egg_info_path: 78 if self._egg_info_path:
59 return self._egg_info_path 79 return self._egg_info_path
60 80
61 raise NotImplementedError("TODO") 81 directory = os.path.dirname(self.setup_py)
82
83 # setup the egg info
84 try:
85 call([sys.executable, 'setup.py', 'egg_info'], cwd=directory, stdout=subprocess.PIPE)
86 except Exception:
87 print "Failure to generate egg_info: %s" % self.setup_py
88 raise
89
90 # get the .egg-info directory
91 egg_info = [i for i in os.listdir(directory)
92 if i.endswith('.egg-info')]
93 assert len(egg_info) == 1, 'Expected one .egg-info directory in %s, got: %s' % (directory, egg_info)
94 egg_info = os.path.join(directory, egg_info[0])
95 assert os.path.isdir(egg_info), "%s is not a directory" % egg_info
96
97 # cache it
98 self._egg_info_path = egg_info
99 return self._egg_info_path
100
101 def _pkg_info(self):
102 """returns path to PKG-INFO file"""
103
104 if self._pkg_info_path:
105 # return cached value
106 return self._pkg_info_path
107
108 try:
109 egg_info = self._egg_info()
110 except Exception, exception:
111 # try to get the package info from a file
112 path = os.path.dirname(self.setup_py)
113 pkg_info = os.path.join(path, 'PKG-INFO')
114 if os.path.exists(pkg_info):
115 self._pkg_info_path = pkg_info
116 return self._pkg_info_path
117 raise Exception("Cannot find or generate PKG-INFO")
118
119 pkg_info = os.path.join(egg_info, 'PKG-INFO')
120 assert os.path.exists(pkg_info)
121 self._pkg_info_path = pkg_info
122 return self._pkg_info_path
123