-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebGISDR-cli-wrapper.py
112 lines (95 loc) · 4.11 KB
/
WebGISDR-cli-wrapper.py
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
import argparse
import os
import subprocess
from pathlib import Path
# Variables
WebGISDR_filename = "webgisdr"
WebGISDR_dir = Path(r"C:\Program Files\ArcGIS\Portal\tools\webgisdr")
WebGISDR_backups_path = Path(r"\\some-filepath-here")
spacer = "-" * 50
def prune_copies(webgisdr_backups_path: Path, backup_arguments: argparse.Namespace):
"""Prunes a list of files according to the number of copies.
Args:
webgisdr_backups_path (Path): a directory path to the .webgissite backups
backup_arguments (argparse.Namespace): the user input arguments for backup and copies
"""
print(f"Pruning backups")
WebGISDR_backups = list(
webgisdr_backups_path.glob(f"*{backup_arguments.mode}.webgissite")
)
WebGISDR_backups.sort(reverse=True)
file_names = [file.name for file in WebGISDR_backups]
backups_retained = file_names[: backup_arguments.copies]
backups_pruned = file_names[backup_arguments.copies :]
for file in backups_pruned:
Path(webgisdr_backups_path, file).unlink()
print(f"\tretained:\t{backups_retained}\n\tpruned:\t\t{backups_pruned}\n")
return
def set_WebGISDR(webgisdr_dir: Path, webgisdr_filename: str, backup_mode: str):
"""Sets the WebGISDR configuration file's BACKUP_RESTORE_MODE property
Args:
webgisdr_dir (Path): the webgisdr tools directory path on the arcgisportal machine
webgisdr_filename (str): the name of your webgisdr configuration file, it is normally 'webgisdr'
backup_mode (str): the backup mode that was input as an argument
"""
print(f"Setting {webgisdr_filename}.properties: BACKUP_RESTORE_MODE")
WebGISDR_properties = Path(webgisdr_dir, f"{webgisdr_filename}.properties")
file_content = WebGISDR_properties.read_text()
lines = file_content.splitlines()
for i, line in enumerate(lines):
if "BACKUP_RESTORE_MODE =" in line:
print(f"\tcurrent value:\t{line.split(' = ')[-1]}")
lines[i] = f"BACKUP_RESTORE_MODE = {backup_mode}"
WebGISDR_properties.write_text("\n".join(lines))
print(f"\tset value:\t{backup_mode}\n")
return
def run_WebGISDR(webgisdr_dir: Path, webgisdr_filename: str):
"""Runs the WebGISDR command-line utility
Args:
webgisdr_dir (Path): the webgisdr tools directory path on the arcgisportal machine
webgisdr_filename (str): the name of your webgisdr configuration file, it is normally 'webgisdr'
"""
print(
f"Changing Directory to {webgisdr_dir}\n\trunning standard WebGISDR utility now\n\n{spacer}\n{spacer}"
)
os.chdir(webgisdr_dir)
cmd_webgisdr = f"webgisdr --export --file {webgisdr_filename}.properties"
subprocess.run(cmd_webgisdr, shell=True)
print(f"{spacer}\n{spacer}\nAll Operations Complete\n{spacer}\n{spacer}")
return
def configure():
"""Configures the user input arguments
Returns:
argparse.Namespace
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"mode",
choices=["FULL", "INCREMENTAL", "BACKUP"],
type=str.upper,
help="""the type of WebGISDR backup to be performed: backup, full, or
incremental. this input is case insensitive.
""",
)
parser.add_argument(
"copies",
choices=range(1, 8),
type=int,
help="""
the number of copies to retain. this is the number of backups
only for the backup mode input/selected. this is exclusive of the primary backup
which will run at the end of the script.
""",
)
print(f"{spacer}\n{spacer}\nRunning WebGISDR Automation")
return parser.parse_args()
def main(backup_arguments: argparse.Namespace):
print(
f"\tbackup_mode is: {backup_arguments.mode}\n\tcopies to retain: {backup_arguments.copies}\n"
)
prune_copies(WebGISDR_backups_path, backup_arguments)
set_WebGISDR(WebGISDR_dir, WebGISDR_filename, backup_arguments.mode)
run_WebGISDR(WebGISDR_dir, WebGISDR_filename)
if __name__ == "__main__":
arguments = configure()
main(arguments)