Files
Aurora.3/tools/mapmerge2/frontend.py
Mykhailo Bykhovtsev 779f8a0733 Integrating mapmerge2 checks into Travis (#5616)
Okay, this update is kinda big. Summary:

- Trimmed unused keys in Exodus telecomms

- Adds script that will run mapmerge2 on Travis to check branch for unused keys or key overflow, etc.

- Fixes matching indentation style in tag-matcher and converts it to use Python 3.6

- Converts mapmerge2 to be used by Python 3.4 and above. Instead of 3.6

- Removes Windows 1252 characters from Communication-blackout.dm that were not able to be seen in UTF-8 format.

Note: the last commit will fail because currently main level is broken

Example of no map issues:
![2018-11-15_li](https://user-images.githubusercontent.com/25555314/48592180-0fd7be80-e8fc-11e8-80b9-cd5af32540e3.jpg)

Example of issues:
![2018-11-15_li 2](https://user-images.githubusercontent.com/25555314/48592190-15cd9f80-e8fc-11e8-99bd-6da4b4c2b9d8.jpg)
2018-11-20 20:14:21 +01:00

129 lines
3.8 KiB
Python

#!/usr/bin/env python3
# Common code for the frontend interface of map tools
import sys
import os
import pathlib
import shutil
from collections import namedtuple
Settings = namedtuple('Settings', ['map_folder', 'tgm'])
MapsToRun = namedtuple('MapsToRun', ['files', 'indices'])
def string_to_num(s):
try:
return int(s)
except ValueError:
return -1
def read_settings():
# discover map folder if needed
try:
map_folder = os.environ['MAPROOT']
except KeyError:
map_folder = '_maps/'
for _ in range(8):
if os.path.exists(map_folder):
break
map_folder = os.path.join('..', map_folder)
else:
map_folder = None
# assume TGM is True by default
tgm = os.environ.get('TGM', "1") == "1"
return Settings(map_folder, tgm)
def pretty_path(settings, path_str):
if settings.map_folder:
return path_str[len(os.path.commonpath([settings.map_folder, path_str]))+1:]
else:
return path_str
def prompt_maps(settings, verb):
if not settings.map_folder:
print("Could not autodetect the _maps folder, set MAPROOT")
exit(1)
list_of_files = list()
for root, directories, filenames in os.walk(settings.map_folder):
for filename in [f for f in filenames if f.endswith(".dmm")]:
list_of_files.append(pathlib.Path(root, filename))
last_dir = ""
for i, this_file in enumerate(list_of_files):
this_dir = this_file.parent
if last_dir != this_dir:
print("--------------------------------")
last_dir = this_dir
print("[{}]: {}".format(i, pretty_path(settings, str(this_file))))
print("--------------------------------")
in_list = input("List the maps you want to " + verb + " (example: 1,3-5,12):\n")
in_list = in_list.replace(" ", "")
in_list = in_list.split(",")
valid_indices = list()
for m in in_list:
index_range = m.split("-")
if len(index_range) == 1:
index = string_to_num(index_range[0])
if index >= 0 and index < len(list_of_files):
valid_indices.append(index)
elif len(index_range) == 2:
index0 = string_to_num(index_range[0])
index1 = string_to_num(index_range[1])
if index0 >= 0 and index0 <= index1 and index1 < len(list_of_files):
valid_indices.extend(range(index0, index1 + 1))
return MapsToRun(list_of_files, valid_indices)
def process(settings, verb, *, modify=True, backup=None):
if backup is None:
backup = modify # by default, backup when we modify
assert modify or not backup # doesn't make sense to backup when not modifying
if len(sys.argv) > 1:
maps = sys.argv[1:]
else:
maps = prompt_maps(settings, verb)
maps = [str(maps.files[i]) for i in maps.indices]
print()
if not maps:
print("No maps selected.")
return
if modify:
print("Maps WILL{} be converted to tgm.".format('' if settings.tgm else ' NOT'))
if backup:
print("Backups will be created with a \".before\" extension.")
else:
print("Warning: backups are NOT being taken.")
print("\nWill {} these maps:".format(verb))
for path_str in maps:
print(pretty_path(settings, path_str))
try:
confirm = input("\nPress Enter to {}...\n".format(verb))
except KeyboardInterrupt:
confirm = "^C"
if confirm != "":
print("\nAborted.")
return
for path_str in maps:
print(" - {}".format(pretty_path(settings, path_str)))
if backup:
shutil.copyfile(path_str, path_str + ".before")
try:
yield path_str
except Exception as e:
print("Error: {}".format(e))
else:
print("Succeeded.")
print("\nFinished.")