summaryrefslogtreecommitdiffstats
path: root/awlsim-proupgrade
blob: 7d38972d06dc20fa6031027a3d6c7805d0461d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# AWL simulator - Project file upgrade utility
#
# Copyright 2017-2018 Michael Buesch <m@bues.ch>
#
# 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
import os
import getopt

from awlsim_loader.common import *
from awlsim.gui.util import *
from awlsim.gui.fup.fupwidget import FupFactory, FupWidget


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:")
	print(" -u|--gen-uuids               Regenerate all UUIDs.")
	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():
	opt_genUUIDs = False
	opt_loglevel = Logging.LOG_WARNING

	Logging.setPrefix("awlsim-proupgrade: ")

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"huL:",
			[ "help", "gen-uuids", "loglevel=", ])
	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
		if o in ("-u", "--gen-uuids"):
			opt_genUUIDs = True
		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.
		printInfo("Upgrading file format of: %s" % projectFile)

		project = Project.fromFile(projectFile)

		for fupSource in project.getFupSources():
			fupWidget = FupWidget(None, None)
			factory = FupFactory(fupWidget=fupWidget)
			factory.parse(fupSource.sourceBytes)
			if opt_genUUIDs:
				# Re-generate all UUIDs.
				fupWidget.regenAllUUIDs()
			fupSource.sourceBytes = factory.compose()

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

		project.toFile()

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

	return ExitCodes.EXIT_OK

if __name__ == "__main__":
	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)
	sys.exit(main())
bues.ch cgit interface