nodeset_compiler.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # Remove blacklisted nodes from the nodeset
  119. # Doing this now ensures that unlinkable pointers will be cleanly removed
  120. # during sanitation.
  121. for blacklist in args.blacklistFiles:
  122. for line in blacklist.readlines():
  123. line = line.replace(" ", "")
  124. id = line.replace("\n", "")
  125. if ns.getNodeByIDString(id) is None:
  126. logger.info("Can't blacklist node, namespace does currently not contain a node with id " + str(id))
  127. else:
  128. ns.removeNodeById(line)
  129. blacklist.close()
  130. # Set the nodes from the ignore list to hidden. This removes them from dependency calculation
  131. # and from printing their generated code.
  132. # These nodes should be already pre-created on the server to avoid any errors during
  133. # creation.
  134. for ignoreFile in args.ignoreFiles:
  135. for line in ignoreFile.readlines():
  136. line = line.replace(" ", "")
  137. id = line.replace("\n", "")
  138. ns.hide_node(NodeId(id))
  139. #if not ns.hide_node(NodeId(id)):
  140. # logger.info("Can't ignore node, namespace does currently not contain a node with id " + str(id))
  141. ignoreFile.close()
  142. # Remove nodes that are not printable or contain parsing errors, such as
  143. # unresolvable or no references or invalid NodeIDs
  144. ns.sanitize()
  145. # Allocate/Parse the data values. In order to do this, we must have run
  146. # buidEncodingRules.
  147. ns.allocateVariables()
  148. ns.addInverseReferences()
  149. ns.setNodeParent()
  150. logger.info("Generating Code for Backend: {}".format(args.backend))
  151. if args.backend == "open62541":
  152. # Create the C code with the open62541 backend of the compiler
  153. from backend_open62541 import generateOpen62541Code
  154. generateOpen62541Code(ns, args.outputFile, args.internal_headers, args.typesArray)
  155. elif args.backend == "graphviz":
  156. from backend_graphviz import generateGraphvizCode
  157. generateGraphvizCode(ns, filename=args.outputFile)
  158. else:
  159. logger.error("Unsupported backend: {}".format(args.backend))
  160. exit(1)
  161. logger.info("NodeSet generation code successfully printed")