nodeset_compiler.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ### This Source Code Form is subject to the terms of the Mozilla Public
  4. ### License, v. 2.0. If a copy of the MPL was not distributed with this
  5. ### file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. ### Copyright 2014-2015 (c) TU-Dresden (Author: Chris Iatrou)
  7. ### Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  8. ### Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  9. import logging
  10. import argparse
  11. import sys
  12. from datatypes import NodeId
  13. from nodeset import *
  14. parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
  15. parser.add_argument('-e', '--existing',
  16. metavar="<existingNodeSetXML>",
  17. type=argparse.FileType('rb'),
  18. dest="existing",
  19. action='append',
  20. default=[],
  21. help='NodeSet XML files with nodes that are already present on the server.')
  22. parser.add_argument('-x', '--xml',
  23. metavar="<nodeSetXML>",
  24. type=argparse.FileType('rb'),
  25. action='append',
  26. dest="infiles",
  27. default=[],
  28. help='NodeSet XML files with nodes that shall be generated.')
  29. parser.add_argument('outputFile',
  30. metavar='<outputFile>',
  31. help='The path/basename for the <output file>.c and <output file>.h files to be generated. This will also be the function name used in the header and c-file.')
  32. parser.add_argument('--internal-headers',
  33. action='store_true',
  34. dest="internal_headers",
  35. help='Include internal headers instead of amalgamated header')
  36. parser.add_argument('-b', '--blacklist',
  37. metavar="<blacklistFile>",
  38. type=argparse.FileType('r'),
  39. action='append',
  40. dest="blacklistFiles",
  41. default=[],
  42. help='Loads a list of NodeIDs stored in blacklistFile (one NodeID per line). Any of the nodeIds encountered in this file will be removed from the nodeset prior to compilation. Any references to these nodes will also be removed')
  43. parser.add_argument('-i', '--ignore',
  44. metavar="<ignoreFile>",
  45. type=argparse.FileType('r'),
  46. action='append',
  47. dest="ignoreFiles",
  48. default=[],
  49. help='Loads a list of NodeIDs stored in ignoreFile (one NodeID per line). Any of the nodeIds encountered in this file will be kept in the nodestore but not printed in the generated code')
  50. parser.add_argument('-t', '--types-array',
  51. metavar="<typesArray>",
  52. action='append',
  53. type=str,
  54. dest="typesArray",
  55. default=[],
  56. help='Types array for the given namespace. Can be used mutliple times to define (in the same order as the .xml files, first for --existing, then --xml) the type arrays')
  57. parser.add_argument('-v', '--verbose', action='count',
  58. default=1,
  59. help='Make the script more verbose. Can be applied up to 4 times')
  60. parser.add_argument('--backend',
  61. default='open62541',
  62. const='open62541',
  63. nargs='?',
  64. choices=['open62541', 'graphviz'],
  65. help='Backend for the output files (default: %(default)s)')
  66. args = parser.parse_args()
  67. # Set up logging
  68. # By default logging outputs to stderr. We want to redirect it to stdout, otherwise build output from cmake
  69. # is in stdout and nodeset compiler in stderr
  70. logging.basicConfig(stream=sys.stdout)
  71. logger = logging.getLogger(__name__)
  72. logger.setLevel(logging.INFO)
  73. verbosity = 0
  74. if args.verbose:
  75. verbosity = int(args.verbose)
  76. if (verbosity == 1):
  77. logging.basicConfig(level=logging.ERROR)
  78. elif (verbosity == 2):
  79. logging.basicConfig(level=logging.WARNING)
  80. elif (verbosity == 3):
  81. logging.basicConfig(level=logging.INFO)
  82. elif (verbosity >= 4):
  83. logging.basicConfig(level=logging.DEBUG)
  84. else:
  85. logging.basicConfig(level=logging.CRITICAL)
  86. # Set up logging
  87. logger = logging.getLogger(__name__)
  88. # Create a new nodeset. The nodeset name is not significant.
  89. # Parse the XML files
  90. ns = NodeSet()
  91. nsCount = 0
  92. loadedFiles = list()
  93. def getTypesArray(nsIdx):
  94. if nsIdx < len(args.typesArray):
  95. return args.typesArray[nsIdx]
  96. else:
  97. return "UA_TYPES"
  98. for xmlfile in args.existing:
  99. if xmlfile.name in loadedFiles:
  100. logger.info("Skipping Nodeset since it is already loaded: {} ".format(xmlfile.name))
  101. continue
  102. loadedFiles.append(xmlfile.name)
  103. logger.info("Preprocessing (existing) " + str(xmlfile.name))
  104. ns.addNodeSet(xmlfile, True, typesArray=getTypesArray(nsCount))
  105. nsCount +=1
  106. for xmlfile in args.infiles:
  107. if xmlfile.name in loadedFiles:
  108. logger.info("Skipping Nodeset since it is already loaded: {} ".format(xmlfile.name))
  109. continue
  110. loadedFiles.append(xmlfile.name)
  111. logger.info("Preprocessing " + str(xmlfile.name))
  112. ns.addNodeSet(xmlfile, typesArray=getTypesArray(nsCount))
  113. nsCount +=1
  114. # # We need to notify the open62541 server of the namespaces used to be able to use i.e. ns=3
  115. # namespaceArrayNames = preProc.getUsedNamespaceArrayNames()
  116. # for key in namespaceArrayNames:
  117. # ns.addNamespace(key, namespaceArrayNames[key])
  118. # Set the nodes from the ignore list to hidden. This removes them from dependency calculation
  119. # and from printing their generated code.
  120. # These nodes should be already pre-created on the server to avoid any errors during
  121. # creation.
  122. for ignoreFile in args.ignoreFiles:
  123. for line in ignoreFile.readlines():
  124. line = line.replace(" ", "")
  125. id = line.replace("\n", "")
  126. ns.hide_node(NodeId(id))
  127. #if not ns.hide_node(NodeId(id)):
  128. # logger.info("Can't ignore node, namespace does currently not contain a node with id " + str(id))
  129. ignoreFile.close()
  130. # Remove nodes that are not printable or contain parsing errors, such as
  131. # unresolvable or no references or invalid NodeIDs
  132. ns.sanitize()
  133. # Allocate/Parse the data values. In order to do this, we must have run
  134. # buidEncodingRules.
  135. ns.allocateVariables()
  136. ns.addInverseReferences()
  137. # Remove blacklisted nodes from the nodeset.
  138. # We need to have the inverse references here to ensure the reference is deleted from the referencing node too
  139. if args.blacklistFiles:
  140. for blacklist in args.blacklistFiles:
  141. for line in blacklist.readlines():
  142. if line.startswith("#"):
  143. continue
  144. line = line.replace(" ", "")
  145. id = line.replace("\n", "")
  146. if len(id) == 0:
  147. continue
  148. n = ns.getNodeByIDString(id)
  149. if n is None:
  150. logger.debug("Can't blacklist node, namespace does currently not contain a node with id " + str(id))
  151. else:
  152. ns.remove_node(n)
  153. blacklist.close()
  154. ns.sanitize()
  155. ns.setNodeParent()
  156. logger.info("Generating Code for Backend: {}".format(args.backend))
  157. if args.backend == "open62541":
  158. # Create the C code with the open62541 backend of the compiler
  159. from backend_open62541 import generateOpen62541Code
  160. generateOpen62541Code(ns, args.outputFile, args.internal_headers, args.typesArray)
  161. elif args.backend == "graphviz":
  162. from backend_graphviz import generateGraphvizCode
  163. generateGraphvizCode(ns, filename=args.outputFile)
  164. else:
  165. logger.error("Unsupported backend: {}".format(args.backend))
  166. exit(1)
  167. logger.info("NodeSet generation code successfully printed")