-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_name_downloader.py
151 lines (125 loc) · 5.18 KB
/
repo_name_downloader.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
import os
import pandas as pd
import csv
import subprocess
import numpy as np
import shutil
from git import Repo
from git import exc
from xml.dom import minidom
from xml.parsers.expat import ExpatError
import time
import datetime
def giveTimeStamp():
tsObj = time.time()
strToret = datetime.datetime.fromtimestamp(tsObj).strftime('%Y-%m-%d %H:%M:%S')
return strToret
def getDevEmailForCommit(repo_path_param, hash_):
author_emails = []
cdCommand = "cd " + repo_path_param + " ; "
commitCountCmd = " git log --format='%ae'" + hash_ + "^!"
command2Run = cdCommand + commitCountCmd
author_emails = str(subprocess.check_output(['bash','-c', command2Run]))
author_emails = author_emails.split('\n')
# print(type(author_emails))
author_emails = [x_.replace(hash_, '') for x_ in author_emails if x_ != '\n' and '@' in x_ ]
author_emails = [x_.replace('^', '') for x_ in author_emails if x_ != '\n' and '@' in x_ ]
author_emails = [x_.replace('!', '') for x_ in author_emails if x_ != '\n' and '@' in x_ ]
author_emails = [x_.replace('\\n', ',') for x_ in author_emails if x_ != '\n' and '@' in x_ ]
try:
author_emails = author_emails[0].split(',')
author_emails = [x_ for x_ in author_emails if len(x_) > 3 ]
# print(author_emails)
author_emails = list(np.unique(author_emails) )
except IndexError as e_:
pass
return author_emails
def getDevCount(full_path_to_repo, branchName='master', explore=1000):
repo_emails = []
all_commits = []
repo_emails = []
if os.path.exists(full_path_to_repo):
repo_ = Repo(full_path_to_repo)
try:
all_commits = list(repo_.iter_commits(branchName))
except exc.GitCommandError:
print('Skipping this repo ... due to branch name problem', full_path_to_repo )
# only check commit by commit if less than explore threshold
if len( all_commits ) < explore:
for commit_ in all_commits:
commit_hash = commit_.hexsha
emails = getDevEmailForCommit(full_path_to_repo, commit_hash)
repo_emails = repo_emails + emails
else:
repo_emails = [ str(x_) for x_ in range(10) ]
return len(repo_emails)
def makeChunks(the_list, size_):
for i in range(0, len(the_list), size_):
yield the_list[i:i+size_]
def cloneRepo(repo_name, target_dir):
cmd_ = "git clone " + repo_name + " " + target_dir
try:
subprocess.check_output(['bash','-c', cmd_])
except subprocess.CalledProcessError:
print('Skipping this repo ... trouble cloning repo:', repo_name )
def dumpContentIntoFile(strP, fileP):
fileToWrite = open( fileP, 'w')
fileToWrite.write(strP )
fileToWrite.close()
return str(os.stat(fileP).st_size)
def deleteRepo(dirName, type_):
print(':::' + type_ + ':::Deleting ', dirName)
try:
if os.path.exists(dirName):
shutil.rmtree(dirName)
except OSError:
print('Failed deleting, will try manually')
def getPuppetUsage(path2dir):
usageCount = 0
for root_, dirnames, filenames in os.walk(path2dir):
for file_ in filenames:
full_path_file = os.path.join(root_, file_)
if(os.path.exists(full_path_file)):
if (file_.endswith('pp')) :
usageCount = usageCount + 1
return usageCount
def cloneRepos(repo_list, bootThreshold = 5.0):
counter = 0
str_ = ''
for repo_ in repo_list:
counter += 1
print('Cloning ', repo_ )
dirName = '/Users/arahman/TAINTPUP_REPOS/GITHUB/' + repo_.split('/')[-3] + '@' + repo_.split('/')[-2]
cloneRepo(repo_, dirName )
all_fil_cnt = sum([len(files) for r_, d_, files in os.walk(dirName)])
if (all_fil_cnt <= 0):
deleteRepo(dirName, 'NO_FILES')
else:
puppUsage = getPuppetUsage(dirName)
puppProp = (float(puppUsage) / float(all_fil_cnt) ) * 100
if (puppProp <= bootThreshold ):
deleteRepo(dirName, str(puppProp) + '_NOT_ENOUGH_PUPPET')
print('#'*100 )
str_ = str_ + str(counter) + ',' + repo_ + ',' + dirName + ',' + str(puppProp) + ',' + '\n'
print("So far we have processed {} repos".format(counter) )
if((counter % 50) == 0):
dumpContentIntoFile(str_, 'github_taintpup_tracker_completed_repos.csv')
print('#'*100)
if __name__=='__main__':
'''
'''
t1 = time.time()
print('Started at:', giveTimeStamp() )
print('*'*100 )
repos_df = pd.read_csv('/Users/arahman/Documents/OneDriveWingUp/OneDrive-TennesseeTechUniversity/Research/IaC/FixFalsePositive/MiningWork/github_repos_puppet_filtered.csv')
list_ = repos_df['repo_url'].tolist()
list_ = np.unique(list_)
print('Repos to download:', len(list_))
cloneRepos(list_)
print('*'*100 )
print('Ended at:', giveTimeStamp() )
print('*'*100 )
t2 = time.time()
time_diff = round( (t2 - t1 ) / 60, 5)
print('Duration: {} minutes'.format(time_diff) )
print( '*'*100 )