Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Python support in build scripts #141

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
d2f9e16
Commit more Python versions of scripts. WIP
Mar 25, 2021
8a0c6ab
Add Python version of add_copyright, and adjust Makefile.aut accordin…
Mar 26, 2021
44e9973
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
047a6eb
Debug in progress...
Mar 28, 2021
27ef7b3
Commit more Python versions of scripts. WIP
Mar 25, 2021
35d1a43
Add Python version of add_copyright, and adjust Makefile.aut accordin…
Mar 26, 2021
f9e1c8b
Debug in progress...
Mar 28, 2021
40c61ae
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
ac7aa70
Merge branch 'doc' of ../less into doc
May 5, 2021
01ab369
WIP
Mar 28, 2021
3410c92
Merge branch 'doc' of ../less into doc
May 5, 2021
03ef742
Commit more Python versions of scripts. WIP
Mar 25, 2021
816109e
Add Python version of add_copyright, and adjust Makefile.aut accordin…
Mar 26, 2021
229bf66
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
2566293
WIP
Mar 28, 2021
bb82079
Commit more Python versions of scripts. WIP
Mar 25, 2021
5568dbf
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
a6102a5
Debug in progress...
Mar 28, 2021
3e1ffb9
Merge branch 'doc' of ../less into doc
May 13, 2021
d434e88
Commit more Python versions of scripts. WIP
Mar 25, 2021
ac48936
Add Python version of add_copyright, and adjust Makefile.aut accordin…
Mar 26, 2021
a16166a
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
3bda1d9
WIP
Mar 28, 2021
38a4000
Commit more Python versions of scripts. WIP
Mar 25, 2021
37eaafc
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
3d7b19e
Debug in progress...
Mar 28, 2021
1f8437c
Fourth time?
May 20, 2021
f7aa531
Merge branch 'doc' of ../less into doc
May 20, 2021
c2dc705
Add Python equivalents to scripts in lesstest.
May 26, 2021
ec07e10
Append Perl scripts with .pl extension, and more.
May 26, 2021
ff4d1a0
Improve Python version of mkhelp.
Aug 19, 2021
6b8fe60
Commit more Python versions of scripts. WIP
Mar 25, 2021
bd7d4f2
Adjust Makefile.aut.
Mar 26, 2021
e58a570
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
e57713c
WIP
Mar 28, 2021
6cde79d
Commit more Python versions of scripts. WIP
Mar 25, 2021
be8b86d
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
76b37af
Debug in progress...
Mar 28, 2021
a25bac3
Fourth time?
May 20, 2021
7d3b755
Commit more Python versions of scripts. WIP
Mar 25, 2021
c55f714
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
d0303ab
WIP
Mar 28, 2021
692cdd2
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
6ae35b8
Debug in progress...
Mar 28, 2021
223a440
Commit more Python versions of scripts. WIP
Mar 25, 2021
1458248
Adjust Makefile.aut accordingly.
Mar 26, 2021
146bbed
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
07c0751
WIP
Mar 28, 2021
a08e9ff
Debug in progress...
Mar 28, 2021
695a978
Commit more Python versions of scripts. WIP
Mar 25, 2021
2b35fdc
Adjust Makefile.aut accordingly.
Mar 26, 2021
6d08bad
Finalize conversion of Perl build scripts into Python versions.
Mar 27, 2021
3e0f671
Debug in progress...
Mar 28, 2021
75cef10
Add Python equivalents to scripts in lesstest.
May 26, 2021
8ce3322
Append Perl scripts with .pl extension, and more.
May 26, 2021
ead4c8f
Improve Python version of mkhelp.
Aug 19, 2021
ed6019f
Fix merge conflict in mkutable.py
Aug 19, 2021
d38a3c6
Touch up a few Python scripts
Aug 19, 2021
f7441da
Remove mention of ADD_COPYRIGHT.
Aug 19, 2021
0cc2c60
parent 0202daa172c6bc08c7043ebc740b2fde2db488b6
gwsw Aug 16, 2021
705bbe1
Remove mention of ADD_COPYRIGHT.
Aug 19, 2021
aa928fb
Replace executing `date` command with standard Python module.
Aug 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Makefile.aut
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ REL := $(shell sed -e '/char version/!d' -e 's/[^0-9.]*\([0-9.]*\).*/\1/' -e q $
ifeq ($(USE_PYTHON),1)
MKHELP = mkhelp.py
MKFUNCS = mkfuncs.py
MKUTABLE = mkutable.py
else
MKHELP = mkhelp.pl
MKFUNCS = mkfuncs.pl
MKUTABLE = mkutable.pl
endif

SRC = \
Expand Down Expand Up @@ -105,13 +107,13 @@ ${srcdir}/lessecho.man: ${srcdir}/lessecho.nro
${NROFF} ${srcdir}/lessecho.nro >${srcdir}/lessecho.man

compose.uni: unicode/UnicodeData.txt
./mkutable -f2 Mn Me -- unicode/UnicodeData.txt > $@
${srcdir}/${MKUTABLE} -f2 Mn Me -- unicode/UnicodeData.txt > $@
fmt.uni: unicode/UnicodeData.txt
./mkutable -f2 Cf -- unicode/UnicodeData.txt > $@
${srcdir}/${MKUTABLE} -f2 Cf -- unicode/UnicodeData.txt > $@
ubin.uni: unicode/UnicodeData.txt
./mkutable -f2 Cc Cs Co Zl Zp -- unicode/UnicodeData.txt > $@
${srcdir}/${MKUTABLE} -f2 Cc Cs Co Zl Zp -- unicode/UnicodeData.txt > $@
wide.uni: unicode/EastAsianWidth.txt
./mkutable -f1 W F -- unicode/EastAsianWidth.txt > $@
${srcdir}/${MKUTABLE} -f1 W F -- unicode/EastAsianWidth.txt > $@

unicode/UnicodeData.txt:
mkdir -p unicode
Expand Down
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ INSTALLATION (Unix & Linux systems only):
if you have not already done so.

2. If you are building from a clone of a git repository,
type "make -f Makefile.aut".
type "make -f Makefile.aut". If you would like to use Python versions of
build scripts, define "USE_PYTHON=1" before typing make.
If you are building from a numbered release package (a tar or
zip file with a name like less-999.tar.gz or less-999.zip downloaded
from greenwoodsoftware.com, not from github), you should skip this step.
Expand Down
30 changes: 30 additions & 0 deletions add_copyright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python

import os
import argparse
import pathlib
import re

parser = argparse.ArgumentParser()
parser.add_argument("file", type=str)
parser.add_argument("dir", type=str)
args = parser.parse_args()
out = pathlib.Path(args.dir) / args.file

copyright = copyright_line = ""
with open("copyright") as f:
copyright = f.read()
for line in copyright.splitlines():
if '(C)' in line:
copyright_line = line.strip("/*/\r\n").rstrip(" ")
break

with open(args.file) as input, out.open('w') as o:
for line in input:
if re.search(r"\@\@copyright\@\@", line):
o.write(copyright)
continue
out_line = re.sub(r"\@\@copyright_oneline\@\@", copyright_line, line)
o.write(out_line)

out.chmod(0o555 if os.access(out, os.X_OK) else 0o444)
67 changes: 67 additions & 0 deletions lesstest/gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python

from argparse import ArgumentParser, FileType
from subprocess import run
from sys import exit, stderr, argv
from itertools import count
from pathlib import Path
from re import search
from os import environ

lesstest = "./lesstest"
outdir = Path("./suite")


def main():
pars = ArgumentParser(description="Generate a lesstest file.", add_help=False)
pars.add_argument("-w", help="width", type=int)
pars.add_argument("-h", help="height", type=int)
pars.add_argument("--lessvar", default=environ.get("LT_LESSVAR"))
pars.add_argument("less", type=Path)
pars.add_argument("lessflags", nargs="?")
pars.add_argument("textfile", type=FileType("r"))
args = pars.parse_args()

width, height = term_size()
if width > 2:
width -= 2
if height > 1:
width -= 1
if args.w is not None:
width = args.w
if args.h is not None:
height = args.h

if not args.textfile.is_file():
print(f"cannot open {args.textfile.name}", file=stderr)
return 0

base = args.textfile.name
if base:
pass

outfile = ""
for n in count(1):
outfile = outdir / Path(f"{base}-{n}.lt")
if outfile.stat().st_size == 0:
break
cmd = ""
if width > 0:
cmd = f"LT_COLUMNS={width} {cmd}"
if height > 0:
cmd = f"LT_LINES={height} {cmd}"
cmd = f"{cmd} {lesstest} -s ./lt_screen -o {outfile} -- ".join(" ", argv)
if run(cmd).returncode:
print(f"error running {lesstest}", file=stderr)
return 1


def term_size():
stty = run(["stty", "-a"], capture_output=True, text=True).output
width = search(r"(rows|lines)\s+(\d+)", stty).group(0)
col = search(r"columns\s+(\d+)", stty).group(0)
return int(width), int(col)


if __name__ == "__main__":
exit(0 if main() else 1)
71 changes: 71 additions & 0 deletions lesstest/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python

import argparse
import sys
import subprocess
from pathlib import Path

lesstest = "./lesstest"
less = "../obj/less"
verbose = False


def main():
args = argparse.ArgumentParser(description="Run one or more test files")
args.add_argument('targets', metavar='file.lt | dir', type=Path, nargs='+')
args.add_argument('-v', '--verbose', action="store_true")
args.add_argument('-l', help="less.exe", type=Path)
args.add_argument('-o', help="arguments to be passed to lesstest",
nargs='+')
parsed = args.parse_args()

if parsed.l:
global less
less = parsed.l
global lesstest_opts
lesstest_opts = parsed.o.strip()
if lesstest_opts and lesstest_opts[0] != '-':
lesstest_opts = '-'+lesstest_opts
global verbose
verbose = parsed.v

errors = 0
for file in parsed.targets:
errors += run(file)
if errors > 0:
print(f"ERR {errors} errors", file=sys.stderr)
return 0

return 1


def run(file):
if file.is_dir():
return run_dir(file)
if file.suffix() != '.lt':
print(f'WARNING skipping {file.name}: not .lt file')
return 0
if not file.is_file():
print(f"ERR cannot open {file.name}", file=sys.stderr)
return 1

cmd = f'{lesstest} -s ./lt_screen -t {file.name} {lesstest_opts} {less}'
if verbose:
print(f"RUN {cmd}", file=sys.stderr)
if subprocess.run(cmd.split()).returncode:
print(f"ERR running {cmd}", file=sys.stderr)
return 1
return 0


def run_dir(dir):
if not dir.is_dir():
print(f"ERR cannot directory {dir.name}", file=sys.stderr)
return 1
errors = 0
errors += run(file for file in dir.iterdir())
return errors


if __name__ == '__main__':
sys.exit(0 if main() else 1)
36 changes: 19 additions & 17 deletions mkfuncs.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#!/usr/bin/env python

import fileinput
import re
from re import search

definition = ''
state = 0
params = 0

for line in fileinput.input():
if test := re.search(r'^\tpublic\s+(.*)', line):
definition = 'public ' + test.group(1)
state = 1
params = 0
elif (state == 1) and (test := re.search(r'(\w+)\s*\(', line)):
definition = '{0} LESSPARAMS (('.format(test.group(1))
state = 2
elif state == 2:
if re.search(r'^{', line):
if not params: definition += 'VOID_PARAM'
print(f'{definition}));')
state = 0
elif test := re.search(r'^\s*([^;]*)', line):
if (definition[-1:] != '('): definition += ', '
definition += test.group(1)
params = 1
if test := search(r'^\tpublic\s+(.*)', line):
definition = 'public ' + test.group(1)
state = 1
params = 0
elif (state == 1) and (test := search(r'(\w+)\s*\(', line)):
definition = f'{test.group(1)} LESSPARAMS (('
state = 2
elif state == 2:
if search(r'^{', line):
if not params:
definition += 'VOID_PARAM'
print(f'{definition}));')
state = 0
elif test := search(r'^\s*([^;]*)', line):
if (definition[-1] != '('):
definition += ', '
definition += test.group(1)
params = 1
62 changes: 29 additions & 33 deletions mkhelp.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
#!/usr/bin/env python

import time
import sys
from time import gmtime

time = time.gmtime()
print("/* This file was generated by mkhelp.py from less.hlp at "\
"%d:%02d GMT on %d/%d/%d */\n" %
(time.tm_hour, time.tm_min, time.tm_year, time.tm_mon, time.tm_mday))
print("#include \"less.h\"")
print("constant char helpdata[] = {")
ch = 0
while True:
prevch = ch
ch = sys.stdin.read(1)
if ch == '':
break
if (ch == "'"):
print("'\\'',", end='')
elif (ch == "\\"):
print("'\\\\',", end='')
elif (ch == "\b"):
print ("'\\b',", end='')
elif (ch == "\t"):
print ("'\\t',", end='')
elif (ch == "\n"):
if prevch != "\r": print("'\\n',")
elif (ch == "\r"):
if prevch != "\n": print("'\\n',")
else:
if ((ord(ch) >= ord(' ')) and (ord(ch) < 0x7f)):
print(f"'{ch}',", end='')
else:
print("0x%02x," % ord(ch), end='')
print(" '\\0' };")
print("constant int size_helpdata = sizeof(helpdata) - 1;")
time = gmtime()
with open('less.c', 'w', newline='') as out, open('less.hlp') as infile:
out.write("/* This file was generated by mkhelp.py from less.hlp at "
"%d:%02d on %d/%d/%d */\n" %
(time.tm_hour, time.tm_min, time.tm_year, time.tm_mon,
time.tm_mday))
out.write("#include \"less.h\"\n")
out.write("constant char helpdata[] = {\n")
for i, line in enumerate(infile):
for ch in line:
if ch == "'":
out.write("'\\'',")
elif ch == "\\":
out.write("'\\\\',")
elif ch == "\b":
out.write("'\\b',")
elif ch == "\t":
out.write("'\\t',")
elif ch == "\n":
if (ch == line[0] and i == 0) or (len(line) > 1):
out.write("'\\n',\n")
else:
if (ord(ch) >= ord(' ')) and (ord(ch) < 0x7f):
out.write(f"'{ch}',")
else:
out.write("0x%02x," % ord(ch))
out.write(" 0 };\n")
out.write("constant int size_helpdata = sizeof(helpdata) - 1;\n")
0 mkutable → mkutable.pl
100755 → 100644
File renamed without changes.
Loading