-
Notifications
You must be signed in to change notification settings - Fork 12
/
Project_selection_count_copy.py
192 lines (165 loc) · 6.94 KB
/
Project_selection_count_copy.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
# -*- coding: utf-8 -*-
import os, shutil, hashlib, filecmp
from MyUtils import read_file, java_files_from_dir, write_file, copytree, rm_dup
github_original_path = '/Users/Falcon/Desktop/GitArchive/Git_20161108/'
github_with_testcode_path = '/Users/Falcon/Desktop/GitArchive/Git_with_test/'
github_with_testcode_over_avg_path = '/Users/Falcon/Desktop/GitArchive/Git_with_test_over_avg/'
github_with_testcode_under_avg_path = '/Users/Falcon/Desktop/GitArchive/Git_with_test_under_avg/'
github_without_testcode_path = '/Users/Falcon/Desktop/GitArchive/Git_without_test/'
github_without_testcode_over_avg_path = '/Users/Falcon/Desktop/GitArchive/Git_without_test_over_avg/'
github_without_testcode_under_avg_path = '/Users/Falcon/Desktop/GitArchive/Git_without_test_under_avg/'
def rm_dup_java(src):
javafiles = java_files_from_dir(src)
final_list = list()
flag = 0
for file in javafiles:
print file
if not file in final_list:
for f in final_list:
if filecmp.cmp(file, f) == 1:
os.remove(file)
print 'Deleting ', file
flag = 1
break
if flag == 0:
final_list.append(file)
elif flag == 1:
flag = 0
def convertingToJava(src):
print 'Conver'
for dirpath, dirnames, files in os.walk(src):
for file in files:
if file.endswith('.txt'):
os.rename(dirpath + '/' + file, dirpath + '/' + file.split('.')[0] + '.java')
def deleteEmptyDirs(src):
for dirpath, dirnames, files in os.walk(src):
if not dirnames and not files:
shutil.rmtree(dirpath)
def refineDirectories(src):
progress = 0
for dirpath, dirnames, files in os.walk(src):
progress = progress + 1
if progress % 1000 == 0: print 'Parsing: ', progress
if dirpath.split('/')[-1] == 'CodeFragments' or dirpath.split('/')[-1] == 'SynthesizedMethods':
print "Directory " + dirpath + "is being deleted.."
shutil.rmtree(dirpath)
if dirpath.split('/')[-1] == 'CompleteMethod':
for file in files:
new_dir = '/'.join(dirpath.split('/')[:-1]) + '/' + file.split('.')[0]
try:os.mkdir(new_dir)
except:pass
shutil.copyfile(dirpath + '/' + file, new_dir + '/' + file)
shutil.rmtree(dirpath)
def count_projects(dir):
count_project = 0
for i in os.listdir(dir):
if i == '.DS_Store':
continue
count_project += 1
print "Projects count: ", count_project
def count_javafiles(dir):
count_javafile = 0
javafiles = java_files_from_dir(dir)
for i in javafiles:
if i == '.DS_Store':
continue
count_javafile += 1
print "Javafiles count: ", count_javafile
def copy_projects_from_gitbase(listfile, gitbase, dst):
print 'Copying the target projects...'
count_copy = 0
target_list = read_file(listfile)
for i in target_list.split('\n'):
if i == '.DS_Store':
continue
count_copy += 1
print "Processing " + i + '...' + str(count_copy)
try:
copytree(gitbase + i, dst + i)
except:
print '!!!!!!!!!!!!!!!!!!!!!!!! Copy failed with ' + i
print 'Done..'
def copyConfirm(listfile, dir):
content = read_file(listfile)
projects = content.split('\n')
error_count = 0
for i in os.listdir(dir):
if i in projects:
continue
else:
error_count += 1
print i
print "error count: ", error_count
def calcAvgJavafiles(dir):
project_list = os.listdir(dir)
total_project = len(project_list)
total_javafiles = 0
for project in project_list:
javafiles = java_files_from_dir(dir + project)
for file in javafiles:
total_javafiles += 1
print total_project
print total_javafiles
return float(total_javafiles / total_project)
def splitProjectsByAvg(base, avg_count, over_dir, under_dir):
project_list = os.listdir(base)
for project in project_list:
basic_str = "Project " + str(project) + " goes to "
javafile_count = 0
javafiles = java_files_from_dir(base + project)
for file in javafiles:
javafile_count += 1
if javafile_count > avg_count:
print basic_str + "OVER (%s)" % javafile_count
try: copytree(base + project, over_dir + project)
except: print "Copy failed with " + project
else:
print basic_str + "UNDER (%s)" % javafile_count
try: copytree(base + project, under_dir + project)
except: print "Copy failed with " + project
if __name__ == '__main__':
# #Count original number of projects
# count_projects(github_original_path)
#
# #Count original number of javafiles
# count_javafiles(github_original_path)
#
# # Count projects with test
# count_projects(github_with_testcode_path)
#
# # Count javafiles with test
# count_javafiles(github_with_testcode_path)
#
# # Count projects without test
# count_projects(github_without_testcode_path)
#
# # Count javafiles without test
# count_javafiles(github_without_testcode_path)
#
# # Count projects without test and over avg
# count_projects(github_without_testcode_over_avg_path)
#
# # Count javafiles without test and over avg
# count_javafiles(github_without_testcode_over_avg_path)
# Copy paste projects in listfile from gitbase
# listfile = '/Users/Falcon/Desktop/projects_without_testcode.txt'
# copy_projects_from_gitbase(listfile, github_original_path, github_without_testcode_path)
# Confirm // 복사할때 자꾸 이상한 프로젝트가 함께 실려간다. ㅡㅡ^
# listfile = '/Users/Falcon/Desktop/projects_without_testcode.txt'
# copyConfirm(listfile, github_without_testcode_path)
# # Split the projects with test
# avg_javafile_count = calcAvgJavafiles(github_with_testcode_path)
# print "--- The average = " + str(avg_javafile_count)+ " ---"
# splitProjectsByAvg(github_with_testcode_path, avg_javafile_count, github_with_testcode_over_avg_path, github_with_testcode_under_avg_path)
# # Split the projects without test
# avg_javafile_count = calcAvgJavafiles(github_without_testcode_path)
# print "--- The average = " + str(avg_javafile_count) + " ---"
# splitProjectsByAvg(github_without_testcode_path, avg_javafile_count, github_without_testcode_over_avg_path, github_without_testcode_under_avg_path)
# test = '/Users/Falcon/Desktop/Test_code_augmentation/aa/'
# src = '/Users/Falcon/Desktop/Test_code_augmentation/facoy_new/'
# refineDirectories(src) # removing useless dirs
# convertingToJava(src) # convert every txt files to java files
# rm_dup(src) # duplicate
# rm_dup_java(src) # duplicate_2
# deleteEmptyDirs(src) # removing empty dirs
pass