-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
174 lines (155 loc) · 5.28 KB
/
project.py
File metadata and controls
174 lines (155 loc) · 5.28 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from datetime import datetime, timezone
from common import DepType
from monty import (
LANG,
PARSER_NAME,
TYPE_NORMAL,
VESION,
)
from protocol.map import Source
from sourcecode import Dep, Dir, File
class Project:
"""Class representing a project"""
name: str
path: str
abs_path: str
excludes: set
excludes_dot: bool
includes: set
includes_file_types: set
directory: str
dirs: dict[str, Dir]
files: dict[str, File]
deps: dict[str, Dep]
def __init__(
self, name: str, path: str, directory: str, excludes: str, file_types: str
):
self.name = name
self.path = path
self.abs_path = os.path.abspath(path)
self.directory = directory
self.dirs = {}
self.files = {}
self.deps = {}
self.excludes = set()
self.excludes_dot = False
self.includes = set()
self.includes_file_types = set()
self.includes_file_types.add("py")
self.build_excludes(excludes)
self.parse_file_types(file_types)
def build_excludes(self, excls: str):
excludes_list = excls.split(",")
for excl in excludes_list:
excl_normalized = excl.strip()
if excl_normalized == ".*":
self.excludes_dot = True
else:
excl_absp = os.path.join(self.abs_path, excl_normalized)
self.excludes.add(excl_absp)
def parse_file_types(self, file_types: str):
file_types_list = file_types.split(",")
for typ in file_types_list:
if typ.startswith("*."):
self.includes_file_types.add(typ[2:])
else:
self.includes.add(typ)
def scan(self):
os.chdir(self.path)
if os.path.isdir(self.directory):
root = Dir(self.directory)
self.dirs[self.directory] = root
self.deps[self.directory] = Dep(root, DepType.PKG)
self.deps[""] = Dep(root, DepType.PKG)
self.traverse(self.directory)
def traverse(self, dir: str):
entries = os.listdir(dir)
for entry in entries:
if self.excludes_dot and entry.startswith("."):
continue
current_entry = os.path.join(dir, entry)
absp = os.path.abspath(current_entry)
if absp in self.excludes:
continue
if current_entry.startswith("./"):
current_entry = current_entry[2:]
isDir = os.path.isdir(current_entry)
if isDir:
pkg = Dir(current_entry)
self.dirs[current_entry] = pkg
self.deps[current_entry] = Dep(pkg, DepType.PKG)
self.traverse(current_entry)
else:
should_parse = False
if entry in self.includes:
should_parse = True
if not should_parse:
file_segements = entry.split(".")
if len(file_segements) > 1:
if file_segements[-1] in self.includes_file_types:
should_parse = True
if not should_parse:
continue
d = self.lookup(dir, 1)
assert d != None and d.typ == DepType.PKG
file = File(current_entry, d.pkg_ptr)
self.deps[current_entry] = Dep(file, DepType.FILE)
self.files[current_entry] = file
def parse_files(self):
for f in self.files.values():
if f.source:
f.parse(self.lookup)
def build_dependencies(self):
for f in self.files.values():
if f.source:
f.connect()
for f in self.files.values():
if f.source:
f.liftup()
def complete_fields(self):
pass
# 0: all, 1: pkg, 2: file
def lookup(self, name: str, typ: int) -> Dep | None:
if typ == 2:
return self.deps.get(name + ".py")
if typ == 1:
return self.deps.get(name)
else:
r = self.deps.get(name)
if r != None:
return r
return self.deps.get(name + ".py")
def dump(self) -> Source:
now = datetime.now(timezone.utc).astimezone()
result = {
"name": self.name,
"lang": LANG,
"parser": "%s %s" % (PARSER_NAME, VESION),
"typ": TYPE_NORMAL,
"timestamp": now.isoformat(),
"repository": "",
"version": "",
"pkgs": [],
"files": [],
"absts": [],
"fns": [],
"calls": [],
"deps": [],
"refs": [],
}
for p in self.dirs.values():
result["pkgs"].append(p.dump())
for f in self.files.values():
result["files"].append(f.dump())
for d in self.deps.values():
if d.typ == DepType.PKG:
continue
result["deps"].append(d.dump())
for f in self.files.values():
if f.source:
for fn in f.parser.fns.values():
result["fns"].append(fn.dump())
for abst in f.parser.absts.values():
result["absts"].append(abst.dump())
return result