88
99 src/loggedfs/cli.py: Command line interface
1010
11- Copyright (C) 2017 Sebastian M. Ernst <[email protected] > 11+ Copyright (C) 2017-2018 Sebastian M. Ernst <[email protected] > 1212
1313<LICENSE_BLOCK>
1414The contents of this file are subject to the Apache License
2929# IMPORT
3030# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3131
32- from pprint import pformat as pf
32+ from collections import OrderedDict
3333
3434from .core import loggedfs_factory
3535
5757 type = click .File (mode = 'r' ),
5858 help = 'Use the "config-file" to filter what you want to log.'
5959 )
60+ @click .option (
61+ '-s' ,
62+ is_flag = True ,
63+ help = 'Deactivate logging to syslog.'
64+ )
6065@click .option (
6166 '-l' ,
6267 # type = click.File(mode = 'a'),
6368 type = click .Path (file_okay = True , dir_okay = False , resolve_path = True ),
64- help = ('Use the "log-file" to write logs to. If no log file is specified'
65- 'then logs are only written to syslog or to stdout, depending on -f.' )
69+ help = ('Use the "log-file" to write logs to.' )
6670 )
6771@click .argument (
6872 'directory' ,
6973 type = click .Path (exists = True , file_okay = False , dir_okay = True , resolve_path = True )
7074 )
71- def cli_entry (f , p , c , l , directory ):
75+ def cli_entry (f , p , c , s , l , directory ):
7276 """LoggedFS-python is a transparent fuse-filesystem which allows to log
7377 every operations that happens in the backend filesystem. Logs can be written
7478 to syslog, to a file, or to the standard output. LoggedFS comes with an XML
@@ -78,31 +82,59 @@ def cli_entry(f, p, c, l, directory):
7882 filters are regular expressions.
7983 """
8084
81- click .echo (pf ((
82- f , p , c , l , directory
83- )))
84-
8585 loggedfs_factory (
8686 directory ,
87- no_daemon_bool = f ,
88- allow_other = p ,
89- loggedfs_param_dict = __process_config__ (c , f ),
90- log_file = l
87+ ** __process_config__ (c , l , s , f , p )
9188 )
9289
9390
94- def __process_config__ (config_fh , no_daemon_bool ):
95-
96- def __process_xml__ (in_xml ):
97- return xmltodict .parse (in_xml )['loggedFS' ]
98-
91+ def __process_config__ (
92+ config_fh ,
93+ log_file ,
94+ log_syslog_off ,
95+ fuse_foreground_bool ,
96+ fuse_allowother_bool
97+ ):
98+
99+ def proc_filter_item (in_item ):
100+ return {
101+ 'extension' : in_item ['@extension' ],
102+ 'uid' : in_item ['@uid' ],
103+ 'action' : in_item ['@action' ],
104+ 'retname' : in_item ['@retname' ]
105+ }
106+
107+ def proc_filter_list (in_list ):
108+ if in_list is None :
109+ return []
110+ if not isinstance (in_list , list ):
111+ return [proc_filter_item (in_list )]
112+ return [proc_filter_item (item ) for item in in_list ]
113+
114+ config_dict = OrderedDict ({
115+ '@logEnabled' : True ,
116+ '@printProcessName' : True ,
117+ 'includes' : [],
118+ 'excludes' : []
119+ })
120+
121+ config_file = None
99122 if config_fh is not None :
100- param = __process_xml__ (config_fh .read ())
101- elif False : # TODO check /etc
102- param = {} # TODO fetch from /etc
103- else :
104- param = {}
105-
106- param .update ({'daemon' : not no_daemon_bool })
107-
108- return param
123+ config_file = config_fh .name
124+ config_dict .update (xmltodict .parse (config_fh .read ())['loggedFS' ])
125+ config_fh .close ()
126+
127+ for f_type in ['includes' , 'excludes' ]:
128+ config_dict [f_type ] = proc_filter_list (config_dict [f_type ][f_type [:- 1 ]])
129+
130+ return {
131+ 'log_includes' : config_dict ['includes' ],
132+ 'log_excludes' : config_dict ['excludes' ],
133+ 'log_file' : log_file ,
134+ 'log_syslog' : not log_syslog_off ,
135+ 'log_configmsg' : 'LoggedFS-python using configuration file %s' % config_file ,
136+ 'log_enabled' : config_dict ['@logEnabled' ],
137+ 'log_printprocessname' : config_dict ['@printProcessName' ],
138+ 'fuse_foreground_bool' : fuse_foreground_bool ,
139+ 'fuse_allowother_bool' : fuse_allowother_bool
140+ }
0 commit comments