forked from CLCMacTeam/besapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_all_sites.py
138 lines (101 loc) · 3.74 KB
/
export_all_sites.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
This will export all bigfix sites to a folder called `export`
This is equivalent of running `python -m besapi export_all_sites`
requires `besapi`, install with command `pip install besapi`
Example Usage:
python export_all_sites.py -r https://localhost:52311/api -u API_USER -p API_PASSWORD
References:
- https://developer.bigfix.com/rest-api/api/admin.html
- https://github.com/jgstew/besapi/blob/master/examples/rest_cmd_args.py
- https://github.com/jgstew/tools/blob/master/Python/locate_self.py
"""
import argparse
import getpass
import logging
import logging.handlers
import ntpath
import os
import platform
import shutil
import sys
import besapi
import besapi.plugin_utilities
__version__ = "0.0.1"
verbose = 0
bes_conn = None
invoke_folder = None
def get_invoke_folder(verbose=0):
"""Get the folder the script was invoked from"""
# using logging here won't actually log it to the file:
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
if verbose:
print("running in a PyInstaller bundle")
invoke_folder = os.path.abspath(os.path.dirname(sys.executable))
else:
if verbose:
print("running in a normal Python process")
invoke_folder = os.path.abspath(os.path.dirname(__file__))
if verbose:
print(f"invoke_folder = {invoke_folder}")
return invoke_folder
def get_invoke_file_name(verbose=0):
"""Get the filename the script was invoked from"""
# using logging here won't actually log it to the file:
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
if verbose:
print("running in a PyInstaller bundle")
invoke_file_path = sys.executable
else:
if verbose:
print("running in a normal Python process")
invoke_file_path = __file__
if verbose:
print(f"invoke_file_path = {invoke_file_path}")
# get just the file name, return without file extension:
return os.path.splitext(ntpath.basename(invoke_file_path))[0]
def main():
"""Execution starts here"""
print("main() start")
print("NOTE: this script requires besapi v3.3.3+")
parser = besapi.plugin_utilities.setup_plugin_argparse()
# add additonal arg specific to this script:
parser.add_argument(
"-d",
"--delete",
help="delete previous export",
required=False,
action="store_true",
)
# allow unknown args to be parsed instead of throwing an error:
args, _unknown = parser.parse_known_args()
# allow set global scoped vars
global bes_conn, verbose, invoke_folder
verbose = args.verbose
# get folder the script was invoked from:
invoke_folder = get_invoke_folder()
log_file_path = os.path.join(
get_invoke_folder(verbose), get_invoke_file_name(verbose) + ".log"
)
print(log_file_path)
logging_config = besapi.plugin_utilities.get_plugin_logging_config(
log_file_path, verbose, args.console
)
logging.basicConfig(**logging_config)
logging.info("----- Starting New Session ------")
logging.debug("invoke folder: %s", invoke_folder)
logging.debug("Python version: %s", platform.sys.version)
logging.debug("BESAPI Module version: %s", besapi.besapi.__version__)
logging.debug("this plugin's version: %s", __version__)
bes_conn = besapi.plugin_utilities.get_besapi_connection(args)
export_folder = os.path.join(invoke_folder, "export")
# if --delete arg used, delete export folder:
if args.delete:
shutil.rmtree(export_folder, ignore_errors=True)
try:
os.mkdir(export_folder)
except FileExistsError:
logging.warning("Folder already exists!")
os.chdir(export_folder)
bes_conn.export_all_sites()
if __name__ == "__main__":
main()