awlsim-proupgrade 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Project file upgrade utility
  5. #
  6. # Copyright 2017-2018 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. projectFile = args[0]
  73. try:
  74. Logging.setLoglevel(opt_loglevel)
  75. # Parse the project file and write it back immediately.
  76. # The read will parse old formats, too.
  77. # The write will write using the latest format.
  78. printInfo("Upgrading file format of: %s" % projectFile)
  79. project = Project.fromFile(projectFile)
  80. for fupSource in project.getFupSources():
  81. fupWidget = FupWidget(None, None)
  82. factory = FupFactory(fupWidget=fupWidget)
  83. factory.parse(fupSource.sourceBytes)
  84. if opt_genUUIDs:
  85. # Re-generate all UUIDs.
  86. fupWidget.regenAllUUIDs()
  87. fupSource.sourceBytes = factory.compose()
  88. for kopSource in project.getKopSources():
  89. pass#TODO
  90. project.toFile()
  91. except AwlSimError as e:
  92. printError(e.getReport())
  93. return ExitCodes.EXIT_ERR_SIM
  94. return ExitCodes.EXIT_OK
  95. if __name__ == "__main__":
  96. qtArgv = list(sys.argv)
  97. if osIsPosix and not os.environ.get("DISPLAY"):
  98. # The X server is not running.
  99. # Smuggle the offscreen option into Qt argv.
  100. qtArgv.insert(1, "-platform")
  101. qtArgv.insert(2, "offscreen")
  102. qapp = QApplication(qtArgv)
  103. sys.exit(main())