-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes.py
148 lines (136 loc) · 5.34 KB
/
includes.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
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
import os
import re
import sys
import getopt
HELP="""HELP
This program show all inclusion or modules/packages in files in a folder.
/!\ No recursion in folder is made (yet) and output is not grepable /!\
-l, --language <name> choose the language name.
Defaults to python.
-p, --path <path> the path of the folder.
Defaults to pwd.
-h, --help shows help.
--license shows the license.
"""
LICENSE_MESSAGE="""
#############################################################
# #
# This program is relased in the GNU GPL v3.0 license #
# you can modify/use this program as you wish. Please #
# link the original distribution of this software. If #
# you plan to redistribute your modified/copied copy #
# you need to relased the in GNU GPL v3.0 licence too #
# according to the overmentioned licence. #
# #
# "PROUDLY" MADE BY chkrr00k (i'm not THAT proud tbh) #
# #
#############################################################
# #
# #
# YEE #
# #
# #
#############################################################
"""
def pythonImport(f, file):
if file.endswith(".py"):
print(file)
imports = 0
for line in f:
if line.find("import") > -1:
m = re.search(r"(from (\S+) )?import (\S+)", line.strip())
if m:
if m.group(2) is None:
print("\t├─", m.group(3))
else:
print("\t├─", m.group(2) + "." + m.group(3))
imports += 1
print("\t└──[Counted " + str(imports) + " import]")
else:
print("Skipping non python file", file)
def cInclude(f, file):
if file.endswith(".c") or file.endswith(".h") or file.endswith(".cc") or file.endswith(".ccp"):
print(file)
imports = 0
for line in f:
if line.find("include") > -1:
m = re.search(r"#include [<\"](\S+)[>\"]", line.strip())
if m:
print("\t├─", m.group(1))
imports += 1
print("\t└──[Counted " + str(imports) + " import]")
else:
print("Skipping non c file", file)
def javaImport(f, file):
if file.endswith(".java"):
print(file)
imports = 0
for line in f:
if line.find("import") > -1:
m = re.search(r"import (\S+)", line.strip())
if m:
print("\t├─", m.group(1))
imports += 1
print("\t└──[Counted " + str(imports) + " import]")
else:
print("Skipping non java file", file)
def phpImport(f, file):
if file.endswith(".php"):
print(file)
imports = 0
requires = 0
for line in f:
if line.find("require") > -1:
m = re.search(r"require(_once)? (\S+)", line.strip())
if m:
print("\t├─(r)─", m.group(2))
requires += 1
elif line.find("include") > -1:
m = re.search(r"include(_once)? (\S+)", line.strip())
if m:
print("\t├─(i)─", m.group(2))
imports += 1
print("\t├──[Counted " + str(requires) + " require]")
print("\t└──[Counted " + str(imports) + " import]")
else:
print("Skipping non php file", file)
def error(msg):
print("ERROR")
print(msg)
sys.exit(1)
def main():
languages = {"python" : pythonImport,
"c" : cInclude,
"java" : javaImport,
"php" : phpImport}
path = os.getcwd()
current = "python"
try:
opts, args = getopt.getopt(sys.argv[1:], "hp:l:y", ["help", "path=", "language=", "license"])
except:
error("Invalid arguments")
for o, a in opts:
if o in ("-h", "--help"):
print(HELP)
sys.exit(0)
elif o in ("-l", "--language"):
if a in languages:
current = a
else:
error("Invalid arguments: language not valid")
elif o in ("-p", "--path"):
path = a
elif o in ("--license"):
print(LICENSE_MESSAGE)
sys.exit(0)
try:
for file in os.listdir(path):
if os.path.isdir(path + "\\" + file):
print("Skipping dir", file)
else:
with open(path + "\\" + file) as f:
languages[current](f, file)
except:
error("Invalid folder: " + path)
if __name__ == "__main__":
main()