libuavcan_dsdlc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. #
  3. # UAVCAN DSDL compiler for libuavcan
  4. # Supported Python versions: 3.2+, 2.7.
  5. #
  6. # Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  7. #
  8. from __future__ import division, absolute_import, print_function, unicode_literals
  9. import os, sys, logging, argparse
  10. # This trickery allows to use the compiler even if pyuavcan is not installed in the system.
  11. # This is extremely important, as it makes the compiler (and therefore libuavcan in general)
  12. # totally dependency-free, except for the Python interpreter itself.
  13. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  14. LOCAL_PYUAVCAN_DIR = os.path.join(SCRIPT_DIR, 'pyuavcan')
  15. RUNNING_FROM_SRC_DIR = os.path.isdir(LOCAL_PYUAVCAN_DIR)
  16. if RUNNING_FROM_SRC_DIR:
  17. #print('Running from the source directory')
  18. sys.path.insert(0, SCRIPT_DIR)
  19. sys.path.insert(0, LOCAL_PYUAVCAN_DIR)
  20. def configure_logging(verbosity):
  21. fmt = '%(message)s'
  22. level = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG }.get(verbosity or 0, logging.DEBUG)
  23. logging.basicConfig(stream=sys.stderr, level=level, format=fmt)
  24. def die(text):
  25. print(text, file=sys.stderr)
  26. exit(1)
  27. DEFAULT_OUTDIR = 'dsdlc_generated'
  28. DESCRIPTION = '''UAVCAN DSDL compiler for libuavcan.
  29. Takes an input directory that contains an hierarchy of DSDL
  30. definitions and converts it into compatible hierarchy of C++ types for libuavcan.
  31. This script can be used directly from the source directory, no installation required!
  32. Supported Python versions: 3.2+, 2.7.
  33. '''
  34. argparser = argparse.ArgumentParser(description=DESCRIPTION)
  35. argparser.add_argument('source_dir', nargs='+', help='source directory with DSDL definitions')
  36. argparser.add_argument('--verbose', '-v', action='count', help='verbosity level (-v, -vv)')
  37. argparser.add_argument('--outdir', '-O', default=DEFAULT_OUTDIR, help='output directory, default %s' % DEFAULT_OUTDIR)
  38. argparser.add_argument('--incdir', '-I', default=[], action='append', help=
  39. '''nested type namespaces, one path per argument. Can be also specified through the environment variable
  40. UAVCAN_DSDL_INCLUDE_PATH, where the path entries are separated by colons ":"''')
  41. args = argparser.parse_args()
  42. configure_logging(args.verbose)
  43. try:
  44. extra_incdir = os.environ['UAVCAN_DSDL_INCLUDE_PATH'].split(':')
  45. logging.info('Additional include directories: %s', extra_incdir)
  46. args.incdir += extra_incdir
  47. except KeyError:
  48. pass
  49. from libuavcan_dsdl_compiler import run as dsdlc_run
  50. try:
  51. dsdlc_run(args.source_dir, args.incdir, args.outdir)
  52. except Exception as ex:
  53. logging.error('Compiler failure', exc_info=True)
  54. die(str(ex))