forked from senaite/senaite.ast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_code_headers.py.in
113 lines (93 loc) · 3.7 KB
/
write_code_headers.py.in
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
#!/usr/bin/env python
#
# Scans a source tree for python files and writes the header template on top of
# each file
import os
import sys
import optparse
from datetime import datetime
SRC_DIR = '${buildout:directory}/src/senaite'
YEAR_FROM = "2020"
TEMPLATE = """# -*- coding: utf-8 -*-
#
# This file is part of SENAITE.AST.
#
# SENAITE.AST 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, version 2.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright {copy_years} by it's authors.
# Some rights reserved, see README and LICENSE.
"""
def get_template():
copy_years = datetime.now().strftime("%Y")
if copy_years != YEAR_FROM:
copy_years = '{}-{}'.format(YEAR_FROM, copy_years)
template_data = {
"copy_years": copy_years,
}
return TEMPLATE.format(**template_data)
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-d", "--directory",
dest="directory",
default=SRC_DIR,
help="Source code directory to scan "
"[default: %default]")
parser.add_option("-e", "--extensions",
dest="extensions",
default=".py",
help="Comma separated list of file extensions "
"[default: %default]")
options, args = parser.parse_args(sys.argv)
data = {}
directory = options.directory
extensions = map(lambda ext: ext.strip(), options.extensions.split(","))
def callback(arg, dirname, names):
# only write the header to the files where the file extension match
# (.py per default)
file_names = filter(lambda x: os.path.splitext(x)[-1] in extensions,
names)
# generate a list of full file paths
file_paths = map(lambda x: os.path.abspath(os.path.join(dirname, x)),
file_names)
# make a mapping of path -> file data
for path in file_paths:
lines = open(path, "r").readlines()
data[path] = lines
# walk the directory
os.path.walk(directory, callback, None)
for path, lines in data.iteritems():
# the new lines start with our header
new_lines = [get_template()]
skip = True
for num, line in enumerate(lines):
# skip all commented lines, but not those of Script (Python)
if skip and line.startswith("#") and not line.startswith("##"):
continue
# skip app empty lines
if skip and line == "\n":
continue
# if we reach this point, we found the first code line
if skip:
print "Found first code line for file {} at {}".format(
path, num)
skip = False
# append all code lines below the new_lines
new_lines.append(line)
with open(path, "w") as f:
# get the last line
last_line = new_lines[-1]
# remove all trailing empty lines and add a single one
new_lines[-1] = last_line.rstrip("\n") + "\n"
f.writelines(new_lines)
print "Wrote header to {}".format(path)