Skip to content

Commit

Permalink
Use common exit codes
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <m@bues.ch>
  • Loading branch information
mbuesch committed Jul 30, 2016
1 parent 8e33674 commit 1b0d88d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 46 deletions.
13 changes: 6 additions & 7 deletions awlsim-client
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def main():
except getopt.GetoptError as e:
printError(str(e))
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
return 0
return ExitCodes.EXIT_OK
if o in ("-c", "--connect"):
try:
host, port = parseNetAddress(v)
Expand Down Expand Up @@ -104,12 +104,11 @@ def main():
sys.exit(1)
if args:
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
if not actions:
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE

exitCode = 0
client = None
try:
Logging.setLoglevel(opt_loglevel)
Expand All @@ -126,12 +125,12 @@ def main():
assert(0)
except AwlSimError as e:
printError(e.getReport())
return 1
return ExitCodes.EXIT_ERR_SIM
finally:
if client:
client.shutdown()

return exitCode
return ExitCodes.EXIT_OK

if __name__ == "__main__":
sys.exit(main())
12 changes: 6 additions & 6 deletions awlsim-linuxcnc-hal
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def main():
return 1
projectFile = args[0]

result = 0
result = ExitCodes.EXIT_OK
try:
Logging.setPrefix("awlsim-linuxcnc: ")
Logging.setLoglevel(opt_loglevel)
Expand Down Expand Up @@ -407,21 +407,21 @@ def main():
# Run loop exited normally. Bail out.
break
except LinuxCNC_NotRunning as e:
result = 1
result = ExitCodes.EXIT_ERR_OTHER
except KeyboardInterrupt as e:
result = 1
result = ExitCodes.EXIT_ERR_OTHER
except (AwlParserError, AwlSimError) as e:
printError(e.getReport())
result = 1
result = ExitCodes.EXIT_ERR_SIM
except MaintenanceRequest as e:
if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
MaintenanceRequest.TYPE_STOP,
MaintenanceRequest.TYPE_RTTIMEOUT):
result = 0
result = ExitCodes.EXIT_OK
else:
printError("Received invalid maintenance request %d" %\
e.requestType)
result = 1
result = ExitCodes.EXIT_ERR_SIM
printInfo("LinuxCNC HAL module shutdown.")

return result
Expand Down
10 changes: 5 additions & 5 deletions awlsim-server
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def main():
except getopt.GetoptError as e:
printError(str(e))
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
return 0
return ExitCodes.EXIT_OK
if o in ("-l", "--listen"):
try:
host, port = parseNetAddress(v)
Expand Down Expand Up @@ -112,11 +112,11 @@ def main():
sys.exit(1)
if len(args) not in (0, 1):
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
if args:
opt_project = args[0]

exitCode = 0
exitCode = ExitCodes.EXIT_OK
try:
Logging.setLoglevel(opt_loglevel)
if opt_background:
Expand Down Expand Up @@ -146,7 +146,7 @@ def main():
projectWriteBack = opt_rwProject)
except AwlSimError as e:
printError(e.getReport())
return 1
return ExitCodes.EXIT_ERR_SIM

return exitCode

Expand Down
16 changes: 8 additions & 8 deletions awlsim-symtab
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def main():
except getopt.GetoptError as e:
printError(str(e))
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
return 0
return ExitCodes.EXIT_OK
if o in ("-I", "--input-format"):
if v.lower() == "auto":
opt_inputParser = None
Expand All @@ -77,20 +77,20 @@ def main():
opt_inputParser = SymTabParser_ASC
else:
printError("Invalid --input-format")
return 1
return ExitCodes.EXIT_ERR_CMDLINE
if o in ("-O", "--output-format"):
opt_outputFormat = v.lower()
if opt_outputFormat not in ("csv", "readable-csv", "asc"):
printError("Invalid --output-format")
return 1
return ExitCodes.EXIT_ERR_CMDLINE
if len(args) == 1:
opt_infile = args[0]
elif len(args) == 2:
opt_infile = args[0]
opt_outfile = args[1]
elif len(args) > 2:
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE

try:
if opt_infile == "-":
Expand Down Expand Up @@ -133,11 +133,11 @@ def main():
except IOError as e:
printError("Failed to write output file '%s': %s" %\
(opt_outfile, str(e)))
return 1
return ExitCodes.EXIT_ERR_IO
except AwlSimError as e:
printError(e.getReport())
return 1
return 0
return ExitCodes.EXIT_ERR_SIM
return ExitCodes.EXIT_OK

if __name__ == "__main__":
sys.exit(main())
26 changes: 13 additions & 13 deletions awlsim-test
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def run(inputFile):
writeStdout(lastDump + '\n')
except (AwlParserError, AwlSimError) as e:
printError(e.getReport())
return 1
return ExitCodes.EXIT_ERR_SIM
except KeyboardInterrupt as e:
pass
except MaintenanceRequest as e:
Expand All @@ -239,7 +239,7 @@ def run(inputFile):
opt_profile)
writeStdout(ps)
writeStdout("\n")
return 0
return ExitCodes.EXIT_OK

def runWithServerBackend(inputFile):
client = None
Expand All @@ -249,7 +249,7 @@ def runWithServerBackend(inputFile):
printError("The accelerated CYTHON core currently is incompatible "
"with the backend server. Please remove the "
"AWLSIM_CYTHON environment variable.")
return 1
return ExitCodes.EXIT_ERR_INTERP

project = Project.fromProjectOrRawAwlFile(inputFile)

Expand Down Expand Up @@ -321,7 +321,7 @@ def runWithServerBackend(inputFile):
writeStdout(lastDump + '\n')
except AwlSimError as e:
printError(e.getReport())
return 1
return ExitCodes.EXIT_ERR_SIM
except MaintenanceRequest as e:
if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
MaintenanceRequest.TYPE_STOP,
Expand All @@ -335,7 +335,7 @@ def runWithServerBackend(inputFile):
finally:
if client:
client.shutdown()
return 0
return ExitCodes.EXIT_OK

def __signalHandler(sig, frame):
printInfo("Received signal %d" % sig)
Expand Down Expand Up @@ -390,11 +390,11 @@ def main():
except getopt.GetoptError as e:
printError(str(e))
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
return 0
return ExitCodes.EXIT_OK
if o in ("-C", "--cycle-time"):
try:
opt_cycletime = float(v)
Expand Down Expand Up @@ -484,15 +484,15 @@ def main():
print("The supported system functions (SFCs) are:")
printSysblockInfo(SFC_table, "SFC", opt_extInsns,
o.endswith("verbose"))
return 0
return ExitCodes.EXIT_OK
if o in ("--list-sfb", "--list-sfb-verbose"):
print("The supported system function blocks (SFBs) are:")
printSysblockInfo(SFB_table, "SFB", opt_extInsns,
o.endswith("verbose"))
return 0
return ExitCodes.EXIT_OK
if len(args) != 1 and not opt_hwinfos:
usage()
return 1
return ExitCodes.EXIT_ERR_CMDLINE
if args:
inputFile = args[0]

Expand All @@ -511,17 +511,17 @@ def main():
for name in opt_hwinfos:
cls = AwlSim.loadHardwareModule(name)
print(cls.getModuleInfo())
return 0
return ExitCodes.EXIT_OK
except (AwlParserError, AwlSimError) as e:
printError(e.getReport())
return 1
return ExitCodes.EXIT_ERR_SIM

signal.signal(signal.SIGTERM, __signalHandler)

if opt_interpreter and not opt_spawnBackend:
printError("Selected an --interpreter, but no "
"--spawn-backend was requested.")
return 1
return ExitCodes.EXIT_ERR_CMDLINE

if opt_spawnBackend or opt_connect:
return runWithServerBackend(inputFile)
Expand Down
6 changes: 3 additions & 3 deletions awlsim/coreserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ def _execute(cls, env=None):
"""Execute the server process.
Returns the exit() return value."""

server, retval = None, 0
server, retval = None, ExitCodes.EXIT_OK
try:
server = AwlSimServer()
for sig in (signal.SIGTERM, ):
signal.signal(sig, server.signalHandler)
server.runFromEnvironment(env)
except AwlSimError as e:
print(e.getReport())
retval = 1
retval = ExitCodes.EXIT_ERR_SIM
except MaintenanceRequest as e:
print("AwlSimServer: Unhandled MaintenanceRequest:\n%s" %\
str(e))
retval = 1
retval = ExitCodes.EXIT_ERR_SIM
except KeyboardInterrupt:
print("AwlSimServer: Interrupted.")
finally:
Expand Down
8 changes: 4 additions & 4 deletions awlsim/gui/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ def usage():
except getopt.GetoptError as e:
printError(str(e))
usage()
sys.exit(1)
sys.exit(ExitCodes.EXIT_ERR_CMDLINE)
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
sys.exit(0)
sys.exit(ExitCodes.EXIT_OK)
if o in ("-L", "--loglevel"):
try:
opt_loglevel = int(v)
except ValueError:
printError("-L|--loglevel: Invalid log level")
sys.exit(1)
sys.exit(ExitCodes.EXIT_ERR_CMDLINE)
if args:
if len(args) == 1:
opt_awlSource = args[0]
else:
usage()
sys.exit(1)
sys.exit(ExitCodes.EXIT_ERR_CMDLINE)

Logging.setPrefix("awlsim-gui: ")
Logging.setLoglevel(opt_loglevel)
Expand Down

0 comments on commit 1b0d88d

Please sign in to comment.