Commit e61f20f1 authored by Deomid "rojer" Ryabkov's avatar Deomid "rojer" Ryabkov Committed by Cesanta Bot

Convert amalgam.py to Python 3

No mongoose.{c,h} diffs

PUBLISHED_FROM=a995a6d6548a914087969e28908f4e8c9ef1d379
parent ca11f5ab
#!/usr/bin/env python #!/usr/bin/env python3
# #
# Generate a reversible amalgamation of several C source files # Generate a reversible amalgamation of several C source files
# along with their required internal headers. # along with their required internal headers.
...@@ -32,10 +32,10 @@ ...@@ -32,10 +32,10 @@
# #
import argparse import argparse
import io
import re import re
import sys import sys
import os import os
from StringIO import StringIO
parser = argparse.ArgumentParser(description='Produce an amalgamated source') parser = argparse.ArgumentParser(description='Produce an amalgamated source')
parser.add_argument('--prefix', default="NS", parser.add_argument('--prefix', default="NS",
...@@ -65,11 +65,11 @@ class File(object): ...@@ -65,11 +65,11 @@ class File(object):
def __init__(self, name, parent_name): def __init__(self, name, parent_name):
self.name = name self.name = name
self.parent_name = parent_name self.parent_name = parent_name
self.buf = StringIO() self.buf = io.StringIO()
emit_file(self.buf, self.name, self.parent_name) emit_file(self.buf, self.name, self.parent_name)
def emit(self): def emit(self):
print self.buf.getvalue(), print('%s' % self.buf.getvalue(), end='')
args = parser.parse_args() args = parser.parse_args()
...@@ -104,21 +104,20 @@ def resolve(path, parent_name): ...@@ -104,21 +104,20 @@ def resolve(path, parent_name):
break break
if os.path.exists(p) and not args.norel: if os.path.exists(p) and not args.norel:
p = os.path.realpath(p).replace('%s%s' % (os.getcwd(), os.sep), '') 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, '/') return p.replace(os.sep, '/')
def emit_line_directive(out, name, parent_name): 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" #line 1 "%(name)s"
#endif''' % dict( #endif''' % dict(
prefix = args.prefix, prefix = args.prefix,
name = resolve(name, parent_name), name = resolve(name, parent_name),
) ), file=out)
def emit_body(out, name, parent_name): def emit_body(out, name, parent_name):
resolved_name = resolve(name, parent_name) resolved_name = resolve(name, parent_name)
if not args.strict and not os.path.exists(resolved_name): if not args.strict and not os.path.exists(resolved_name):
print >>out, '#include "%s"' % (name,) print('#include "%s"' % name, file=out)
return return
with open(resolved_name) as f: with open(resolved_name) as f:
...@@ -130,7 +129,7 @@ def emit_body(out, name, parent_name): ...@@ -130,7 +129,7 @@ def emit_body(out, name, parent_name):
if re.match('\s*\*/$', l): if re.match('\s*\*/$', l):
in_comment = False in_comment = False
if not re.match('.*Copyright.*Cesanta', comment, re.M | re.S): if not re.match('.*Copyright.*Cesanta', comment, re.M | re.S):
print >>out, comment, out.write(comment)
continue continue
if re.match('/\*$', l): if re.match('/\*$', l):
...@@ -145,9 +144,9 @@ def emit_body(out, name, parent_name): ...@@ -145,9 +144,9 @@ def emit_body(out, name, parent_name):
if not should_ignore(path_to_include, parent_name): if not should_ignore(path_to_include, parent_name):
already_included.add(path_to_include) already_included.add(path_to_include)
includes.append(File(path_to_include, resolved_name)) includes.append(File(path_to_include, resolved_name))
print >>out, '/* Amalgamated: %s */' % (all,) print('/* Amalgamated: %s */' % all, file=out)
else: else:
print >>out, l, out.write(l)
def emit_file(out, name, parent_name): def emit_file(out, name, parent_name):
...@@ -174,17 +173,17 @@ if sys.platform == "win32": ...@@ -174,17 +173,17 @@ if sys.platform == "win32":
if args.license: if args.license:
with open(args.license) as f: with open(args.license) as f:
print f.read() print(f.read())
if args.public: if args.public:
print '#include "%s"' % (args.public) print('#include "%s"' % args.public)
for i in includes: for i in includes:
i.emit() i.emit()
if args.export: if args.export:
print '#ifndef %s_EXPORT_INTERNAL_HEADERS' % (args.prefix,) print('#ifndef %s_EXPORT_INTERNAL_HEADERS' % (args.prefix,))
for i in sources: for i in sources:
i.emit() i.emit()
if args.export: if args.export:
print '#endif /* %s_EXPORT_INTERNAL_HEADERS */' % (args.prefix,) print('#endif /* %s_EXPORT_INTERNAL_HEADERS */' % (args.prefix,))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment