aboutsummaryrefslogtreecommitdiff
path: root/tools/amalgamate.py
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2017-02-12 17:12:52 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2017-02-12 17:12:52 +0200
commitcd2faf64b2e995dee42e7e3911325d372c6604a1 (patch)
treee0ab608cdd7005e759b570a5a4ec2031191bafb5 /tools/amalgamate.py
parent54f78d73ec378fe1d62a696edeb2ddc5a59e2669 (diff)
downloadmqtt-cd2faf64b2e995dee42e7e3911325d372c6604a1.tar.gz
mqtt-cd2faf64b2e995dee42e7e3911325d372c6604a1.zip
Add all the stuff
Diffstat (limited to 'tools/amalgamate.py')
-rw-r--r--tools/amalgamate.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/tools/amalgamate.py b/tools/amalgamate.py
new file mode 100644
index 0000000..a807603
--- /dev/null
+++ b/tools/amalgamate.py
@@ -0,0 +1,90 @@
+import io
+import os
+import sys
+
+this_dir = os.path.dirname(os.path.abspath(__file__))
+
+src_dir = os.path.join(this_dir, '..', 'src')
+
+sources = (
+ 'queue.h',
+ 'log.h',
+ 'misc.c',
+ 'stringbuf.c',
+ 'stream.c',
+ 'socketstream.c',
+ 'stringstream.c',
+ 'stream_mqtt.c',
+ 'socket.c',
+ 'packet.c',
+ 'serialize.c',
+ 'deserialize.c',
+ 'client.c'
+)
+
+
+def is_header(filename):
+ return os.path.splitext(filename)[1] == '.h'
+
+
+def get_header(src):
+ root, ext = os.path.splitext(src)
+ return root + '.h'
+
+
+def read_file(filename):
+ def tounicode(s):
+ if sys.version_info[0] == 2:
+ return s.decode('utf-8')
+ else:
+ return s
+ with open(filename, 'r') as fp:
+ buf = io.StringIO()
+ for line in fp:
+ if line.startswith('#include "'):
+ if line[10:].startswith('mqtt.h'):
+ pass
+ else:
+ continue
+ buf.write(tounicode(line))
+ return buf.getvalue()
+
+
+def file_header(filename):
+ filename = os.path.basename(filename)
+ # how long lines we create
+ linelen = 72
+ # how much space left after the necessary comment markup
+ chars = linelen - 4
+ # how much padding in total for filename
+ padding = chars - len(filename)
+ padding_l = padding // 2
+ padding_r = padding - padding_l
+ lines = (
+ '',
+ '/*' + '*'*chars + '*/',
+ '/*' + ' '*padding_l + filename + ' '*padding_r + '*/',
+ '/*' + '*'*chars + '*/',
+ '\n',
+ )
+ return '\n'.join(lines)
+
+
+def write_file(output, srcfilename):
+ output.write(file_header(srcfilename))
+ output.write(read_file(srcfilename))
+
+
+output_filename = sys.argv[1]
+
+with open(output_filename, 'w') as out:
+ for source in sources:
+ path = os.path.join(src_dir, source)
+
+ if is_header(path):
+ write_file(out, path)
+ else:
+ header = get_header(path)
+ if os.path.isfile(header):
+ write_file(out, header)
+ write_file(out, path)