aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/third_party/modulegraph/scripts/extract_implies.py
blob: d6ab353614197508f8dbb52dd8ffbd3a075d2b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
"""
This script looks for ImportModules calls in C extensions
of the stdlib.

The current version has harcoded the location of the source
tries on Ronald's machine, a future version will be able
to rebuild the modulegraph source file that contains
this information.
"""

import re
import sys
import os
import pprint

import_re = re.compile('PyImport_ImportModule\w+\("(\w+)"\);')

def extract_implies(root):
    modules_dir = os.path.join(root, "Modules")
    for fn in os.listdir(modules_dir):
        if not fn.endswith('.c'):
            continue

        module_name = fn[:-2]
        if module_name.endswith('module'):
            module_name = module_name[:-6]

        with open(os.path.join(modules_dir, fn)) as fp:
            data = fp.read()

        imports = list(sorted(set(import_re.findall(data))))
        if imports:
            yield module_name, imports



def main():
    for version in ('2.6', '2.7', '3.1'):
        print "====", version
        pprint.pprint(list(extract_implies('/Users/ronald/Projects/python/release%s-maint'%(version.replace('.', '')))))

if __name__ == "__main__":
    main()