Skip to content

Commit 16183d1

Browse files
committed
gufi_config revamp
added OutputBuffer to Server config Exec has been renamed to Executable Server and Client are now classes instead of prefixes getting values from the config is now done by calling functions instead of passing in hardcoded strings as keys to a dictionary
1 parent 877754c commit 16183d1

File tree

8 files changed

+106
-80
lines changed

8 files changed

+106
-80
lines changed

config/server.example.in

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ Threads=@CORES@
6767

6868
# absolute path to gufi_query
6969
# single path string
70-
Exec=@CMAKE_INSTALL_PREFIX@/bin/gufi_query
70+
Executable=@CMAKE_INSTALL_PREFIX@/bin/gufi_query
7171

7272
# directory where all GUFI indicies are placed under
7373
# single path string
7474
IndexRoot=@CMAKE_BINARY_DIR@
75+
76+
# size of per-thread print buffers
77+
OutputBuffer=4096

scripts/completions

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ __gufi() {
7878
fi
7979

8080
root=$(readlink -m $(grep "IndexRoot=" "${CONFIG}" | sed "s/^.*=//"))
81-
bfq=$(readlink -m $(grep "Exec=" ${CONFIG} | sed "s/^.*=//"))
81+
bfq=$(readlink -m $(grep "Executable=" ${CONFIG} | sed "s/^.*=//"))
8282
curr=$(printf "%s" "${COMP_WORDS[@]:${COMP_CWORD}}")
8383
path="${root}/${curr}"
8484
name=${path##*/} # basename without expanding

scripts/gufi_client

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def talk_to_server(config, args):
8080
ssh.load_system_host_keys('/etc/ssh/ssh_known_hosts')
8181

8282
# connect to the server
83-
ssh.connect(hostname=config['Server'],
84-
port=config['Port'],
83+
ssh.connect(hostname=config.server(),
84+
port=config.port()],
8585
username=getpass.getuser(),
8686
hostkey=paramiko.opensshkey.load_pubkey_from_file('/etc/ssh/ssh_host_rsa_key.pub'))
8787

@@ -100,10 +100,10 @@ def talk_to_server(config, args):
100100
ssh.close()
101101

102102
if __name__ == '__main__':
103-
config = gufi_config.client_config(gufi_config.DEFAULT_CONFIG_PATH)
103+
config = gufi_config.Client(gufi_config.DEFAULT_PATH)
104104

105105
# import paramiko from provided path
106-
sys.path.append(config['Paramiko'])
106+
sys.path.append(config.paramiko())
107107
import paramiko
108108

109109
talk_to_server(config, sys.argv[1:])

scripts/gufi_config.py

+62-34
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,9 @@
6969
import gufi_common
7070

7171
# default configuration file location
72-
DEFAULT_CONFIG_PATH='/etc/GUFI/config'
73-
74-
# recognized settings
75-
CLIENT_SETTINGS = [
76-
'Server', # hostname
77-
'Port', # ssh port
78-
'Paramiko' # location of paramiko installation
79-
]
80-
81-
SERVER_SETTINGS = [
82-
'Threads', # number of threads to use
83-
'Exec', # absolute path of gufi_query
84-
'IndexRoot' # absolute path of root directory for GUFI to traverse
85-
]
86-
87-
def read_config(filename):
72+
DEFAULT_PATH='/etc/GUFI/config'
73+
74+
def _parse(filename):
8875
with open(filename, 'r') as f:
8976
out = {}
9077
for line in f:
@@ -105,26 +92,67 @@ def read_config(filename):
10592
value = gufi_common.get_positive(value)
10693
elif name in ['Paramiko', 'IndexRoot']:
10794
value = os.path.normpath(value)
95+
elif name == 'OutputBuffer':
96+
value = gufi_common.get_non_negative(value)
10897
out[name] = value
98+
10999
return out
110100

111-
# command line arguments override configuration file values (logic is inverted)
112-
def server_config(filename = DEFAULT_CONFIG_PATH):
113-
config = read_config(filename)
114-
if 'Threads' not in config:
115-
raise exceptions.Exception('Missing Threads')
116-
if 'Exec' not in config:
117-
raise exceptions.Exception('Missing Exec')
118-
if 'IndexRoot' not in config:
119-
raise exceptions.Exception('Missing IndexRoot')
101+
def _read(settings, filename = DEFAULT_PATH):
102+
config = _parse(filename)
103+
for key in settings:
104+
if key not in config:
105+
raise exceptions.Exception(filename + ' missing ' + key)
120106
return config
121107

122-
def client_config(filename = DEFAULT_CONFIG_PATH):
123-
config = read_config(filename)
124-
if 'Server' not in config:
125-
raise exceptions.Exception('Missing Server')
126-
if 'Port' not in config:
127-
raise exceptions.Exception('Missing Port')
128-
if 'Paramiko' not in config:
129-
raise exceptions.Exception('Missing Paramiko')
130-
return config
108+
class Server:
109+
THREADS = 'Threads' # number of threads to use
110+
EXECUTABLE = 'Executable' # absolute path of gufi_query
111+
INDEXROOT = 'IndexRoot' # absolute path of root directory for GUFI to traverse
112+
OUTPUTBUFFER = 'OutputBuffer' # size of per-thread buffers used to buffer prints
113+
114+
SETTINGS = [
115+
THREADS,
116+
EXECUTABLE,
117+
INDEXROOT,
118+
OUTPUTBUFFER
119+
]
120+
121+
def __init__(self, filename = DEFAULT_PATH):
122+
self.config = _read(Server.SETTINGS, filename)
123+
124+
def threads(self):
125+
return self.config[Server.THREADS]
126+
127+
def executable(self):
128+
return self.config[Server.EXECUTABLE]
129+
130+
def indexroot(self):
131+
return self.config[Server.INDEXROOT]
132+
133+
def outputbuffer(self):
134+
return self.config[Server.OUTPUTBUFFER]
135+
136+
class Client:
137+
SERVER = 'Server' # hostname
138+
PORT = 'Port' # ssh port
139+
PARAMIKO = 'Paramiko' # location of paramiko installation
140+
141+
142+
SETTINGS = [
143+
SERVER,
144+
PORT,
145+
PARAMIKO,
146+
]
147+
148+
def __init__(self, filename = DEFAULT_PATH):
149+
self.config = _read(Client.SETTINGS, filename)
150+
151+
def server(self):
152+
return self.config[Client.SERVER]
153+
154+
def port(self):
155+
return self.config[Client.PORT]
156+
157+
def paramiko(self):
158+
return self.config[Client.PARAMIKO]

scripts/gufi_find

+7-9
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,14 @@ def build_expression_parser():
535535
parser.add_argument('--num-results', metavar='n', dest='num_results', type=gufi_common.get_non_negative, help='first n results')
536536
parser.add_argument('--smallest', dest='smallest', action='store_true', help='top n smallest files')
537537
parser.add_argument('--largest', dest='largest', action='store_true', help='top n largest files')
538-
parser.add_argument('--output-buffer', metavar='bytes', dest='output_buffer', type=gufi_common.get_positive, default=4096, help='Size of each thread\'s output buffer')
539538
parser.add_argument('--in-memory-name', metavar='name', dest='inmemory_name', type=str, default='out', help='Name of in-memory database when aggregation is performed')
540539

541540
return parser
542541

543542
# argv[0] should be the command name
544543
def run(argv, config_path):
545544
# find and parse the configuration file first
546-
config = gufi_config.server_config(config_path)
545+
config = gufi_config.Server(config_path)
547546

548547
expression_parser = build_expression_parser()
549548

@@ -588,7 +587,7 @@ def run(argv, config_path):
588587
paths = ['']
589588

590589
# prepend the provided paths with the GUFI root path
591-
paths = [os.path.normpath(os.path.sep.join([config['IndexRoot'], path])) for path in paths]
590+
paths = [os.path.normpath(os.path.sep.join([config.indexroot(), path])) for path in paths]
592591

593592
# parse expressions without the 'real' and path arguments
594593
args, unknown = expression_parser.parse_known_args(argv[i:])
@@ -608,8 +607,10 @@ def run(argv, config_path):
608607
output = build_output(args)
609608

610609
# create the query command
611-
query_cmd = [config['Exec'],
612-
'-n', str(config['Threads'])]
610+
query_cmd = [config.executable(),
611+
'-n', str(config.threads()),
612+
'-B', str(config.outputbuffer())
613+
]
613614

614615
S = None
615616
E = None
@@ -697,13 +698,10 @@ def run(argv, config_path):
697698
if args.fprint:
698699
query_cmd += ['-o', args.fprint]
699700

700-
if args.output_buffer:
701-
query_cmd += ['-B', str(args.output_buffer)]
702-
703701
query = subprocess.Popen(query_cmd + paths) # positional arguments must appear after flags
704702
query.communicate() # block until query finishes
705703

706704
return query.returncode
707705

708706
if __name__=='__main__':
709-
sys.exit(run(sys.argv, gufi_config.DEFAULT_CONFIG_PATH))
707+
sys.exit(run(sys.argv, gufi_config.DEFAULT_PATH))

scripts/gufi_ls

+9-12
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def build_order_by_recursive(args):
222222
# argv[0] should be the command name
223223
def run(argv, config_path):
224224
# find and parse the configuration file first
225-
config = gufi_config.server_config(config_path)
225+
config = gufi_config.Server(config_path)
226226

227227
class FullTimeAction(argparse.Action):
228228
def __init__(self, option_strings, dest, nargs=None, **kwargs):
@@ -259,8 +259,7 @@ def run(argv, config_path):
259259
parser.add_argument('paths', type=str, action='append' , nargs='*')
260260

261261
# GUFI specific arguments
262-
parser.add_argument('--delim', metavar='c', dest='delim', type=gufi_common.get_char, default=' ', help='delimiter separating output columns')
263-
parser.add_argument('--output-buffer', metavar='bytes', dest='output_buffer', type=gufi_common.get_non_negative, default=4096, help='Size of each thread\'s output buffer')
262+
parser.add_argument('--delim', metavar='c', dest='delim', type=gufi_common.get_char, default=' ', help='delimiter separating output columns')
264263
parser.add_argument('--in-memory-name', metavar='name', dest='inmemory_name', type=str, default='out', help='Name of in-memory database when -R is used')
265264
parser.add_argument('--nlink-width', metavar='chars', dest='nlink_width', type=gufi_common.get_non_negative, default=2, help='Width of nlink column')
266265
parser.add_argument('--size-width', metavar='chars', dest='size_width', type=gufi_common.get_non_negative, default=10, help='Width of size column')
@@ -278,10 +277,10 @@ def run(argv, config_path):
278277

279278
for path in args.paths[0]:
280279
# prepend the provided paths with the GUFI index root
281-
fullpath = os.path.normpath(os.path.sep.join([config['IndexRoot'], path]))
280+
fullpath = os.path.normpath(os.path.sep.join([config.indexroot(), path]))
282281

283282
# create the base command
284-
query_cmd = [config['Exec']]
283+
query_cmd = [config.executable()]
285284

286285
# directory and non-directory paths are processed differently
287286
if os.path.isdir(fullpath):
@@ -340,7 +339,6 @@ def run(argv, config_path):
340339
None))
341340

342341
query_cmd += ['-e', '0',
343-
'-n', str(config['Threads']),
344342
'-I', I,
345343
'-S', S,
346344
'-E', E,
@@ -378,7 +376,6 @@ def run(argv, config_path):
378376

379377

380378
query_cmd += ['-e', '0',
381-
'-n', str(config['Threads']),
382379
'-I', I,
383380
'-S', S,
384381
'-E', E,
@@ -388,6 +385,8 @@ def run(argv, config_path):
388385
'-z', '1',
389386
'-a']
390387

388+
query_cmd += ['-n', str(config.threads())]
389+
391390
else:
392391
# if the path is not a directory, remove the last part of the path
393392
fullpath, match_name = os.path.split(fullpath)
@@ -400,17 +399,15 @@ def run(argv, config_path):
400399
None)
401400

402401
query_cmd += ['-e', '1',
403-
'-n', str(config['Threads']),
404402
'-E', E,
405403
'-y', '0',
406404
'-z', '0']
407405

406+
query_cmd += ['-B', str(config.outputbuffer())]
407+
408408
if args.delim:
409409
query_cmd += ['-d', args.delim]
410410

411-
if args.output_buffer:
412-
query_cmd += ['-B', str(args.output_buffer)]
413-
414411
query = subprocess.Popen(query_cmd + [fullpath])
415412
query.communicate() # block until query finishes
416413

@@ -420,4 +417,4 @@ def run(argv, config_path):
420417
return rc
421418

422419
if __name__=='__main__':
423-
sys.exit(run(sys.argv, gufi_config.DEFAULT_CONFIG_PATH))
420+
sys.exit(run(sys.argv, gufi_config.DEFAULT_PATH))

0 commit comments

Comments
 (0)