forked from distributed-system-analysis/smallfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_slave.py
80 lines (69 loc) · 2.38 KB
/
parse_slave.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
parse_slave.py -- parses SSH cmd for invocation of smallfile_remote.py
Copyright 2012 -- Ben England
Licensed under the Apache License at http://www.apache.org/licenses/LICENSE-2.0
See Appendix on this page for instructions pertaining to license.
'''
import sys
import os
import errno
import time
import pickle
import smallfile
# convert boolean value into 'Y' or 'N'
def usage(msg): # call if CLI syntax error or invalid parameter
print('')
print('ERROR: ' + msg)
print('usage: smallfile_remote.py ' +
'[ --network-sync-dir path ] --as-host hostname ')
sys.exit(1)
# parse command line and return tuple containing:
# network sync directory
# host identity of this remote invocation
def parse():
prm_network_sync_dir = None
argc = len(sys.argv)
if argc == 1:
print('for additional help add the parameter "--help" to the command')
sys.exit(1)
j = 1
while j < argc:
rawprm = sys.argv[j]
if rawprm == '-h' or rawprm == '--help':
usage('normally this process is run automatically ' +
'by smallfile_cli.py')
if rawprm[0:2] != '--':
usage('parameter names begin with "--"')
prm = rawprm[2:]
if j == argc - 1 and argc % 2 != 1:
usage('all parameters consist of a name and a value')
val = sys.argv[j + 1]
if len(rawprm) < 3:
usage('parameter name not long enough')
j += 2
if prm == 'network-sync-dir':
prm_network_sync_dir = val
elif prm == 'as-host':
# --ashost should not be used by end-user
prm_as_host = smallfile.get_hostname(val)
else:
usage('unrecognized parameter name')
param_pickle_fname = os.path.join(prm_network_sync_dir,
'param.pickle')
if not os.path.exists(param_pickle_fname):
time.sleep(1.1)
params = None
try:
with open(param_pickle_fname, 'rb') as pickled_params:
params = pickle.load(pickled_params)
params.is_slave = True
params.as_host = prm_as_host
params.master_invoke.onhost = prm_as_host
except IOError as e:
if e.errno != errno.ENOENT:
raise e
usage('could not read parameter pickle file %s'
% param_pickle_fname)
return params