This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.py
executable file
路186 lines (150 loc) 路 4.15 KB
/
cli.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
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
#!/usr/bin/env python3
from lib import activate_venv
from lib.veevutils import banner
from painter import paint
import codecs, sys, os, glob, stat, subprocess, pty
def parseUtils():
def isExecutable(f):
return (os.stat(f)[stat.ST_MODE] & stat.S_IXUSR) > 0
scripts = [util for util in glob.glob(os.path.join(UTILS_DIR, "*.py")) if isExecutable(util)]
names = [os.path.splitext(os.path.basename(script))[0] for script in scripts]
return sorted(names)
def indented(message):
print(" " + message)
def usage():
def pad(name):
return ' ' * (10 - len(name))
print("SYNPOSIS")
for command in sorted(COMMANDS):
indented(PROGNAME + ' ' + command + ' ' + COMMANDS[command]['usage'])
print("\nDESCRIPTION")
indented("The combined cli utility for Velveeva")
indented("The following options are available\n\n")
for command in sorted(COMMANDS):
indented(PROGNAME + ' ' + command + pad(command) + COMMANDS[command]['help'])
def util_help():
print("UTILS")
indented("(For more information use: velveeva util util_name --help)\n")
for util in parseUtils():
indented(PROGNAME + " util " + util)
def help(args):
print(banner())
usage()
def maybe_multibyte(stream):
# todo: deal with unicode BOM
b0 = stream.read(1)
if not len(b0): return ''
# from https://en.wikipedia.org/wiki/UTF-8#Description
ONE_BYTE = 0b00000000
TWO_BYTES = 0b11000000
THREE_BYTES = 0b11100000
FOUR_BYTES = 0b11110000
MAX = 0b11110111
multibyte = False
char = b0
first_byte = int.from_bytes(b0, sys.byteorder)
if first_byte < TWO_BYTES:
pass
elif first_byte < THREE_BYTES:
multibyte = True
char = char + stream.read(1)
elif first_byte < FOUR_BYTES:
multibyte = True
char = char + stream.read(2)
elif first_byte < MAX:
multibyte = True
char = char + stream.read(3)
decoded = char.decode('utf-8')
return decoded
def exec_cmd(cmd, args=[]):
call = [os.path.join(BASE_DIR, cmd)] + args
process = subprocess.Popen([cmd] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
try:
out = maybe_multibyte(process.stdout)
except KeyboardInterrupt:
sys.stdout.write("\n")
return 1
if out == '' and process.poll() != None:
for err in process.stderr.readlines():
sys.stdout.write(err.decode('utf-8'))
sys.stderr.flush()
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()
return process.returncode
def exec_util(args):
if not args:
print(banner())
util_help()
return 1
util = args[0]
util_args = args[1:]
utils = parseUtils()
if util is None:
print(banner())
util_help()
return 1
elif util in utils:
script = os.path.join(UTILS_DIR,util+".py")
return exec_cmd(script, util_args)
else:
print(util + ' is not a recognized Velveeva util command')
return 1
def dispatch(command_name, args):
if COMMANDS.get(command_name, None) is not None:
cmd = COMMANDS[command_name]['command']
if cmd is None:
return
elif callable(cmd):
cmd(args)
else:
return exec_cmd(os.path.join(BASE_DIR, cmd), args)
else:
print("'%s' is not a valid velveeva command" % command_name, file=sys.stderr)
def main():
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
if len(sys.argv) < 2:
print(banner(subtitle='An easier way to manage, maintain,\n and build Veeva iRep presentations'))
usage()
return 1
else:
return dispatch(sys.argv[1], sys.argv[2:])
PROGNAME = 'velveeva'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
UTILS_DIR = os.path.join(BASE_DIR,'lib')
COMMANDS = {
'go': {
'command': './go.py',
'usage': '[options]',
'help': 'build the project. See go --help for more info'
},
'init': {
'command': './init',
'usage': '',
'help': 'initialize the wizard to create a new project'
},
'util': {
'command': exec_util,
'usage': 'utilname [options]',
'help': 'execute a utility script'
},
'update': { # gets intercepted by the cli wrapper
'command': None,
'usage': '',
'help': 'update to the latest VELVEEVA-cli utility docker image'
},
'help': {
'command': help,
'usage': '',
'help': 'display this message'
},
'version': {
'command': None,
'usage': '',
'help': 'velveeva-cli version info'
}
}
if __name__ == '__main__':
sys.exit(main())