-
Notifications
You must be signed in to change notification settings - Fork 13
/
pycerfl.py
232 lines (199 loc) · 6.8 KB
/
pycerfl.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
Main Program
"""
import ast
import os
import sys
import shlex
import subprocess
import requests
from ClassIterTree import IterTree
from getjson import read_Json
from getcsv import read_FileCsv
# Create lists of each attribute
Literals = ['ast.List', 'ast.Tuple', 'ast.Dict']
Variables = ['ast.Name']
Expressions = ['ast.Call', 'ast.IfExp', 'ast.Attribute']
Comprehensions = ['ast.ListComp', 'ast.GeneratorExp', 'ast.DictComp']
Statements = ['ast.Assign', 'ast.AugAssign', 'ast.Raise', 'ast.Assert',
'ast.Pass']
Imports = ['ast.Import', 'ast.ImportFrom']
ControlFlow = ['ast.If', 'ast.For', 'ast.While', 'ast.Break', 'ast.Continue',
'ast.Try', 'ast.With']
FunctionsClass = ['ast.FunctionDef', 'ast.Lambda', 'ast.Return', 'ast.Yield',
'ast.ClassDef']
# Create list of attribute lists
SetClass = [Literals, Variables, Expressions, Comprehensions, Statements,
Imports, ControlFlow, FunctionsClass]
def choose_option():
""" Choose option. """
if type_option == 'directory':
repo = option.split('/')[-1]
read_Directory(option, repo)
elif type_option == 'repo-url':
request_url()
elif type_option == 'user':
run_user()
else:
sys.exit('Incorrect Option')
def request_url():
""" Request url by shell. """
values = option.split("/")
try:
protocol = values[0].split(':')[0]
type_git = values[2]
user = values[3]
repo = values[4][0:-4]
except:
sys.exit('ERROR --> Usage: http://TYPEGIT/USER/NAMEREPO.git')
# Check url
check_url(protocol, type_git)
# Check languaje
check_lenguage(option, protocol, type_git, user, repo)
def check_url(protocol, type_git):
""" Check url sintax. """
if protocol != 'https':
sys.exit('Usage: https protocol')
elif type_git != 'github.com':
sys.exit('Usage: github.com')
def check_lenguage(url, protocol, type_git, user, repo):
""" Check lenguaje python. """
total_elem = 0
python_leng = False
python_quantity = 0
# Create the url of the api
repo_url = (protocol + "://api." + type_git + "/repos/" + user + "/" +
repo + "/languages")
print("Analyzing repository languages...\n")
# Get content
r = requests.get(repo_url)
# Decode JSON response into a Python dict:
content = r.json()
# Get used languages and their quantity
for key in content.keys():
print(key + ": " + str(content[key]))
if key == 'Python':
python_leng = True
python_quantity = content[key]
total_elem += content[key]
# Check if python is 50%
if python_leng:
amount = total_elem/2
if python_quantity >= amount:
print('\nPython 50% OK\n')
# Clone the repository
run_url(url)
else:
print('\nThe repository does not contain 50% of the Python.\n')
def run_url(url):
""" Run url. """
command_line = "git clone " + url
print('Run url...')
# print(command_line)
# List everything and separate
args = shlex.split(command_line)
# Run in the shell the command_line
subprocess.call(args)
get_directory(url)
def run_user():
""" Run user. """
# Create the url of the api
user_url = ("https://api.github.com/users/" + option)
print(user_url)
print("Analyzing user...\n")
try:
# Extract headers
headers = requests.get(user_url)
# Decode JSON response into a Python dict:
content = headers.json()
# Get repository url
repo_url = content["repos_url"]
except KeyError:
sys.exit('An unavailable user has been entered')
print("Analyzing repositories...\n")
# Extract repository names
names = requests.get(repo_url)
# Decode JSON response into a Python dict:
content = names.json()
# Show repository names
for repository in content:
print('\nRepository: ' + str(repository["name"]))
url = ("https://github.com/" + option + "/" + repository["name"])
check_lenguage(url, 'https', 'github.com', option, repository["name"])
def get_directory(url):
""" Get the name of the downloaded repository directory. """
# Get values rom the url
values = url.split('/')
# Last item in the list
name_directory = values[-1]
# Remove extension .git
if '.git' in str(name_directory):
name_directory = name_directory[0:-4]
print("The directory is: " + name_directory)
get_path(name_directory)
def get_path(name_directory):
""" Get the path to the directory. """
absFilePath = os.path.abspath(name_directory)
# Check if the last element is a file.py
fichero = absFilePath.split('/')[-1]
if fichero.endswith('.py'):
absFilePath = absFilePath.replace("/" + fichero, "")
print("This script absolute path is ", absFilePath)
read_Directory(absFilePath, name_directory)
def read_Directory(absFilePath, repo):
""" Extract the .py files from the directory. """
pos = ''
print('Directory: ')
path = absFilePath
try:
directory = os.listdir(path)
print(directory)
for i in range(0, len(directory)):
if directory[i].endswith('.py'):
print('Python File: ' + str(directory[i]))
pos = path + "/" + directory[i]
read_File(pos, repo)
elif '.' not in directory[i]:
print('\nOpening another directory...\n')
path2 = absFilePath + '/' + directory[i]
try:
read_Directory(path2, directory[i])
except NotADirectoryError:
pass
except FileNotFoundError:
pass
def read_File(pos, repo):
""" Read the file and return the tree. """
with open(pos) as fp:
my_code = fp.read()
try:
tree = ast.parse(my_code)
# print (ast.dump(tree))
iterate_List(tree, pos, repo)
except SyntaxError:
print('There is a misspelled code')
pass
def iterate_List(tree, pos, repo):
""" Iterate list and assign attributes."""
for i in range(0, len(SetClass)):
for j in range(0, len(SetClass[i])):
attrib = SetClass[i][j]
deepen(tree, attrib, pos, repo)
def deepen(tree, attrib, pos, repo):
""" Create class object """
file = pos.split('/')[-1]
object = IterTree(tree, attrib, file, repo)
def summary_Levels():
""" Summary of directory levels """
result = read_Json()
read_FileCsv()
print(result)
if __name__ == "__main__":
try:
type_option = sys.argv[1]
option = sys.argv[2]
except:
sys.exit("Usage: python3 file.py type-option('directory', " +
"'repo-url', 'user') option(directory, url, user)")
choose_option()
summary_Levels()