mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Add script trim-src.py
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
5a87040e10
commit
24ba182fa1
5 changed files with 127 additions and 0 deletions
4
bin/Makefile
Normal file
4
bin/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
TOPDIR = ..
|
||||||
|
|
||||||
|
include $(TOPDIR)/make/proj.mk
|
||||||
|
include $(MODDIR)/make/bin.mk
|
||||||
4
scripts/Makefile
Normal file
4
scripts/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
TOPDIR = ..
|
||||||
|
|
||||||
|
include $(TOPDIR)/make/proj.mk
|
||||||
|
include $(MODDIR)/make/scripts.mk
|
||||||
75
scripts/trim-src.py
Normal file
75
scripts/trim-src.py
Normal file
|
|
@ -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')
|
||||||
22
test/trim-src/Makefile
Normal file
22
test/trim-src/Makefile
Normal file
|
|
@ -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
|
||||||
22
test/trim-src/test.mk
Normal file
22
test/trim-src/test.mk
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue