summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexis Hildebrandt <afh@surryhill.net>2023-12-04 12:23:56 +0100
committerAlexis Hildebrandt <afh@surryhill.net>2023-12-04 12:23:56 +0100
commit3d22ddc7e67a06d0bdf2994b1ef018bc46e92f34 (patch)
tree287e867ef25bf2ecbaa9364f257aefdfb23ef97f
parent8bbd3fed06087243861a6d6ec9c58dd8858ab34c (diff)
downloadfork-ledger-3d22ddc7e67a06d0bdf2994b1ef018bc46e92f34.tar.gz
fork-ledger-3d22ddc7e67a06d0bdf2994b1ef018bc46e92f34.tar.bz2
fork-ledger-3d22ddc7e67a06d0bdf2994b1ef018bc46e92f34.zip
Migrate Python scripts to Python 3
-rwxr-xr-xacprep2
-rwxr-xr-xcontrib/getquote-uk.py13
-rwxr-xr-xcontrib/ledger-du32
-rwxr-xr-xpython/demo.py2
-rwxr-xr-xtest/CheckBaselineTests.py3
-rw-r--r--test/CheckComments.py46
-rwxr-xr-xtest/CheckManpage.py3
-rwxr-xr-xtest/CheckOptions.py19
-rwxr-xr-xtest/CheckTexinfo.py3
-rwxr-xr-xtest/ConfirmTests.py10
-rwxr-xr-xtest/DocTests.py3
-rwxr-xr-xtest/GenerateTests.py2
-rwxr-xr-xtest/LedgerHarness.py3
-rwxr-xr-xtest/RegressTests.py3
-rwxr-xr-xtest/convert.py2
-rw-r--r--test/python/JournalTest.py2
-rw-r--r--test/python/PostingTest.py2
-rw-r--r--test/python/TransactionTest.py2
-rw-r--r--test/regress/B21BF389.py3
-rwxr-xr-xtools/average6
-rwxr-xr-xtools/genuuid29
21 files changed, 96 insertions, 94 deletions
diff --git a/acprep b/acprep
index c6665e24..ec1576f0 100755
--- a/acprep
+++ b/acprep
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# acprep, version 3.1
#
diff --git a/contrib/getquote-uk.py b/contrib/getquote-uk.py
index a69d4e7d..0c6c052a 100755
--- a/contrib/getquote-uk.py
+++ b/contrib/getquote-uk.py
@@ -1,7 +1,6 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
-import urllib, string, sys
+import urllib, string, sys, os
def download(sym):
url = "http://uk.old.finance.yahoo.com/d/quotes.csv?s="
@@ -13,11 +12,13 @@ def download(sym):
result = float(fields[1])/100
return result
-
+if len(sys.argv) == 1:
+ print(f'USAGE: {os.path.basename(__file__)} SYMBOL', file=sys.stderr)
+ sys.exit(-1)
sym = sys.argv[1]
sym = sym.replace('_', '.')
if sym == '£':
- print '£1.00'
+ print('£1.00')
else:
- try: print "£" +str(download(sym))
+ try: print(f'£ {str(download(sym))}')
except: pass
diff --git a/contrib/ledger-du b/contrib/ledger-du
index 580e916e..fe5a0706 100755
--- a/contrib/ledger-du
+++ b/contrib/ledger-du
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
import string
import sys
@@ -9,28 +9,28 @@ from stat import *
from os.path import *
def report_file(path):
- dir_elems = string.split(dirname(path), os.sep)
+ dir_elems = dirname(path).split(os.sep)
if dir_elems[0] == "." or dir_elems[0] == "":
- dir_elems = dir_elems[1 :]
- account = string.join(dir_elems, ":")
+ dir_elems = dir_elems[1 :]
+ account = ":".join(dir_elems)
info = os.stat(path)
- print time.strftime("%Y/%m/%d", time.localtime(info[ST_MTIME])),
+ print(time.strftime("%Y/%m/%d", time.localtime(info[ST_MTIME])))
- print basename(path)
- print " ", account, " ", info[ST_SIZE], "b"
- print " Equity:Files"
- print
+ print(f'''{basename(path)}
+ \t{account} {info[ST_SIZE]}b
+ \tEquity:Files
+ ''')
def find_files(path):
xacts = os.listdir(path)
for xact in xacts:
xact = join(path, xact)
- if not islink(xact):
- if isdir(xact) and xact != "/proc":
- find_files(xact)
- else:
- report_file(xact)
+ if not islink(xact):
+ if isdir(xact) and xact != "/proc":
+ find_files(xact)
+ else:
+ report_file(xact)
args = sys.argv[1:]
if len(args):
@@ -38,12 +38,12 @@ if len(args):
else:
paths = ["."]
-print """
+print("""
C 1.00 Kb = 1024 b
C 1.00 Mb = 1024 Kb
C 1.00 Gb = 1024 Mb
C 1.00 Tb = 1024 Gb
-"""
+""")
for path in paths:
find_files(path)
diff --git a/python/demo.py b/python/demo.py
index b3fe1d04..2102afa5 100755
--- a/python/demo.py
+++ b/python/demo.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
import sys
from datetime import datetime
diff --git a/test/CheckBaselineTests.py b/test/CheckBaselineTests.py
index fa9fa2bc..9f52dd10 100755
--- a/test/CheckBaselineTests.py
+++ b/test/CheckBaselineTests.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import sys
import re
diff --git a/test/CheckComments.py b/test/CheckComments.py
index 446137b0..9bcfcbb3 100644
--- a/test/CheckComments.py
+++ b/test/CheckComments.py
@@ -1,20 +1,19 @@
+#!/usr/bin/env python3
+
import sys
import re
import os
+import argparse
-ok = 100.0
-if sys.argv[1] == '-l':
- ok = float(sys.argv[2])
- sys.argv = [sys.argv[0]] + sys.argv[3:]
-
-debug = False
-if sys.argv[1] == '-v':
- debug = True
- sys.argv = [sys.argv[0]] + sys.argv[2:]
+parser = argparse.ArgumentParser( prog='CheckComments')
+parser.add_argument('-v', '--debug', dest='debug', action='store_true')
+parser.add_argument('-l', '--limit', dest='ok', type=float, default=100.0)
+parser.add_argument('path', nargs='+', help='Path to source file to check comments')
+args = parser.parse_args()
errors = 0
-for path in sys.argv[1:]:
+for path in args.path:
other_depth = 0
brace_depth = 0
code_count = 0
@@ -26,6 +25,7 @@ for path in sys.argv[1:]:
ctor_dtor = False
linenum = 0
+ print(f'checking {path}')
fd = open(path, 'r')
for line in fd.readlines():
linenum += 1
@@ -48,48 +48,48 @@ for path in sys.argv[1:]:
match = re.search('(namespace|enum|class|struct|union)', line)
if match:
kind = match.group(1)
- if debug: print "kind =", kind
+ if args.debug: print("kind =", kind)
elif kind == "function":
match = re.search('(\S+)\(', line)
if match:
function_name = match.group(1)
long_comments[function_name] = comment_count
comment_count = 0
- if debug: print "name found %s" % function_name
+ if args.debug: print("name found %s" % function_name)
if re.search('{', line) and not re.search('@{', line):
if kind == "function":
brace_depth += 1
- if debug: print "brace_depth =", brace_depth
+ if args.debug: print("brace_depth =", brace_depth)
else:
other_depth += 1
kind = "function"
- if debug: print "other_depth =", other_depth
+ if args.debug: print("other_depth =", other_depth)
if re.search('}', line) and not re.search('@}', line):
if brace_depth > 0:
brace_depth -= 1
- if debug: print "brace_depth =", brace_depth
+ if args.debug: print("brace_depth =", brace_depth)
if brace_depth == 0:
- if debug: print "function done"
+ if args.debug: print("function done")
if function_name in long_comments:
comment_count += long_comments[function_name]
if code_count == 0:
- percent = ok
- print "%7s %4d/%4d %s:%d: %s" % \
+ percent = args.ok
+ print("%7s %4d/%4d %s:%d: %s" % \
("empty", comment_count, code_count,
os.path.basename(path), linenum,
- function_name)
+ function_name))
errors += 1
else:
percent = 100.0 * (float(comment_count) /
float(code_count))
- if percent < ok and not ctor_dtor:
- print "%6.0f%% %4d/%4d %s:%d: %s" % \
+ if percent < args.ok and not ctor_dtor:
+ print("%6.0f%% %4d/%4d %s:%d: %s" % \
(percent, comment_count, code_count,
os.path.basename(path), linenum,
- function_name)
+ function_name))
errors += 1
code_count = 0
comment_count = 0
@@ -98,7 +98,7 @@ for path in sys.argv[1:]:
ctor_dtor = False
else:
other_depth -= 1
- if debug: print "other_depth =", other_depth
+ if args.debug: print("other_depth =", other_depth)
if brace_depth > 0:
if re.search("TRACE_[CD]TOR", line):
diff --git a/test/CheckManpage.py b/test/CheckManpage.py
index 3da9d872..e519a00a 100755
--- a/test/CheckManpage.py
+++ b/test/CheckManpage.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import sys
import re
diff --git a/test/CheckOptions.py b/test/CheckOptions.py
index 3f08fb0d..faf1630e 100755
--- a/test/CheckOptions.py
+++ b/test/CheckOptions.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import re
import os
@@ -40,7 +39,7 @@ class CheckOptions (object):
for source_file in ['session', 'report']:
command.append(os.path.join(self.source, 'src', '%s.cc' % source_file))
try:
- output = subprocess.check_output(command).split('\n');
+ output = subprocess.check_output(command, universal_newlines=True).split('\n')
except subprocess.CalledProcessError:
output = ''
@@ -58,7 +57,7 @@ class CheckOptions (object):
def ledger_functions(self):
command = shlex.split('grep --no-filename fn_ %s' % (os.path.join(self.source, 'src', 'report.h')))
try:
- output = subprocess.check_output(command).split('\n');
+ output = subprocess.check_output(command, universal_newlines=True).split('\n');
except subprocess.CalledProcessError:
output = ''
@@ -74,7 +73,13 @@ class CheckOptions (object):
else:
options.remove(option)
known_alternates = self.find_alternates()
- self.unknown_options = {option for option in options if option not in known_alternates}
+ self.unknown_options = options - known_alternates
+ self.missing_options -= {
+ # The option --explicit is a no-op as of March 2020 and is
+ # therefore intentionally undocumented.
+ # For details see commit 43b07fbab3b4c144eca4a771524e59c531ffa074
+ 'explicit'
+ }
functions = self.find_functions(self.source_file)
for function in self.ledger_functions():
@@ -82,8 +87,8 @@ class CheckOptions (object):
self.missing_functions.add(function)
else:
functions.remove(function)
- known_functions = ['tag', 'has_tag', 'meta', 'has_meta']
- self.unknown_functions = {function for function in functions if function not in known_functions}
+ known_functions = {'tag', 'has_tag', 'meta', 'has_meta'}
+ self.unknown_functions = functions - known_functions
if len(self.missing_options):
print("Missing %s option entries for:%s%s\n" % (self.source_type, self.sep, self.sep.join(sorted(list(self.missing_options)))))
diff --git a/test/CheckTexinfo.py b/test/CheckTexinfo.py
index fa709e1b..82c7a286 100755
--- a/test/CheckTexinfo.py
+++ b/test/CheckTexinfo.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import sys
import re
diff --git a/test/ConfirmTests.py b/test/ConfirmTests.py
index e82479ed..0dc2b9f5 100755
--- a/test/ConfirmTests.py
+++ b/test/ConfirmTests.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
# This script confirms both that the register report "adds up", and that its
# final balance is the same as what the balance report shows.
@@ -56,8 +56,8 @@ def confirm_report(command):
if re.search(' -[VGB] ', command) and diff < 0.015:
diff = 0.0
if diff > 0.001:
- print("DISCREPANCY: %.3f (%.3f - %.3f) at line %d:" % \)
- (running_total - total, running_total, total, index)
+ print("DISCREPANCY: %.3f (%.3f - %.3f) at line %d:" % \
+ (running_total - total, running_total, total, index))
print(line,)
running_total = total
failure = True
@@ -78,8 +78,8 @@ def confirm_report(command):
diff = 0.0
if diff > 0.001:
print()
- print("DISCREPANCY: %.3f (%.3f - %.3f) between register and balance" % \)
- (balance_total - running_total, balance_total, running_total)
+ print("DISCREPANCY: %.3f (%.3f - %.3f) between register and balance" % \
+ (balance_total - running_total, balance_total, running_total))
print(last_line,)
failure = True
diff --git a/test/DocTests.py b/test/DocTests.py
index daecdd90..e12a4ae4 100755
--- a/test/DocTests.py
+++ b/test/DocTests.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
from io import open
diff --git a/test/GenerateTests.py b/test/GenerateTests.py
index 2b966e35..9bd4a7bc 100755
--- a/test/GenerateTests.py
+++ b/test/GenerateTests.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
# This script confirms both that the register report "adds up", and that its
# final balance is the same as what the balance report shows.
diff --git a/test/LedgerHarness.py b/test/LedgerHarness.py
index 5051fc8b..0da32e8a 100755
--- a/test/LedgerHarness.py
+++ b/test/LedgerHarness.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import sys
import os
diff --git a/test/RegressTests.py b/test/RegressTests.py
index 849c4137..a3998f1f 100755
--- a/test/RegressTests.py
+++ b/test/RegressTests.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
from io import open
diff --git a/test/convert.py b/test/convert.py
index 85e52701..a54edaac 100755
--- a/test/convert.py
+++ b/test/convert.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# convert.py: This script converts a Boost.Test unit test into an
# equivalent Python unit test.
diff --git a/test/python/JournalTest.py b/test/python/JournalTest.py
index 84ad2f43..59442968 100644
--- a/test/python/JournalTest.py
+++ b/test/python/JournalTest.py
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import unittest
diff --git a/test/python/PostingTest.py b/test/python/PostingTest.py
index 0dd18112..f6b9bc04 100644
--- a/test/python/PostingTest.py
+++ b/test/python/PostingTest.py
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import unittest
import operator
diff --git a/test/python/TransactionTest.py b/test/python/TransactionTest.py
index c87f8c4b..12069389 100644
--- a/test/python/TransactionTest.py
+++ b/test/python/TransactionTest.py
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import unittest
import operator
diff --git a/test/regress/B21BF389.py b/test/regress/B21BF389.py
index 11208e91..e130b7b3 100644
--- a/test/regress/B21BF389.py
+++ b/test/regress/B21BF389.py
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
import sys
import ledger
diff --git a/tools/average b/tools/average
index 0e95c1c5..4c1e4b43 100755
--- a/tools/average
+++ b/tools/average
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
import getopt
import time
@@ -18,9 +18,9 @@ length = 0.0
i = 0
while i < count:
begin = time.time()
- cmd = '"' + string.join(args, '" "') + '"';
+ cmd = '"' + '" "'.join(args) + '"';
os.system(cmd)
length += time.time() - begin
i += 1
-print >> sys.stderr, length / count
+print(length / count, file=sys.stderr)
diff --git a/tools/genuuid b/tools/genuuid
index 53fb7a0a..0ad5bd92 100755
--- a/tools/genuuid
+++ b/tools/genuuid
@@ -1,24 +1,27 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
import re
import sys
def scan_path(path):
bug = uuid = None
- with open(path, 'r') as fd:
- for line in fd:
- match = re.match('\*', line)
- if match:
- bug = uuid = None
+ try:
+ with open(path, 'r') as fd:
+ for line in fd:
+ match = re.match('\*', line)
+ if match:
+ bug = uuid = None
- match = re.search('\[\[bug:([0-9]+)\]\[#[0-9]+\]\]', line)
- if match:
- bug = match.group(1)
- elif bug:
- match = re.search(':ID:\s+(.+?)\s*$', line)
+ match = re.search('\[\[bug:([0-9]+)\]\[#[0-9]+\]\]', line)
if match:
- uuid = match.group(1)
- print "UPDATE bugs SET cf_uuid='%s' WHERE bug_id=%s;" % (uuid, bug)
+ bug = match.group(1)
+ elif bug:
+ match = re.search(':ID:\s+(.+?)\s*$', line)
+ if match:
+ uuid = match.group(1)
+ print(f"UPDATE bugs SET cf_uuid='{uuid}' WHERE bug_id={bug};")
+ except FileNotFoundError:
+ print(f'{path}: No such file or directory')
scan_path('/Users/johnw/src/ledger/plan/TODO')
scan_path('/Users/johnw/src/ledger/plan/TODO-3.0')