-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean_repo.py
198 lines (169 loc) · 6.91 KB
/
clean_repo.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
# Before running this file you need to run the `./scripts/clean_repos.ipynb`
# Copyright (c) Huawei Cloud.
# Licensed under the MIT license.
import json
import sys
import os
import re
import ipdb
import shutil
import isort
import autopep8
import black
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
class RepoCleaner():
"""
Strategies to clean a repository.
"""
@staticmethod
def remove_copy_right(python_file_content):
"""
Clean the given python file by removing copy right.
"""
# Remove copy right in the beginning of the file.
copyright_regex = r"^\s*#.*(?:\n\s*#.*)*"
cleaned_content = re.sub(copyright_regex, '', python_file_content, count=1, flags=re.MULTILINE)
return cleaned_content.strip()
@staticmethod
def remove_empty_folders(path):
is_deleted = False
if not os.path.isdir(path):
return is_deleted
for entry in os.listdir(path):
full_path = os.path.join(path, entry)
if os.path.isdir(full_path):
if RepoCleaner.remove_empty_folders(full_path):
is_deleted = True
if not os.listdir(path):
logging.info(f"Removing empty folder: {path}")
os.rmdir(path)
is_deleted = True
return is_deleted
@staticmethod
def delete_md(path):
"""
Delete all the ".md" files in a directory except the "README.md" file.
"""
remained_path = os.path.join(path, "README.md")
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".md"):
# os.remove(os.path.join(root, file))
this_md_path = os.path.join(root, file)
if this_md_path == remained_path:
continue
os.system(f"rm -rf {this_md_path}")
def not_contains_py_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
return False
return True
def list_all_paths(repo_path):
"""
Get all file or folder paths in a directory
"""
all_repo_paths = []
for root, dirs, files in os.walk(repo_path):
for dir in dirs:
if dir.startswith('.'):
continue
all_repo_paths.append(os.path.join(root, dir))
for file in files:
if file.startswith('.'):
continue
all_repo_paths.append(os.path.join(root, file))
return all_repo_paths
def mkdir_folder_for_file(file_path):
"""
Create folder for a file if not exists.
"""
folder_path = os.path.dirname(file_path)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Running clean script for given repository.')
parser.add_argument('--base_path', type=str, help='Path to repository.')
parser.add_argument('--new_base_path', type=str, help='Cleaned repository path.')
parser.add_argument('--repo_names', type=str, help='Repository names.')
args = parser.parse_args()
repo_names = args.repo_names.split(',')
for repo_name in repo_names:
repo_path = os.path.join(args.base_path, repo_name)
new_repo_path = os.path.join(args.new_base_path, repo_name)
logging.info(f"")
# reset new_repo_path
# if os.path.exists(new_repo_path):
# os.system(f"rm -rf {new_repo_path}")
all_repo_paths = list_all_paths(repo_path)
logging.info(f"total {len(all_repo_paths)} files in {repo_path}, cleaning...")
for old_path in all_repo_paths:
new_path = old_path.replace(args.base_path, args.new_base_path)
assert "test" not in repo_name and "log" not in repo_name
if "test" in new_path or "log" in new_path:
continue
if os.path.isdir(old_path): # folder
flag = False
for folder in os.path.dirname(new_path).split("/"):
if folder.startswith('.') and "." != folder:
flag = True
break
if flag:
continue
if not os.path.exists(new_path):
os.makedirs(new_path)
else: # file
# if os.path.dirname(new_path).
flag = False
for folder in os.path.dirname(new_path).split("/"):
if folder.startswith('.') and "." != folder:
flag = True
break
if flag:
continue
if not os.path.exists(os.path.dirname(new_path)):
os.makedirs(os.path.dirname(new_path))
if new_path.endswith('.py'):
with open(old_path, 'r') as f:
python_file_content = f.read()
cleaned_python_file_content = RepoCleaner.remove_copy_right(python_file_content)
try:
sorted_code = isort.code(cleaned_python_file_content)
# beatified_code = autopep8.fix_code(sorted_code) # using `autopep8`
except:
continue
try:
beatified_code = black.format_str(sorted_code, mode=black.FileMode()) # using `black`
except:
beatified_code = sorted_code
with open(new_path, 'w+') as f:
f.write(beatified_code)
elif "README.md" in new_path:
if os.path.exists(new_path):
continue
shutil.copyfile(old_path, new_path)
elif "requirements.txt" in new_path:
shutil.copyfile(old_path, new_path)
elif new_path.endswith('.sh') or new_path.endswith('html'):
shutil.copyfile(old_path, new_path)
else:
# with open(new_path, 'w+') as f:
# f.write('')
pass
logging.info(f"finished cleaning {repo_path}.")
logging.info(f"clean done!")
# Delete all the ".md" files in a directory except the "README.md" file.
RepoCleaner.delete_md(new_repo_path)
logging.info(f"delete md done!")
# Delete all empty folders in new_repo_path
RepoCleaner.remove_empty_folders(new_repo_path)
logging.info(f"remove empty folders done!")
if not_contains_py_files(new_repo_path):
logging.info(f"no py files in {new_repo_path}, removing...")
os.system(f"rm -rf {new_repo_path}")
logging.info(f"remove done!")