|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Scans a source tree for python files and writes the header template on top of each file |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import optparse |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +SRC_DIR = '${buildout:directory}/src/senaite/sync' |
| 11 | + |
| 12 | +YEAR_FROM = '2018' |
| 13 | +TEMPLATE = """# -*- coding: utf-8 -*- |
| 14 | +# |
| 15 | +# This file is part of SENAITE.SYNC |
| 16 | +# |
| 17 | +# Copyright {copy_years} by it's authors. |
| 18 | +# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +def get_template(): |
| 24 | + copy_years = datetime.now().strftime("%Y") |
| 25 | + if copy_years != YEAR_FROM: |
| 26 | + copy_years = '{}-{}'.format(YEAR_FROM, copy_years) |
| 27 | + template_data = { |
| 28 | + "copy_years": copy_years, |
| 29 | + } |
| 30 | + return TEMPLATE.format(**template_data) |
| 31 | + |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + |
| 35 | + parser = optparse.OptionParser() |
| 36 | + |
| 37 | + parser.add_option('-d', '--directory', |
| 38 | + dest='directory', |
| 39 | + default=SRC_DIR, |
| 40 | + help='Source code directory to scan [default: %default]') |
| 41 | + |
| 42 | + parser.add_option('-e', '--extensions', |
| 43 | + dest='extensions', |
| 44 | + default='.py', |
| 45 | + help='Comma separated list of file extensions [default: %default]') |
| 46 | + |
| 47 | + options, args = parser.parse_args(sys.argv) |
| 48 | + |
| 49 | + data = {} |
| 50 | + directory = options.directory |
| 51 | + extensions = map(lambda ext: ext.strip(), options.extensions.split(",")) |
| 52 | + |
| 53 | + def callback(arg, dirname, names): |
| 54 | + # only write the header to the files where the file extension match (.py per default) |
| 55 | + file_names = filter(lambda x: os.path.splitext(x)[-1] in extensions, names) |
| 56 | + # generate a list of full file paths |
| 57 | + file_paths = map(lambda x: os.path.abspath(os.path.join(dirname, x)), file_names) |
| 58 | + # make a mapping of path -> file data |
| 59 | + for path in file_paths: |
| 60 | + lines = open(path, "r").readlines() |
| 61 | + data[path] = lines |
| 62 | + |
| 63 | + # walk the directory |
| 64 | + os.path.walk(directory, callback, None) |
| 65 | + |
| 66 | + for path, lines in data.iteritems(): |
| 67 | + # the new lines start with our header |
| 68 | + new_lines = [get_template()] |
| 69 | + |
| 70 | + skip = True |
| 71 | + for num, line in enumerate(lines): |
| 72 | + # skip all commented lines, but not those of Script (Python) |
| 73 | + if skip and line.startswith("#") and not line.startswith("##"): |
| 74 | + continue |
| 75 | + # skip app empty lines |
| 76 | + if skip and line == "\n": |
| 77 | + continue |
| 78 | + |
| 79 | + # if we reach this point, we found the first code line |
| 80 | + if skip: |
| 81 | + print "Found first code line for file {} at {}".format(path, num) |
| 82 | + skip = False |
| 83 | + |
| 84 | + # append all code lines below the new_lines |
| 85 | + new_lines.append(line) |
| 86 | + |
| 87 | + with open(path, "w") as f: |
| 88 | + # get the last line |
| 89 | + last_line = new_lines[-1] |
| 90 | + # remove all trailing empty lines and add a single one |
| 91 | + new_lines[-1] = last_line.rstrip("\n") + "\n" |
| 92 | + f.writelines(new_lines) |
| 93 | + print "Wrote header to {}".format(path) |
0 commit comments