|
| 1 | +import datetime |
| 2 | +import os |
| 3 | + |
| 4 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 5 | +source_dirs = [os.path.join(os.path.dirname(script_dir), 'src')] |
| 6 | +context = {} |
| 7 | + |
| 8 | +def read_copyright(): |
| 9 | + with open(os.path.join(script_dir,'copyright_text.txt')) as input_file: |
| 10 | + return input_file.read().strip() |
| 11 | + |
| 12 | +def format_copyright(text): |
| 13 | + c = context.copy() |
| 14 | + c['year'] = datetime.datetime.now().year |
| 15 | + return "\n".join([("// "+ t).strip() for t in text.format(**c).split("\n")]) |
| 16 | + |
| 17 | +def process_file(path, copyright_notice): |
| 18 | + with open(path) as input: |
| 19 | + content = input.read() |
| 20 | + lines = content.split("\n") |
| 21 | + for line in lines: |
| 22 | + if line.strip() == "// no-copyright-header": |
| 23 | + return |
| 24 | + for i, line in enumerate(lines): |
| 25 | + if not line.startswith('//'): |
| 26 | + break |
| 27 | + if lines[i].strip() == '': |
| 28 | + i+=1 |
| 29 | + lines = lines[i:] |
| 30 | + new_content = copyright_notice+"\n\n"+"\n".join(lines) |
| 31 | + print("Writing {}".format(path)) |
| 32 | + with open(path, 'w') as output: |
| 33 | + output.write(new_content) |
| 34 | + |
| 35 | + |
| 36 | +def enumerate_files(dir, extensions=['.go', '.js', '.jsx', '.ts', '.tsx'], exclude=set(['node_modules'])): |
| 37 | + for file in os.listdir(dir): |
| 38 | + path = os.path.join(dir, file) |
| 39 | + if file.startswith('.'): |
| 40 | + continue |
| 41 | + if os.path.isdir(path) and file not in exclude: |
| 42 | + for path in enumerate_files(path): |
| 43 | + yield path |
| 44 | + else: |
| 45 | + for extension in extensions: |
| 46 | + if file.endswith(extension): |
| 47 | + yield path |
| 48 | +if __name__ == '__main__': |
| 49 | + copyright_notice = format_copyright(read_copyright()) |
| 50 | + for source_dir in source_dirs: |
| 51 | + for path in enumerate_files(source_dir): |
| 52 | + process_file(path, copyright_notice) |
0 commit comments