From e61f20f1ed9c4730d8a04489d14af61887601546 Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Sat, 2 May 2020 21:46:40 +0100 Subject: [PATCH] Convert amalgam.py to Python 3 No mongoose.{c,h} diffs PUBLISHED_FROM=a995a6d6548a914087969e28908f4e8c9ef1d379 --- tools/amalgam.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tools/amalgam.py b/tools/amalgam.py index 3d40d4b2..0c931d8e 100755 --- a/tools/amalgam.py +++ b/tools/amalgam.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Generate a reversible amalgamation of several C source files # along with their required internal headers. @@ -32,10 +32,10 @@ # import argparse +import io import re import sys import os -from StringIO import StringIO parser = argparse.ArgumentParser(description='Produce an amalgamated source') parser.add_argument('--prefix', default="NS", @@ -65,11 +65,11 @@ class File(object): def __init__(self, name, parent_name): self.name = name self.parent_name = parent_name - self.buf = StringIO() + self.buf = io.StringIO() emit_file(self.buf, self.name, self.parent_name) def emit(self): - print self.buf.getvalue(), + print('%s' % self.buf.getvalue(), end='') args = parser.parse_args() @@ -104,21 +104,20 @@ def resolve(path, parent_name): break if os.path.exists(p) and not args.norel: p = os.path.realpath(p).replace('%s%s' % (os.getcwd(), os.sep), '') - # print >>sys.stderr, '%s %s -> %s (cwd %s)' % (path, parent_name, p, os.getcwd()) return p.replace(os.sep, '/') def emit_line_directive(out, name, parent_name): - print >>out, '''#ifdef %(prefix)s_MODULE_LINES + print ('''#ifdef %(prefix)s_MODULE_LINES #line 1 "%(name)s" #endif''' % dict( prefix = args.prefix, name = resolve(name, parent_name), -) +), file=out) def emit_body(out, name, parent_name): resolved_name = resolve(name, parent_name) if not args.strict and not os.path.exists(resolved_name): - print >>out, '#include "%s"' % (name,) + print('#include "%s"' % name, file=out) return with open(resolved_name) as f: @@ -130,7 +129,7 @@ def emit_body(out, name, parent_name): if re.match('\s*\*/$', l): in_comment = False if not re.match('.*Copyright.*Cesanta', comment, re.M | re.S): - print >>out, comment, + out.write(comment) continue if re.match('/\*$', l): @@ -145,9 +144,9 @@ def emit_body(out, name, parent_name): if not should_ignore(path_to_include, parent_name): already_included.add(path_to_include) includes.append(File(path_to_include, resolved_name)) - print >>out, '/* Amalgamated: %s */' % (all,) + print('/* Amalgamated: %s */' % all, file=out) else: - print >>out, l, + out.write(l) def emit_file(out, name, parent_name): @@ -174,17 +173,17 @@ if sys.platform == "win32": if args.license: with open(args.license) as f: - print f.read() + print(f.read()) if args.public: - print '#include "%s"' % (args.public) + print('#include "%s"' % args.public) for i in includes: i.emit() if args.export: - print '#ifndef %s_EXPORT_INTERNAL_HEADERS' % (args.prefix,) + print('#ifndef %s_EXPORT_INTERNAL_HEADERS' % (args.prefix,)) for i in sources: i.emit() if args.export: - print '#endif /* %s_EXPORT_INTERNAL_HEADERS */' % (args.prefix,) + print('#endif /* %s_EXPORT_INTERNAL_HEADERS */' % (args.prefix,))