-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpshell.py
More file actions
executable file
·216 lines (188 loc) · 7.93 KB
/
pshell.py
File metadata and controls
executable file
·216 lines (188 loc) · 7.93 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
"""
Standard lib python3 command line client for mediaflux and s3
Author: Sean Fleming
"""
import sys
# magnus python 3.4 seems broken, module python/3.6.3 works fine
VERSION_MIN = (3, 6)
if sys.version_info < VERSION_MIN:
sys.exit("ERROR: Python >= %d.%d is required, your version = %d.%d\n" % (VERSION_MIN[0], VERSION_MIN[1], sys.version_info[0], sys.version_info[1]))
import os
import json
import logging
import argparse
import platform
import itertools
import configparser
import concurrent.futures
import parser
import util
# no readline on windows
try:
import readline
except:
pass
# auto
build="repository"
version="v1.2.1"
#------------------------------------------------------------
def main():
global build
global version
# server config (section heading) to use
p = argparse.ArgumentParser(description="pshell help")
p.add_argument("-c", dest='current', default='pawsey', help="the config name in $HOME/.pshell_config to connect to")
p.add_argument("-i", dest='script', help="input script file containing pshell commands")
p.add_argument("-o", dest='output', default=None, help="output any failed commands to a script")
p.add_argument("-v", dest='verbose', default=None, help="set verbosity level (0,1,2)")
p.add_argument("-u", dest='url', default=None, help="Remote endpoint URL")
p.add_argument("-t", dest='type', default=None, help="Remote endpoint type (eg mflux, s3)")
p.add_argument("-V","--version", action="store_true", help="Show version")
p.add_argument("command", nargs="?", default="", help="a single command to execute")
args = p.parse_args()
if args.version is True:
print(f"pshell version {version}")
print(f"Build {build}")
sys.exit(0)
# configure logging
logging_level = logging.ERROR
if args.verbose is not None:
if args.verbose == "2":
logging_level = logging.DEBUG
elif args.verbose == "1":
logging_level = logging.INFO
# print("log level = %d" % logging_level)
logging.basicConfig(format='%(levelname)9s %(asctime)-15s >>> %(module)s.%(funcName)s(): %(message)s', level=logging_level)
# basic info
logging.info("Version=%s" % version)
logging.info("Build=%s" % build)
logging.info("PLATFORM=%s" % platform.system())
version = sys.version
i = version.find("\n")
logging.info("PYTHON=%s" % version[:i])
# attempt to locate a valid config file
config_filepath = os.path.expanduser("~/.pshell_config")
try:
open(config_filepath, 'a').close()
except:
config_filepath = os.path.join(os.getcwd(), ".pshell_config")
util.normalize_config_file(config_filepath)
config = configparser.ConfigParser()
logging.debug("Reading config file: [%s]" % config_filepath)
config.read(config_filepath)
# NEW
remotes_home = None
remotes_current = None
endpoints = {}
# create an endpoint
try:
# endpoint = None
if args.url is None:
# existing config and no input URL
if config.has_section(args.current) is True:
logging.debug("No input URL, reading endpoints from existing config [%s]" % args.current)
endpoints = json.loads(config.get(args.current, 'endpoints'))
else:
# 1st time default
logging.debug("Initialising [%s] config" % args.current)
if args.current == 'pawsey':
endpoints['portal'] = {'type':'mflux', 'url':'https://data.pawsey.org.au:443', 'domain':'ivec'}
endpoints['public'] = {'type':'mflux', 'url':'https://data.pawsey.org.au:443', 'domain':'public'}
# projects.pawsey.org.au did not work as an endpoint on setonix, but acacia.pawsey.org.au worked fine
endpoints['private'] = {'type':'s3', 'url':'https://acacia.pawsey.org.au'}
remotes_home = '/projects'
remotes_current = 'portal'
# store endpoints in config
config[args.current] = {'endpoints':json.dumps(endpoints)}
else:
# TODO - could want to do this in combo with a URL ...
raise Exception("No default config available for [%s]" % args.current)
# URL override
else:
logging.debug("Creating custom remote from url: [%s]" % args.url)
remotes_current = 'custom'
endpoints[remotes_current] = {'type':args.type, 'url':args.url}
except Exception as e:
logging.debug(str(e))
# extract terminal size for auto pagination
try:
import fcntl, termios, struct
size = struct.unpack('hh', fcntl.ioctl(0, termios.TIOCGWINSZ, '1234'))
except:
# FIXME - make this work with windows
size = (80, 20)
# configure parsing loop
my_parser = parser.parser()
my_parser.config = config
my_parser.config_name = args.current
my_parser.config_filepath = config_filepath
# generic thread pool for background processes
my_parser.thread_executor = concurrent.futures.ThreadPoolExecutor(max_workers=my_parser.thread_max)
# NEW - default remote name (could make it an arg...)
if remotes_current is not None:
my_parser.config.set(args.current, 'remotes_current', remotes_current)
if my_parser.config.has_option(args.current, 'remotes_current'):
my_parser.remotes_current = my_parser.config.get(args.current, 'remotes_current')
# NEW - default home folder (could make it an arg...)
if remotes_home is not None:
my_parser.config.set(args.current, 'remotes_home', remotes_home)
if my_parser.config.has_option(args.current, 'remotes_home'):
my_parser.cwd = my_parser.config.get(args.current, 'remotes_home')
# add endpoints
try:
for mount in endpoints:
my_parser.remote_add(mount, endpoints[mount])
# set current
my_parser.remote_set(my_parser.remotes_current, my_parser.cwd)
# added all remotes without error - save to config
my_parser.remotes_config_save()
except Exception as e:
logging.info(str(e))
# just in case the terminal height calculation returns a very low value
my_parser.terminal_height = max(size[0], my_parser.terminal_height)
# restart script
if args.output is not None:
my_parser.script_output = args.output
# TAB completion
# strange hackery required to get tab completion working under OS-X and also still be able to use the b key
# REF - http://stackoverflow.com/questions/7124035/in-python-shell-b-letter-does-not-work-what-the
try:
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
except:
logging.info("No readline module; tab completion unavailable")
# build non interactive input iterator
input_list = []
my_parser.interactive = True
if args.script:
input_list = itertools.chain(input_list, open(args.script))
my_parser.interactive = False
# FIXME - strictly, need regex to avoid split on quote protected &&
if len(args.command) != 0:
input_list = itertools.chain(input_list, args.command.split("&&"))
my_parser.interactive = False
# interactive or input iterator (scripted)
if my_parser.interactive:
print("Welcome to pshell: type 'help' for a list of commands")
print("=====================================================")
my_parser.loop_interactively()
else:
for item in input_list:
line = item.strip()
try:
print("%s:%s> %s" % (my_parser.remotes_current, my_parser.cwd, line))
my_parser.onecmd(line)
except KeyboardInterrupt:
print(" Interrupted")
sys.exit(-1)
except SyntaxError:
print(" Syntax error: for more information on commands type 'help'")
sys.exit(-1)
except Exception as e:
print(str(e))
sys.exit(-1)
if __name__ == '__main__':
main()