aboutsummaryrefslogtreecommitdiff
path: root/tools/Internals/topc.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/Internals/topc.py')
-rw-r--r--tools/Internals/topc.py145
1 files changed, 145 insertions, 0 deletions
diff --git a/tools/Internals/topc.py b/tools/Internals/topc.py
new file mode 100644
index 0000000..8966e94
--- /dev/null
+++ b/tools/Internals/topc.py
@@ -0,0 +1,145 @@
+#!/usr/bin/env python3
+""" Top comment generator and management.
+
+ The top comment contains the file name and role, the module containing
+ it, and the license statement.
+"""
+
+import os, textwrap
+
+# ---
+# License statements.
+# ---
+
+__lgpl3_statement = """\
+This file is free software: you can redistribute it and/or modify \
+it under the terms of the GNU Lesser General Public License as published by \
+the Free Software Foundation, either version 3 of the License, or \
+(at your option) any later version.
+
+This file 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 Lesser General Lesser Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License \
+along with this file. If not, see <http://www.gnu.org/licenses/>."""
+
+def __get_license_statement(license):
+ """ Get the license statement. """
+
+ if license != 'LGPL-3':
+ return 'This license is unknown.'
+ return __lgpl3_statement
+
+# ---
+# Make the top comment text.
+# ---
+
+def __make_topc_text(mname, name, role, authors, license, width=75):
+ """ Make the top comment text. """
+
+ text = "%s -- " % name
+ text += '\n'.join(textwrap.wrap(role, width=(width - len(text)),
+ subsequent_indent=" "*len(text)))
+ text += '\n\n'
+
+ max_ds_line = 0
+ for (start, end), name, mail in authors:
+ ds = '%s-%s' % (start, end) if start < end else '%s' % start
+ if max_ds_line < len(ds): max_ds_line = len(ds)
+
+ for (start, end), name, mail in authors:
+ ds = '%s-%s' % (start, end) if start < end else '%s' % start
+ copystart = "Copyright (C) %*s "%(max_ds_line, ds)
+ copyline = copystart + "%s <%s>" % (name, mail)
+ copyline = textwrap.wrap(copyline, width=width,
+ subsequent_indent=" "*len(copystart))
+ text += '\n'.join(copyline) + "\n"
+
+ text += "\n"
+ text += '\n'.join(textwrap.wrap("This file is part of the '%s' module " \
+ "in libcarrot, an experimental modular libc project." \
+ % mname, width=width))
+
+ statement = __get_license_statement(license)
+ if statement: text += "\n\n" + statement
+
+ wtab = []
+ for stline in text.splitlines():
+ par = textwrap.wrap(stline, width=width, replace_whitespace=None)
+ if not par: par = ['']
+ for line in par:
+ wtab.append(line)
+
+ return '\n'.join(wtab)
+
+# ---
+# Make the top comment for specific languages, out of text.
+# ---
+
+def __make_topc_c(s, sep='\n'):
+ """ Make a top comment in C using text. """
+
+ __top = '/* ' + '*' * 76
+ __bot = ' * ' + '*' * 73 + ' */'
+
+ mid = sep.join(map(lambda x: ' *' + (' ' if x else '') + x,
+ s.splitlines()))
+ fin = sep.join((__top, mid, __bot)) + sep
+ return fin
+
+def __make_topc_sh(s, sep='\n'):
+ """ Make a bootiful header. """
+
+ __sep = '#' + '*' * 78
+
+ mid = sep.join(map(lambda x: '#' + (' ' if x else '') + x,
+ s.splitlines()))
+ fin = sep.join((__sep, mid, __sep)) + sep
+ return fin
+
+# ---
+# Main interface:
+# Make the top comment, and get the file content after the top comment.
+# ---
+
+def get_source_content(path, sep='\n', syntax='c'):
+ """ Get a source file content after the top comment. """
+
+ if syntax == 'c':
+ is_str = lambda x: x[:3] == '/* ' and \
+ all(c == '*' for c in x[3:])
+ is_end = lambda x: x[:3] == ' * ' and \
+ all(c == '*' for c in x[3:-3]) and x[-3:] == ' */'
+ else:
+ is_str = lambda x: x[0] == '#' and \
+ all(c == '*' for c in x[1:])
+ is_end = isstr
+
+ with open(path) as src:
+ line = src.readline()
+ while True:
+ if not line or not is_str(line.splitlines()[0]):
+ break
+
+ while True:
+ line = src.readline()
+ if not line or is_end(line.splitlines()[0]):
+ break
+
+ line = src.readline()
+
+ oneline = lambda x:x.splitlines()[0] if x else ""
+ line = oneline(line)
+ return sep.join([line] + list(map(oneline, src.readlines())) + [''])
+
+def make_top_comment(module_name, name, role, authors, license, syntax='c'):
+ """ Make a top comment using different syntaxes. """
+
+ width = 76 if syntax == 'c' else 77
+ text = __make_topc_text(module_name, name, role, authors, license,
+ width=width)
+ return __make_topc_c(text) if syntax == 'c' else __make_topc_sh(text)
+
+# End of file.