6 class HandledException(Exception):
7 """Feature-less Exception child. Use for expected operational errors."""
12 def setup_server_io(io_db):
13 """Fill IO files DB with proper file( path)s. Write process IO test string.
15 Ensure IO files directory at server/. Remove any old in file if found. Set
16 up new in file (io_db["file_in"]) for reading at io_db["path_in"], and new
17 out file (io_db["file_out"]) for writing at io_db["path_out"]. Start out
18 file with process hash line of format PID + " " + floated UNIX time
19 (io_db["teststring"]). Set worldstate file path io_db["path_worldstate"].
22 io_db["path_in"] = io_dir + "in"
23 io_db["path_out"] = io_dir + "out"
24 io_db["path_worldstate"] = io_dir + "worldstate"
25 io_db["teststring"] = str(os.getpid()) + " " + str(time.time())
26 os.makedirs(io_dir, exist_ok=True)
27 io_db["file_out"] = open(io_db["path_out"], "w")
28 io_db["file_out"].write(io_db["teststring"] + "\n")
29 if os.access(io_db["path_in"], os.F_OK):
30 os.remove(io_db["path_in"])
31 io_db["file_in"] = open(io_db["path_in"], "w")
32 io_db["file_in"].close()
33 io_db["file_in"] = open(io_db["path_in"], "r")
36 def cleanup_server_io(io_db):
37 """Close and remove all files in IO files DB."""
38 io_db["file_out"].close()
39 os.remove(io_db["path_out"])
40 io_db["file_in"].close()
41 os.remove(io_db["path_in"])
42 if "file_worldstate" in io_db: # This file's only set up
43 io_db["file_worldstate"].close() # properly when the game
44 if os.access(io_db["path_worldstate"], os.F_OK): # world is active, which
45 os.remove(io_db["path_worldstate"]) # is not guaranteed.
48 def detect_atomic_leftover(path):
49 """Raise explained HandledException if file is found at path + "_tmp"."""
50 path_tmp = path + "_tmp"
51 msg = "Found file '" + path_tmp + "' that may be a leftover from an " \
52 "aborted previous attempt to write '" + path + "'. Aborting until " \
53 "the matter is resolved by removing it from its current path."
54 if os.access(path_tmp, os.F_OK):
55 raise HandledException(msg)
60 print("DUMMY: Obey command-line arguments.")
61 print("DUMMY: Open files.")
62 setup_server_io(io_db)
63 print("DUMMY: Run game.")
64 path_recordfile = "recordfile"
65 path_savefile = "savefile"
66 detect_atomic_leftover(path_savefile)
67 detect_atomic_leftover(path_recordfile)
68 if os.access(path_savefile, os.F_OK):
69 print(open(path_savefile, "r").read())
71 msg = "MAKE_WORLD " + str(int(time.time()))
73 except HandledException as exception:
75 print(exception.args[0])
77 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
80 cleanup_server_io(io_db)
81 print("DUMMY: (Clean up C heap.)")