3 import gzip, os.path, re
5 from os import environ, popen, walk
6 from optparse import OptionParser
9 from xml.sax import ContentHandler, make_parser
10 from xml.sax.handler import feature_external_ges
12 default_books = ['atk', 'gdk', 'gdk-pixbuf', 'glib', 'gio', 'gobject', 'gtk', 'pango']
14 __comment_regex = re.compile(r'/\*.*?\*/', re.DOTALL)
15 __word_regex = re.compile(r'\b[A-Za-z_][A-Za-z0-9_]*\b')
18 def __init__(self, name, folders, version=None):
24 self.__version = version
28 lambda n: os.path.join(f, name, n % name),
29 ['%s.devhelp2', '%s.devhelp2.gz'])
31 lambda n: (os.path.getmtime(n), n),
32 filter(os.path.isfile, catalogs))
37 self.__catalog = catalogs[-1][1]
40 if not self.__catalog:
41 raise IOError, 'No devhelp book found for "%s"' % name
43 def __cmp__(self, other):
44 if isinstance(other, Book):
45 return cmp(self.name, other.name)
50 return '<Book name="%s">' % self.__name
53 timestamp = os.path.getmtime(self.__catalog)
55 if not self.__symbols or timestamp > self.__timestamp:
56 class DevhelpContentHandler(ContentHandler):
57 def __init__(self, book, symbols):
59 self.__symbols = symbols
61 def startElement(self, name, attrs):
63 self.title = attrs.get('title')
67 symbol = Symbol.from_xml(self.__book, attrs)
68 if symbol: self.__symbols[symbol.name] = symbol
71 self.__symbols, self.__timestamp = dict(), timestamp
72 handler = DevhelpContentHandler(self, self.__symbols)
74 parser = make_parser()
75 parser.setFeature(feature_external_ges, False)
76 parser.setContentHandler(handler)
78 if self.__catalog.endswith('.gz'):
79 parser.parse(gzip.open(self.__catalog))
82 parser.parse(open(self.__catalog))
84 self.__title = handler.title
86 def _get_symbols(self):
87 self.parse(); return self.__symbols
89 self.parse(); return self.__title
91 def find_requirements(self):
94 for symbol in self.symbols.values():
95 if not symbol.matches:
98 if symbol.since and symbol.since > self.version:
99 symbol_list = requirements.get(symbol.since, [])
100 requirements[symbol.since] = symbol_list
101 symbol_list.append(symbol)
105 catalog = property(lambda self: self.__catalog)
106 name = property(lambda self: self.__name)
107 version = property(lambda self: self.__version)
109 symbols = property(_get_symbols)
110 title = property(_get_title)
112 class Symbol(object):
113 known_attributes = ('name', 'type', 'link', 'deprecated', 'since')
115 class DeprecationInfo(object):
116 def __init__(self, text):
118 pair = text.split(':', 1)
120 self.__version = Symbol.VersionInfo(pair[0])
121 self.__details = pair[1].strip()
124 self.__version = None
125 self.__details = text.strip()
127 def __cmp__(self, other):
128 if isinstance(other, Symbol.DeprecationInfo):
129 return cmp(self.version, other.version)
131 if isinstance(other, Symbol.VersionInfo):
132 return cmp(self.version, other)
137 if not self.__version:
138 return self.__details and str(self.__details) or 'Deprecated'
141 return 'Since %s: %s' % (self.__version, self.__details)
143 return 'Since %s' % self.__version
145 details = property(lambda self: self.__details)
146 version = property(lambda self: self.__version)
148 class VersionInfo(object):
149 def __init__(self, text):
150 match = re.match(r'^\w*\s*((?:\d+\.)*\d+)', text)
152 self.__numbers = map(int, match.group(1).split('.'))
153 self.__hash = reduce(lambda x, y: x * 1000 + y, reversed(self.__numbers))
154 self.__text = text.strip()
156 def __get_number(self, index):
157 if len(self.__numbers) > index:
158 return self.__numbers[index]
162 def __cmp__(self, other):
163 if isinstance(other, Symbol.VersionInfo):
164 return cmp(self.numbers, other.numbers)
172 return '.'.join(map(str, self.__numbers))
174 major = property(lambda self: self.__get_number(0))
175 minor = property(lambda self: self.__get_number(1))
176 patch = property(lambda self: self.__get_number(2))
177 numbers = property(lambda self: self.__numbers)
178 text = property(lambda self: self.__text)
181 def from_xml(cls, book, attrs):
182 name, type, link, deprecated, since = map(attrs.get, Symbol.known_attributes)
186 if name.endswith('()'):
187 if not type in ('function', 'macro'):
188 type = (name[0].islower() and 'function' or 'macro')
190 name = name[:-2].strip()
192 words = name.split(' ')
195 if words[0] in ('enum', 'struct', 'union'):
196 if not type: type = words[0]
197 name = name[len(words[0]):].strip()
198 elif 'property' == words[-1]:
199 assert('The' == words[0])
200 owner = link.split('#', 1)[1].split('-', 1)[0]
201 type, name = 'property', '%s::%s' % (owner, name.split('"')[1])
202 elif 'signal' == words[-1]:
203 assert('The' == words[0])
204 owner = link.split('#', 1)[1].split('-', 1)[0]
205 type, name = 'signal', '%s:%s' % (owner, name.split('"')[1])
207 if not type: return None
209 if None != deprecated: deprecated = Symbol.DeprecationInfo(deprecated)
210 if since: since = Symbol.VersionInfo(since)
214 'WARNING: Malformed symbol name: "%s" (type=%s) in %s.' % (
215 name, type, book.name))
217 return Symbol(book, name, type, link, deprecated, since)
219 def __init__(self, book, name, type, link=None, deprecated=None, since=None):
224 self.__deprecated = deprecated
230 '<Symbol: %s, type=%s, since=%s, deprecated=%s>' % (
231 self.name, self.type, self.since, self.deprecated))
234 book = property(lambda self: self.__book)
235 name = property(lambda self: self.__name)
236 type = property(lambda self: self.__type)
237 link = property(lambda self: self.__link)
238 deprecated = property(lambda self: self.__deprecated)
239 matches = property(lambda self: self.__matches)
240 since = property(lambda self: self.__since)
243 options = OptionParser(version="@VERSION@")
245 options.add_option('-b', '--book', dest='books',
246 help='name of a devhelp book to consider',
247 default=[], action='append')
248 options.add_option('-d', '--html-dir', metavar='PATH', dest='dirs',
249 help='path of additional folders with devhelp books',
250 default=[], action='append')
251 options.add_option('-u', '--list-unknown', action='store_true', default=False,
252 help='list symbols not found in any book', dest='unknown')
253 options.add_option('-v', '--verbose', action='store_true', default=False,
254 help='print additional information')
256 return options.parse_args()
258 def merge_gnome_path(options):
259 path = environ.get('GNOME2_PATH')
260 path = path and path.split(':') or []
263 'pkg-config --variable=prefix glib-2.0'
264 ).readline().rstrip()
266 path.insert(0, prefix)
267 path = filter(None, [p.strip() for p in path])
270 os.path.join(p, 'share', 'devhelp', 'books'),
271 os.path.join(p, 'share', 'gtk-doc', 'html')]
274 path = reduce(list.__add__, path)
275 path = filter(os.path.isdir, path)
279 if '__main__' == __name__:
280 options, args = parse_cmdline()
282 merge_gnome_path(options)
284 if not options.books:
285 options.books = default_books
287 def trace(message, *args):
288 if options.verbose: print message % args
290 def parse_book(name):
292 match = re.match(r'^(.*?)(?::(\d+(?:\.\d+)*))?$', name)
293 name, version = match.groups()
294 trace('reading book: %s', name)
296 version = version and Symbol.VersionInfo(version)
297 return name, Book(name, options.dirs, version)
300 print >>stderr, 'WARNING: %s.' % e
302 def scan_source_file(name):
306 contents = __comment_regex.sub('', file(name).read())
312 trace('scanning: %s', name)
313 lines = contents.split('\n')
315 for lineno in range(len(lines)):
316 for word in __word_regex.findall(lines[lineno]):
317 symbol = symbols.get(word)
320 symbol.matches.append((name, lineno, symbol))
322 elif options.unknown and word.find('_') > 0:
323 unknown_symbols.append((name, lineno, word))
326 matches, symbols = dict(), dict()
327 books = dict(filter(None, map(parse_book, set(options.books))))
329 for book in books.values():
330 symbols.update(book.symbols)
333 if os.path.isdir(name):
334 for path, dirs, files in walk(name):
337 scan_source_file(os.path.join(path, f))
340 scan_source_file(name)
344 for book in books.values():
345 requirements = book.find_requirements().items()
349 for symbol in requirements[-1][1]:
350 matches += symbol.matches
353 matches += unknown_symbols
357 for filename, lineno, symbol in matches:
358 if isinstance(symbol, Symbol):
359 args = filename, lineno, symbol.book.name, symbol.since, symbol.name
360 print '%s:%d: %s-%s required for %s' % args
362 elif options.verbose:
363 print '%s:%d: unknown symbol %s' % (filename, lineno, symbol)
366 unknown = [m[2].split('_')[0].lower() for m in unknown_symbols]
367 unknown = list(set(unknown))
370 print 'unknown prefixes: %s' % ', '.join(unknown)
372 raise SystemExit(matches and 1 or 0)