Skip to content

Commit e8373e0

Browse files
committed
Merge pull request #11084 from peter1000/add_license_to_files_jl
add_license_to_files_jl [ci skip] since this just adds a new file
2 parents 76927ce + 2d7362a commit e8373e0

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

contrib/add_license_to_files.jl

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/usr/bin/env julia
2+
# This file is a part of Julia. License is MIT: https://julialang.org/license
3+
4+
# Adds the julia license line `new_license` to configured file extensions in `rootdirs`.
5+
#
6+
# Option `old_license` to remove an existing license first in case one wants to change the
7+
# license text in the future.
8+
#
9+
# Checks also if somewhere else in the file the license text is found (`copy/past error`)
10+
# and if possible deletes such lines - if other text is on the same line it raises an error.
11+
12+
### CONFIG HERE
13+
14+
const print_result = true # prints files which where not processed.
15+
16+
const rootdirs = [
17+
"../base",
18+
"../contrib",
19+
"../examples",
20+
"../src",
21+
"../test",
22+
]
23+
24+
# to exculde whole sub directories
25+
const excludedirs = [
26+
# see: https://github.com/JuliaLang/julia/pull/11073#issuecomment-98090053
27+
"../base/grisu",
28+
"../src/flisp",
29+
]
30+
31+
const skipfiles = [
32+
"../contrib/add_license_to_files.jl",
33+
# files to check - already copyright
34+
# see: https://github.com/JuliaLang/julia/pull/11073#issuecomment-98099389
35+
"../base/special/trig.jl",
36+
"../base/sparse/csparse.jl",
37+
#
38+
"../src/abi_llvm.cpp",
39+
"../src/abi_win32.cpp",
40+
"../src/abi_win64.cpp",
41+
"../src/abi_x86.cpp",
42+
"../src/abi_x86_64.cpp",
43+
"../src/disasm.cpp",
44+
"../src/support/END.h",
45+
"../src/support/ENTRY.amd64.h",
46+
"../src/support/ENTRY.i387.h",
47+
"../src/support/MurmurHash3.c",
48+
"../src/support/MurmurHash3.h",
49+
"../src/support/asprintf.c",
50+
"../src/support/strtod.c",
51+
"../src/support/utf8.c",
52+
]
53+
54+
const ext_prefix = Dict([
55+
(".jl", "# "),
56+
(".sh", "# "),
57+
(".h", "\/\/ "),
58+
(".c", "\/\/ "),
59+
(".cpp", "\/\/ "),
60+
])
61+
62+
const new_license = "This file is a part of Julia. License is MIT: http://julialang.org/license"
63+
64+
# Old License text if such should be first removed - or empty string
65+
const old_license = ""
66+
67+
### END CONFIG HERE
68+
69+
70+
function check_lines!(path::AbstractString, lines::Vector, checktxt::AbstractString,
71+
prefix::ASCIIString, oldcheck::Bool)
72+
remove = []
73+
for i in 1:length(lines)
74+
line = lines[i]
75+
if contains(line, checktxt)
76+
if strip(line) == strip(prefix * checktxt) || strip(line) == strip(checktxt)
77+
push!(remove, i)
78+
else
79+
error(string("`path` contains an additional line with ",
80+
oldcheck ? "old" : "new",
81+
" license.\nlinenum: $(i): $(line) \n`path:` $(path)\n",
82+
"Fix this first or add the file to `skipfiles`.\n\n"))
83+
end
84+
end
85+
end
86+
deleteat!(lines, remove)
87+
end
88+
89+
license_linenum(line) = startswith(strip(line), "#!") ? 2 : 1
90+
91+
# Collects all absolute file paths in rootdir inclusive subdirs
92+
function getfilespaths!(filepaths::Vector, rootdir::AbstractString)
93+
isdir(rootdir) || error(string("`rootdir` must be an directory. "))
94+
abs_rootdir = abspath(rootdir)
95+
for name in readdir(abs_rootdir)
96+
path = joinpath(abs_rootdir, name)
97+
if isdir(path)
98+
getfilespaths!(filepaths, path)
99+
else
100+
push!(filepaths, joinpath(abs_rootdir, name))
101+
end
102+
end
103+
end
104+
105+
function add_license_line!(unprocessed::Vector, src::AbstractString, new_license::AbstractString,
106+
old_license::AbstractString, ext_prefix::Dict, abs_excludedirs::Vector,
107+
skipfiles::Vector)
108+
109+
for name in readdir(src)
110+
path = normpath(joinpath(src, name))
111+
if isdir(path)
112+
if path in abs_excludedirs
113+
getfilespaths!(unprocessed, path)
114+
continue
115+
else
116+
add_license_line!(unprocessed, path, new_license, old_license,
117+
ext_prefix, abs_excludedirs, skipfiles)
118+
end
119+
elseif path in skipfiles
120+
push!(unprocessed, path)
121+
continue
122+
else
123+
ext = splitext(path)[2]
124+
if ext in keys(ext_prefix)
125+
prefix = ext_prefix[ext]
126+
f = open(path, "r")
127+
lines = readlines(f)
128+
close(f)
129+
isempty(lines) && (push!(unprocessed, path); continue)
130+
isempty(old_license) || check_lines!(path, lines, old_license, prefix, true)
131+
check_lines!(path, lines, new_license, prefix, false)
132+
# check shebang file
133+
linenum = license_linenum(lines[1])
134+
if !isempty(strip(lines[linenum]))
135+
insert!(lines, linenum, string(prefix, new_license, "\n\n"))
136+
else
137+
insert!(lines, linenum, string(prefix, new_license, "\n"))
138+
end
139+
open(path, "w") do f
140+
for line in lines
141+
write(f, line)
142+
end
143+
end
144+
else
145+
push!(unprocessed, path)
146+
end
147+
end
148+
end
149+
return unprocessed
150+
end
151+
152+
# Returns a new Vector with all absolute path: raises an error if path does not exist
153+
function abspaths(A::Vector)
154+
abs_A = []
155+
for p in A
156+
abs_p = isabspath(p) ? normpath(p) : normpath(joinpath(dirname(@__FILE__), p))
157+
ispath(abs_p) || error(string("`abs_p` seems not to be an existing path. ",
158+
"Adjust your configuration: <", p, "> : ", abs_p, "\n"))
159+
push!(abs_A, abs_p)
160+
end
161+
return abs_A
162+
end
163+
164+
function add_license(rootdirs::Vector, new_license::AbstractString, old_license::AbstractString,
165+
ext_prefix::Dict, excludedirs::Vector, skipfiles::Vector, print_result::Bool)
166+
isempty(strip(new_license)) && error("`new_license` may not only contain white space.")
167+
abs_skipfiles = abspaths(skipfiles)
168+
abs_rootdirs = abspaths(rootdirs)
169+
abs_excludedirs = abspaths(excludedirs)
170+
for p in abs_rootdirs
171+
p in abs_excludedirs && error(string("Seems one of the `rootdirs` is also included in ",
172+
"the `excludedirs`. `rootdirs`: ", p))
173+
unprocessed = []
174+
add_license_line!(unprocessed, p, new_license, old_license, ext_prefix,
175+
abs_excludedirs, abs_skipfiles)
176+
if print_result
177+
println("\nUnprocessed files in rootdir: <$(p)>\n")
178+
for file in unprocessed
179+
println(" <", basename(file), "> : ", file)
180+
end
181+
end
182+
end
183+
end
184+
185+
186+
## ---------------
187+
188+
add_license(rootdirs, new_license, old_license, ext_prefix, excludedirs, skipfiles, print_result)

0 commit comments

Comments
 (0)