awlsim-proupgrade 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Project file upgrade utility
  5. #
  6. # Copyright 2017-2020 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import sys
  24. import os
  25. import getopt
  26. from awlsim_loader.common import *
  27. from awlsim.gui.util import *
  28. from awlsim.gui.fup.fupwidget import FupFactory, FupWidget
  29. def usage():
  30. print("awlsim-proupgrade version %s" % VERSION_STRING)
  31. print("")
  32. print("This utility re-writes a project file using the latest file format version.")
  33. print("")
  34. print("Usage: awlsim-proupgrade [OPTIONS] PROJECTFILE.awlpro ...")
  35. print("")
  36. print("Options:")
  37. print(" -u|--gen-uuids Regenerate all UUIDs.")
  38. print(" -L|--loglevel LVL Set the client log level:")
  39. print(" 0: Log nothing")
  40. print(" 1: Log errors")
  41. print(" 2: Log errors and warnings (default)")
  42. print(" 3: Log errors, warnings and info messages")
  43. print(" 4: Verbose logging")
  44. print(" 5: Extremely verbose logging")
  45. def main():
  46. opt_genUUIDs = False
  47. opt_loglevel = Logging.LOG_WARNING
  48. Logging.setPrefix("awlsim-proupgrade: ")
  49. try:
  50. (opts, args) = getopt.getopt(sys.argv[1:],
  51. "huL:",
  52. [ "help", "gen-uuids", "loglevel=", ])
  53. except getopt.GetoptError as e:
  54. printError(str(e))
  55. usage()
  56. return ExitCodes.EXIT_ERR_CMDLINE
  57. for (o, v) in opts:
  58. if o in ("-h", "--help"):
  59. usage()
  60. return ExitCodes.EXIT_OK
  61. if o in ("-u", "--gen-uuids"):
  62. opt_genUUIDs = True
  63. if o in ("-L", "--loglevel"):
  64. try:
  65. opt_loglevel = int(v)
  66. except ValueError:
  67. printError("-L|--loglevel: Invalid log level")
  68. sys.exit(1)
  69. if len(args) < 1:
  70. usage()
  71. return ExitCodes.EXIT_ERR_CMDLINE
  72. projectFiles = args
  73. try:
  74. Logging.setLoglevel(opt_loglevel)
  75. for projectFile in projectFiles:
  76. # Parse the project file and write it back immediately.
  77. # The read will parse old formats, too.
  78. # The write will write using the latest format.
  79. printInfo("Upgrading file format of: %s" % projectFile)
  80. project = Project.fromFile(projectFile)
  81. for fupSource in project.getFupSources():
  82. fupWidget = FupWidget(None, None)
  83. factory = FupFactory(fupWidget=fupWidget)
  84. factory.parse(fupSource.sourceBytes)
  85. if opt_genUUIDs:
  86. # Re-generate all UUIDs.
  87. fupWidget.regenAllUUIDs()
  88. fupSource.sourceBytes = factory.compose()
  89. for kopSource in project.getKopSources():
  90. pass#TODO
  91. project.toFile()
  92. except AwlSimError as e:
  93. printError(e.getReport())
  94. return ExitCodes.EXIT_ERR_SIM
  95. return ExitCodes.EXIT_OK
  96. if __name__ == "__main__":
  97. qtArgv = list(sys.argv)
  98. if osIsPosix and not os.environ.get("DISPLAY"):
  99. # The X server is not running.
  100. # Smuggle the offscreen option into Qt argv.
  101. qtArgv.insert(1, "-platform")
  102. qtArgv.insert(2, "offscreen")
  103. qapp = QApplication(qtArgv)
  104. sys.exit(main())