-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·161 lines (132 loc) · 4.96 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# Copyright (c) 2013 Galah Group LLC
# Copyright (c) 2013 Other contributers as noted in the CONTRIBUTERS file
#
# This file is part of galah-apiclient.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import pprint
import os
def main():
# Initialize the logging library.
import lib.logcontrol
import logging
lib.logcontrol.init_logging()
logger = logging.getLogger("apiclient")
# Load up the configuration, this includes parsing any command line
# arguments.
import lib.config as config
config.CONFIG = config.load_config()
if "verbosity" in config.CONFIG:
lib.logcontrol.set_level(config.CONFIG["verbosity"])
logger.debug(
"Final configuration dictionary...\n%s",
pprint.pformat(config.CONFIG, width = 72)
)
lib.logcontrol.show_tracebacks = config.CONFIG["show-tracebacks"]
# Set to True by any of the "do something and exit" options.
exit_now = False
# If the user wants to logout...
if config.CONFIG.get("logout"):
session_path = config.CONFIG["session-path"]
if os.path.isfile(session_path):
logger.info("Deleting session file at %s.", session_path)
os.remove(session_path)
else:
logger.info(
"No session file exists at %s. Doing nothing.",
session_path
)
exit_now = True
# If the user wants to clear the cache...
if config.CONFIG.get("clear-api-info"):
api_info_path = config.CONFIG["api-info-path"]
if os.path.isfile(api_info_path):
logger.info("Deleting session file at %s.", api_info_path)
os.remove(api_info_path)
else:
logger.info(
"No session file exists at %s. Doing nothing.",
api_info_path
)
exit_now = True
# If the user wants to save their configuration
if config.CONFIG.get("save"):
import lib.utils
save_to = config.CONFIG.get("config", config.DEFAULT_CONFIG_PATHS[0])
save_to = lib.utils.resolve_path(save_to)
logger.info("Saving configuration settings to %s.", save_to)
serialized_config = lib.config.dump_config()
logger.debug("Configuration...\n%s", serialized_config)
try:
config_dir = os.path.dirname(save_to)
if not os.path.exists(config_dir):
logger.debug("Creating %s directory.", config_dir)
lib.utils.prepare_directory(config_dir)
with open(save_to, "w") as f:
f.write(serialized_config)
except IOError:
logger.critical(
"Could not save configuration.", exc_info = sys.exc_info()
)
sys.exit(1)
logger.info("Successfully saved configuration settings.")
exit_now = True
if exit_now:
sys.exit(0)
# Grab the user's old session information if they are already logged in.
import lib.communicate
session = lib.communicate.APIClientSession()
session.load()
save_session = False
# Login if necessary
import lib.ui
if session.user is None:
if config.CONFIG.get("use-oauth"):
session.login_oauth2()
else:
session.login(*lib.ui.determine_credentials())
save_session = True
# Request the API info from the server if we don't have it cached
if session.api_info is None:
session.fetch_api_info()
save_session = True
# Save the session if we had to login or if we replenished our cache
# (because they are tied together artificially by our design).
if save_session:
session.save()
# Enter the shell or execute a command.
if config.CONFIG.get("shell"):
import lib.shell
new_shell = lib.shell.APIShell(session)
try:
new_shell.cmdloop()
except KeyboardInterrupt:
while True:
try:
new_shell.cmdloop("")
except KeyboardInterrupt:
print "\nInterrupted..."
else:
break
print "Exiting..."
else:
# Perform the command the user wants to execute
command_args, command_kwargs = lib.ui.parse_raw_args(config.ARGS)
if command_args:
session.call(command_args[0], *command_args[1:], **command_kwargs)
else:
logger.info("No command given. Doing nothing...")
if __name__ == "__main__":
main()