-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
executable file
·111 lines (89 loc) · 3.5 KB
/
manage.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
#!/usr/bin/python3
help_msg = """
this is a script to manage cati project
Commands:
update-headers update files copyright headers
"""
header_text = """#
# the cati project
# Copyright 2020-2021 parsa shahmaleki <[email protected]>
#
# This file is part of cati.
#
# cati is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cati is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cati. If not, see <https://www.gnu.org/licenses/>.
"""
import sys, os
if len(sys.argv) <= 1:
print(help_msg)
sys.exit()
class SetHeaders:
""" Loads all of .py scripts and sets copyright header of them """
def __init__(self, path: str):
self.files_list = []
self.get(path)
def get(self, path: str):
""" Load all of .py files from path """
for f in os.listdir(path):
if os.path.isdir(path + '/' + f):
self.get(path + '/' + f + '/')
elif os.path.isfile(path + '/' + f):
self.add_once(path + '/' + f)
def add_once(self, f: str):
""" Checks a file path and if that file is .py file, add it to the list """
if f[len(f)-3:] == '.py':
# replace // with /
f = f.replace('//', '/')
self.files_list.append(f)
@staticmethod
def set_once_file_header(fname: str):
""" Sets once file copyright header """
global header_text
spliter = ('#' * 50) + '\n\n'
# open file and add header
content = open(fname, 'r').read()
new_content = ''
parts = content.split(spliter)
if len(parts) == 1:
new_content = header_text + spliter + parts[0]
elif len(parts) == 2:
old_header = parts[0]
old_header_parts = old_header.split('# This file is part of cati.')
old_copyrights = old_header_parts[0].split('# the cati project')[-1]
new_header = header_text
tmp = new_header.split('# This file is part of cati.')
new_header = '# the cati project' + old_copyrights + '# This file is part of cati.' + tmp[-1]
new_content = new_header + spliter + parts[1]
else:
print('error in ' + fname + ': duplicate spliter')
return
# add current filename to header
only_file_name = fname.split('/')[-1]
new_content = '#\n# ' + only_file_name + '\n#\n' + new_content
if fname == 'cati/__main__.py' or fname == 'tests/run.py' or fname == 'tests/make_test.py':
new_content = '#!/usr/bin/env python3\n' + new_content
f = open(fname, 'w')
f.write(new_content)
f.close()
if sys.argv[1] == 'update-headers':
# get files list in cati/ folder and set header of them
files_list = SetHeaders('cati/').files_list
for f in files_list:
SetHeaders.set_once_file_header(f)
# get files list in tests/ folder and set header of them
files_list = SetHeaders('tests/').files_list
for f in files_list:
SetHeaders.set_once_file_header(f)
sys.exit()
print('Unknow command "' + sys.argv[1] + '"')
sys.exit(1)