diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-16 10:54:16 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-16 10:54:16 +0100 |
commit | bda2f709646f80dc82e52cd664d17cd41e8b2b6a (patch) | |
tree | 98da1f4bedc61563c9abc905da6aa02467a61a7b /tools | |
parent | 8a919fb051361e422817491c56e9096143b5fe7d (diff) | |
parent | 1452221aca8851df029702e8a71be339005bd5fd (diff) |
Merge pull request #498 from lurch/create-headers-from-files
Auto-generate the stmhal/pybcdc_inf header file from static files
Diffstat (limited to 'tools')
-rw-r--r-- | tools/file2h.py | 28 | ||||
-rw-r--r-- | tools/insert-usb-ids.py | 36 |
2 files changed, 64 insertions, 0 deletions
diff --git a/tools/file2h.py b/tools/file2h.py new file mode 100644 index 000000000..fb5bd1a4b --- /dev/null +++ b/tools/file2h.py @@ -0,0 +1,28 @@ +# Reads in a text file, and performs the necessary escapes so that it +# can be #included as a static string like: +# static const char string_from_textfile[] = +# #include "build/textfile.h" +# ; +# This script simply prints the escaped string straight to stdout + +from __future__ import print_function + +import sys + +# Can either be set explicitly, or left blank to auto-detect +line_end = '' + +if __name__ == "__main__": + filename = sys.argv[1] + for line in open(filename, 'r').readlines(): + if not line_end: + for ending in ('\r\n', '\r', '\n'): + if line.endswith(ending): + line_end = ending.replace('\r', '\\r').replace('\n', '\\n') + break + if not line_end: + raise Exception("Couldn't auto-detect line-ending of %s" % filename) + line = line.rstrip('\r\n') + line = line.replace('\\', '\\\\') + line = line.replace('"', '\\"') + print('"%s%s"' % (line, line_end)) diff --git a/tools/insert-usb-ids.py b/tools/insert-usb-ids.py new file mode 100644 index 000000000..af010dad9 --- /dev/null +++ b/tools/insert-usb-ids.py @@ -0,0 +1,36 @@ +# Reads the USB VID and PID from the file specifed by sys.arg[1] and then +# inserts those values into the template file specified by sys.argv[2], +# printing the result to stdout + +from __future__ import print_function + +import sys +import re +import string + +def parse_usb_ids(filename): + rv = dict() + if filename == 'usbd_desc_cdc_msc.c': + for line in open(filename).readlines(): + line = line.rstrip('\r\n') + match = re.match('^#define\s+(\w+)\s+0x(\d+)$', line) + if match: + if match.group(1) == 'USBD_VID': + rv['USB_VID'] = match.group(2) + elif match.group(1) == 'USBD_PID': + rv['USB_PID'] = match.group(2) + if 'USB_VID' in rv and 'USB_PID' in rv: + break + else: + raise Exception("Don't (yet) know how to parse USB IDs from %s" % filename) + for k in ('USB_PID', 'USB_VID'): + if k not in rv: + raise Exception("Unable to parse %s from %s" % (k, filename)) + return rv + +if __name__ == "__main__": + usb_ids_file = sys.argv[1] + template_file = sys.argv[2] + replacements = parse_usb_ids(usb_ids_file) + for line in open(template_file, 'r').readlines(): + print(string.Template(line).safe_substitute(replacements), end='') |