-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_mapper.py
65 lines (44 loc) · 1.64 KB
/
mod_mapper.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import re
def get_files(dir_path, specifics=None):
# names in specifics will be included, additionally
if specifics is None:
specifics = set()
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith(".py") or file in specifics:
yield os.path.join(root, file)
def lines_in_file(file_path):
with open(file_path) as file:
return file.readlines()
def words_in_line(line):
return re.findall(r"[\w.]+|#", line) # XXX: note that ';' is not treated
def contains_import(words):
return ("import" in words)
def cut_comment(words):
return words[:(None if "#" not in words else words.index("#"))]
def cut_as(words):
return words[:(None if "as" not in words else words.index("as"))]
def gen_import(words):
if "import" in words:
for i in words[words.index("import") + 1:]:
yield i
def parser(words):
if "from" in words:
return [[words[1], words[3:]]]
else:
return [[i, []] for i in gen_import(words)]
def find_dependencies(dir_path, specifics=None):
return {
f.replace(dir_path, ""):
reduce(list.__add__,
map(parser,
filter(contains_import,
(cut_as(cut_comment(words_in_line(line)))
for line in lines_in_file(f)))), [])
for f in get_files(dir_path, specifics)}
if __name__ == "__main__":
dir_path = r"/home/mda5232/git/fitpack"
deps = find_dependencies(dir_path, {"jam3d", "mcproc", "run_notebook"})