From 24ba182fa12b1cd6a4d07cf5699eb6542c962228 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 18 Nov 2017 15:42:09 +0100 Subject: [PATCH] Add script trim-src.py Signed-off-by: Jan Lindemann --- bin/Makefile | 4 +++ scripts/Makefile | 4 +++ scripts/trim-src.py | 75 ++++++++++++++++++++++++++++++++++++++++++ test/trim-src/Makefile | 22 +++++++++++++ test/trim-src/test.mk | 22 +++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 bin/Makefile create mode 100644 scripts/Makefile create mode 100644 scripts/trim-src.py create mode 100644 test/trim-src/Makefile create mode 100644 test/trim-src/test.mk diff --git a/bin/Makefile b/bin/Makefile new file mode 100644 index 0000000..ec53fd6 --- /dev/null +++ b/bin/Makefile @@ -0,0 +1,4 @@ +TOPDIR = .. + +include $(TOPDIR)/make/proj.mk +include $(MODDIR)/make/bin.mk diff --git a/scripts/Makefile b/scripts/Makefile new file mode 100644 index 0000000..2b85b66 --- /dev/null +++ b/scripts/Makefile @@ -0,0 +1,4 @@ +TOPDIR = .. + +include $(TOPDIR)/make/proj.mk +include $(MODDIR)/make/scripts.mk diff --git a/scripts/trim-src.py b/scripts/trim-src.py new file mode 100644 index 0000000..8508e08 --- /dev/null +++ b/scripts/trim-src.py @@ -0,0 +1,75 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from abc import abstractmethod +import magic +import os.path +import re + +import jwutils +from jwutils.log import * + +class TrimSourceCmd(jwutils.Cmd): + + def __init__(self, name, help): + super(TrimSourceCmd, self).__init__(name, help=help) + + @abstractmethod + def _run(self, args, mime, path, contents): + raise Exception("_run() method not implemented") + + def add_parser(self, parsers): + p = super(TrimSourceCmd, self).add_parser(parsers) + p.add_argument("input", help="input files", nargs='+') + return p + + def run(self, args): + ms = magic.open(magic.NONE) + ms.load() + for path in args.input: + with open(path, 'r+') as infile: + contents = infile.read() + + # TODO: support other types in this block + mime = magic.detect_from_content(contents).mime_type + if mime != 'text/plain': + raise Exception("unsupported file type", mime, "of", path) + name = os.path.basename(path) + ext = os.path.splitext(path)[1] + if name != 'Makefile' and ext != '.mk': + raise Exception("unsupported file type of", path) + mime = 'text/x-makefile' + + out = self._run(args, mime, path, contents) + infile.seek(0) + infile.write(out) + +class CmdBeautify(TrimSourceCmd): + + def __init__(self): + super(CmdBeautify, self).__init__("beautify", help="Beautify source code files") + + def add_parser(self, parsers): + p = super(CmdBeautify, self).add_parser(parsers) + p.add_argument('--indent-assignments', help='indent equal sign by this amount', nargs='?', default=30) + return p + + def _processTextXMakefile(self, args, mime, path, contents): + r = '' + indent = args.indent_assignments + for line in contents.splitlines(True): + m = re.match(r'^ *([a-zA-Z_][a-zA-Z0-9_]*)\s*([?+]*)(=)\s*(.*)', line) + if m is None: + r += line + continue + r += misc.pad(m.group(1), indent - len(m.group(2))) + m.group(2) + m.group(3) + ' ' + m.group(4) + '\n' + return r + + def _run(self, args, mime, path, contents): + slog(NOTICE, "formatting", path, "of type", mime) + method = getattr(self, '_process' + ''.join(s.capitalize() for s in re.split('[/-]', mime))) + if method is None: + raise Exception("unsupported file type", mime, "of", path) + return method(args, mime, path, contents) + +jwutils.run_sub_commands('trim source code') diff --git a/test/trim-src/Makefile b/test/trim-src/Makefile new file mode 100644 index 0000000..2ea8bf2 --- /dev/null +++ b/test/trim-src/Makefile @@ -0,0 +1,22 @@ +TOPDIR = ../.. + +MAKEFLAGS += --no-builtin-rules + +INPUT = test.mk +EXE = $(TOPDIR)/scripts/trim-src.py +EXE_ARGS = beautify $(INPUT) + +include $(TOPDIR)/make/proj.mk +include $(MODDIR)/make/py-run.mk + +run: $(INPUT) + +$(INPUT): + cp Makefile $@ +clean.test: + rm -f $(INPUT) +clean: clean.test + +help: + python $(EXE) -h + python $(EXE) beautify -h diff --git a/test/trim-src/test.mk b/test/trim-src/test.mk new file mode 100644 index 0000000..206fc90 --- /dev/null +++ b/test/trim-src/test.mk @@ -0,0 +1,22 @@ +TOPDIR = ../.. + +MAKEFLAGS += --no-builtin-rules + +INPUT = test.mk +EXE = $(TOPDIR)/scripts/trim-src.py +EXE_ARGS = beautify $(INPUT) + +include $(TOPDIR)/make/proj.mk +include $(MODDIR)/make/py-run.mk + +run: $(INPUT) + +$(INPUT): + cp Makefile $@ +clean.test: + rm -f $(INPUT) +clean: clean.test + +help: + python $(EXE) -h + python $(EXE) beautify -h