awlsim-proupgrade 3.59 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# AWL simulator - Project file upgrade utility
#
6
# Copyright 2017-2018 Michael Buesch <m@bues.ch>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals

import sys
26
import os
27 28 29
import getopt

from awlsim_loader.common import *
30 31
from awlsim.gui.util import *
from awlsim.gui.fup.fupwidget import FupFactory, FupWidget
32 33 34 35 36 37 38 39 40 41


def usage():
	print("awlsim-proupgrade version %s" % VERSION_STRING)
	print("")
	print("This utility re-writes a project file using the latest file format version.")
	print("")
	print("Usage: awlsim-proupgrade [OPTIONS] PROJECTFILE.awlpro")
	print("")
	print("Options:")
42
	print(" -u|--gen-uuids               Regenerate all UUIDs.")
43 44 45 46 47 48 49 50 51
	print(" -L|--loglevel LVL            Set the client log level:")
	print("                              0: Log nothing")
	print("                              1: Log errors")
	print("                              2: Log errors and warnings (default)")
	print("                              3: Log errors, warnings and info messages")
	print("                              4: Verbose logging")
	print("                              5: Extremely verbose logging")

def main():
52
	opt_genUUIDs = False
53 54
	opt_loglevel = Logging.LOG_WARNING

55 56
	Logging.setPrefix("awlsim-proupgrade: ")

57 58
	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
59 60
			"huL:",
			[ "help", "gen-uuids", "loglevel=", ])
61 62 63 64 65 66 67 68
	except getopt.GetoptError as e:
		printError(str(e))
		usage()
		return ExitCodes.EXIT_ERR_CMDLINE
	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			return ExitCodes.EXIT_OK
69 70
		if o in ("-u", "--gen-uuids"):
			opt_genUUIDs = True
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		if o in ("-L", "--loglevel"):
			try:
				opt_loglevel = int(v)
			except ValueError:
				printError("-L|--loglevel: Invalid log level")
				sys.exit(1)
	if len(args) != 1:
		usage()
		return ExitCodes.EXIT_ERR_CMDLINE
	projectFile = args[0]

	try:
		Logging.setLoglevel(opt_loglevel)

		# Parse the project file and write it back immediately.
		# The read will parse old formats, too.
		# The write will write using the latest format.
88
		printInfo("Upgrading file format of: %s" % projectFile)
89

90
		project = Project.fromFile(projectFile)
91 92

		for fupSource in project.getFupSources():
93 94
			fupWidget = FupWidget(None, None)
			factory = FupFactory(fupWidget=fupWidget)
95
			factory.parse(fupSource.sourceBytes)
96 97 98
			if opt_genUUIDs:
				# Re-generate all UUIDs.
				fupWidget.regenAllUUIDs()
99 100 101 102 103
			fupSource.sourceBytes = factory.compose()

		for kopSource in project.getKopSources():
			pass#TODO

104 105 106 107 108 109 110 111 112
		project.toFile()

	except AwlSimError as e:
		printError(e.getReport())
		return ExitCodes.EXIT_ERR_SIM

	return ExitCodes.EXIT_OK

if __name__ == "__main__":
113 114 115 116 117 118 119
	qtArgv = list(sys.argv)
	if osIsPosix and not os.environ.get("DISPLAY"):
		# The X server is not running.
		# Smuggle the offscreen option into Qt argv.
		qtArgv.insert(1, "-platform")
		qtArgv.insert(2, "offscreen")
	qapp = QApplication(qtArgv)
120
	sys.exit(main())