1#!/usr/bin/env python
2"""
3This script looks for ImportModules calls in C extensions
4of the stdlib.
5
6The current version has harcoded the location of the source
7tries on Ronald's machine, a future version will be able
8to rebuild the modulegraph source file that contains
9this information.
10"""
11
12import re
13import sys
14import os
15import pprint
16
17import_re = re.compile('PyImport_ImportModule\w+\("(\w+)"\);')
18
19def extract_implies(root):
20    modules_dir = os.path.join(root, "Modules")
21    for fn in os.listdir(modules_dir):
22        if not fn.endswith('.c'):
23            continue
24
25        module_name = fn[:-2]
26        if module_name.endswith('module'):
27            module_name = module_name[:-6]
28
29        with open(os.path.join(modules_dir, fn)) as fp:
30            data = fp.read()
31
32        imports = list(sorted(set(import_re.findall(data))))
33        if imports:
34            yield module_name, imports
35
36
37
38def main():
39    for version in ('2.6', '2.7', '3.1'):
40        print "====", version
41        pprint.pprint(list(extract_implies('/Users/ronald/Projects/python/release%s-maint'%(version.replace('.', '')))))
42
43if __name__ == "__main__":
44    main()
45