29
29
import user_sync .config
30
30
import user_sync .connector .directory
31
31
import user_sync .connector .umapi
32
+ import user_sync .helper
32
33
import user_sync .lockfile
33
34
import user_sync .rules
34
35
from user_sync .error import AssertionException
37
38
LOG_STRING_FORMAT = '%(asctime)s %(process)d %(levelname)s %(name)s - %(message)s'
38
39
LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
39
40
41
+ # file global logger, defined early so later functions can refer to it.
42
+ logger = logging .getLogger ('main' )
43
+
44
+
45
+ def init_console_log ():
46
+ handler = logging .StreamHandler (sys .stdout )
47
+ handler .setFormatter (logging .Formatter (LOG_STRING_FORMAT , LOG_DATE_FORMAT ))
48
+ root_logger = logging .getLogger ()
49
+ root_logger .addHandler (handler )
50
+ root_logger .setLevel (logging .DEBUG )
51
+ return handler
52
+
53
+ # file global console_log_handler, defined early so later functions can refer to it.
54
+ console_log_handler = init_console_log ()
55
+
40
56
41
57
def process_args ():
42
58
parser = argparse .ArgumentParser (description = 'User Sync from Adobe' )
@@ -64,9 +80,9 @@ def process_args():
64
80
'side is updated to match.' ,
65
81
action = 'store_true' , dest = 'update_user_info' )
66
82
parser .add_argument ('--process-groups' ,
67
- help = 'if the membership in mapped groups differs between the enterprise directory and Adobe sides, '
83
+ help = 'if membership in mapped groups differs between the enterprise directory and Adobe sides, '
68
84
'the group membership is updated on the Adobe side so that the memberships in mapped '
69
- 'groups matches those on the enterprise directory side.' ,
85
+ 'groups match those on the enterprise directory side.' ,
70
86
action = 'store_true' , dest = 'manage_groups' )
71
87
parser .add_argument ('--adobe-only-user-action' ,
72
88
help = "specify what action to take on Adobe users that don't match users from the "
@@ -94,19 +110,10 @@ def process_args():
94
110
return parser .parse_args ()
95
111
96
112
97
- def init_console_log ():
98
- console_log_handler = logging .StreamHandler (sys .stdout )
99
- console_log_handler .setFormatter (logging .Formatter (LOG_STRING_FORMAT , LOG_DATE_FORMAT ))
100
- root_logger = logging .getLogger ()
101
- root_logger .addHandler (console_log_handler )
102
- root_logger .setLevel (logging .DEBUG )
103
- return console_log_handler
104
-
105
-
106
113
def init_log (logging_config ):
107
- '''
114
+ """
108
115
:type logging_config: user_sync.config.DictConfig
109
- '''
116
+ """
110
117
builder = user_sync .config .OptionsBuilder (logging_config )
111
118
builder .set_bool_value ('log_to_file' , False )
112
119
builder .set_string_value ('file_log_directory' , 'logs' )
@@ -123,66 +130,66 @@ def init_log(logging_config):
123
130
}
124
131
125
132
console_log_level = level_lookup .get (options ['console_log_level' ])
126
- if ( console_log_level == None ) :
133
+ if console_log_level is None :
127
134
console_log_level = logging .INFO
128
135
logger .log (logging .WARNING , 'Unknown console log level: %s setting to info' % options ['console_log_level' ])
129
136
console_log_handler .setLevel (console_log_level )
130
137
131
- if options ['log_to_file' ] == True :
138
+ if options ['log_to_file' ]:
132
139
unknown_file_log_level = False
133
140
file_log_level = level_lookup .get (options ['file_log_level' ])
134
- if ( file_log_level == None ) :
141
+ if file_log_level is None :
135
142
file_log_level = logging .INFO
136
143
unknown_file_log_level = True
137
144
file_log_directory = options ['file_log_directory' ]
138
145
if not os .path .exists (file_log_directory ):
139
146
os .makedirs (file_log_directory )
140
147
141
148
file_path = os .path .join (file_log_directory , datetime .date .today ().isoformat () + ".log" )
142
- fileHandler = logging .FileHandler (file_path )
143
- fileHandler .setLevel (file_log_level )
144
- fileHandler .setFormatter (logging .Formatter (LOG_STRING_FORMAT , LOG_DATE_FORMAT ))
145
- logging .getLogger ().addHandler (fileHandler )
149
+ file_handler = logging .FileHandler (file_path )
150
+ file_handler .setLevel (file_log_level )
151
+ file_handler .setFormatter (logging .Formatter (LOG_STRING_FORMAT , LOG_DATE_FORMAT ))
152
+ logging .getLogger ().addHandler (file_handler )
146
153
if unknown_file_log_level :
147
154
logger .log (logging .WARNING , 'Unknown file log level: %s setting to info' % options ['file_log_level' ])
148
155
149
156
150
157
def begin_work (config_loader ):
151
- '''
158
+ """
152
159
:type config_loader: user_sync.config.ConfigLoader
153
- '''
160
+ """
154
161
155
162
directory_groups = config_loader .get_directory_groups ()
156
163
primary_umapi_config , secondary_umapi_configs = config_loader .get_umapi_options ()
157
164
rule_config = config_loader .get_rule_options ()
158
165
159
166
# process mapped configuration after the directory groups have been loaded, as mapped setting depends on this.
160
- if ( rule_config ['directory_group_mapped' ]) :
167
+ if rule_config ['directory_group_mapped' ]:
161
168
rule_config ['directory_group_filter' ] = set (directory_groups .iterkeys ())
162
169
163
170
# make sure that all the adobe groups are from known umapi connector names
164
171
referenced_umapi_names = set ()
165
172
for groups in directory_groups .itervalues ():
166
173
for group in groups :
167
174
umapi_name = group .umapi_name
168
- if ( umapi_name != user_sync .rules .PRIMARY_UMAPI_NAME ) :
175
+ if umapi_name != user_sync .rules .PRIMARY_UMAPI_NAME :
169
176
referenced_umapi_names .add (umapi_name )
170
177
referenced_umapi_names .difference_update (secondary_umapi_configs .iterkeys ())
171
178
172
- if ( len (referenced_umapi_names ) > 0 ) :
179
+ if len (referenced_umapi_names ) > 0 :
173
180
raise AssertionException ('Adobe groups reference unknown umapi connectors: %s' % referenced_umapi_names )
174
181
175
182
directory_connector = None
176
183
directory_connector_options = None
177
184
directory_connector_module_name = config_loader .get_directory_connector_module_name ()
178
- if ( directory_connector_module_name != None ) :
185
+ if directory_connector_module_name is not None :
179
186
directory_connector_module = __import__ (directory_connector_module_name , fromlist = ['' ])
180
187
directory_connector = user_sync .connector .directory .DirectoryConnector (directory_connector_module )
181
188
directory_connector_options = config_loader .get_directory_connector_options (directory_connector .name )
182
189
183
190
config_loader .check_unused_config_keys ()
184
191
185
- if ( directory_connector != None and directory_connector_options != None ) :
192
+ if directory_connector is not None and directory_connector_options is not None :
186
193
# specify the default user_identity_type if it's not already specified in the options
187
194
if 'user_identity_type' not in directory_connector_options :
188
195
directory_connector_options ['user_identity_type' ] = rule_config ['new_account_type' ]
@@ -198,7 +205,7 @@ def begin_work(config_loader):
198
205
umapi_connectors = user_sync .rules .UmapiConnectors (umapi_primary_connector , umapi_other_connectors )
199
206
200
207
rule_processor = user_sync .rules .RuleProcessor (rule_config )
201
- if ( len (directory_groups ) == 0 and rule_processor .will_manage_groups () ):
208
+ if len (directory_groups ) == 0 and rule_processor .will_manage_groups ():
202
209
logger .warn ('no groups mapped in config file' )
203
210
rule_processor .run (directory_groups , directory_connector , umapi_connectors )
204
211
@@ -213,14 +220,14 @@ def create_config_loader(args):
213
220
214
221
215
222
def create_config_loader_options (args ):
216
- '''
223
+ """
217
224
This is where all the command-line arguments get set as options in the main config
218
225
so that it appears as if they were loaded as part of the main configuration file.
219
226
If you add an option that is supposed to be set from the command line here, you
220
227
had better make sure you add it to the ones read in get_rule_options.
221
228
:param args: the command-line args as parsed
222
229
:return: the configured options for the config loader.
223
- '''
230
+ """
224
231
config_options = {
225
232
'delete_strays' : False ,
226
233
'directory_connector_module_name' : None ,
@@ -241,17 +248,17 @@ def create_config_loader_options(args):
241
248
# --users
242
249
users_args = args .users
243
250
users_action = None if not users_args else user_sync .helper .normalize_string (users_args .pop (0 ))
244
- if ( users_action == None or users_action == 'all' ) :
251
+ if users_action is None or users_action == 'all' :
245
252
config_options ['directory_connector_module_name' ] = 'user_sync.connector.directory_ldap'
246
- elif ( users_action == 'file' ) :
253
+ elif users_action == 'file' :
247
254
if len (users_args ) == 0 :
248
255
raise AssertionException ('Missing file path for --users %s [file_path]' % users_action )
249
256
config_options ['directory_connector_module_name' ] = 'user_sync.connector.directory_csv'
250
257
config_options ['directory_connector_overridden_options' ] = {'file_path' : users_args .pop (0 )}
251
- elif ( users_action == 'mapped' ) :
258
+ elif users_action == 'mapped' :
252
259
config_options ['directory_connector_module_name' ] = 'user_sync.connector.directory_ldap'
253
260
config_options ['directory_group_mapped' ] = True
254
- elif ( users_action == 'group' ) :
261
+ elif users_action == 'group' :
255
262
if len (users_args ) == 0 :
256
263
raise AssertionException ('Missing groups for --users %s [groups]' % users_action )
257
264
config_options ['directory_connector_module_name' ] = 'user_sync.connector.directory_ldap'
@@ -260,7 +267,7 @@ def create_config_loader_options(args):
260
267
raise AssertionException ('Unknown argument --users %s' % users_action )
261
268
262
269
username_filter_pattern = args .username_filter_pattern
263
- if ( username_filter_pattern ) :
270
+ if username_filter_pattern :
264
271
try :
265
272
compiled_expression = re .compile (r'\A' + username_filter_pattern + r'\Z' , re .IGNORECASE )
266
273
except Exception as e :
@@ -272,19 +279,19 @@ def create_config_loader_options(args):
272
279
adobe_action_args = args .adobe_only_user_action
273
280
if adobe_action_args is not None :
274
281
adobe_action = None if not adobe_action_args else user_sync .helper .normalize_string (adobe_action_args .pop (0 ))
275
- if ( adobe_action == None or adobe_action == 'preserve' ) :
282
+ if adobe_action is None or adobe_action == 'preserve' :
276
283
pass # no option settings needed
277
- elif ( adobe_action == 'exclude' ) :
284
+ elif adobe_action == 'exclude' :
278
285
config_options ['exclude_strays' ] = True
279
- elif ( adobe_action == 'write-file' ) :
286
+ elif adobe_action == 'write-file' :
280
287
if not adobe_action_args :
281
288
raise AssertionException ('Missing file path for --adobe-only-user-action %s [file_path]' % adobe_action )
282
289
config_options ['stray_list_output_path' ] = adobe_action_args .pop (0 )
283
- elif ( adobe_action == 'delete' ) :
290
+ elif adobe_action == 'delete' :
284
291
config_options ['delete_strays' ] = True
285
- elif ( adobe_action == 'remove' ) :
292
+ elif adobe_action == 'remove' :
286
293
config_options ['remove_strays' ] = True
287
- elif ( adobe_action == 'remove-adobe-groups' ) :
294
+ elif adobe_action == 'remove-adobe-groups' :
288
295
config_options ['disentitle_strays' ] = True
289
296
else :
290
297
raise AssertionException ('Unknown argument --adobe-only-user-action %s' % adobe_action )
@@ -305,11 +312,11 @@ def create_config_loader_options(args):
305
312
306
313
307
314
def log_parameters (args ):
308
- '''
315
+ """
309
316
Log the invocation parameters to make it easier to diagnose problem with customers
310
317
:param args: namespace
311
318
:return: None
312
- '''
319
+ """
313
320
logger .info ('------- Invocation parameters -------' )
314
321
logger .info (' ' .join (sys .argv ))
315
322
logger .debug ('-------- Internal parameters --------' )
@@ -349,7 +356,7 @@ def main():
349
356
logger .critical ("A different User Sync process is currently running." )
350
357
351
358
except AssertionException as e :
352
- if ( not e .is_reported () ):
359
+ if not e .is_reported ():
353
360
logger .critical (e .message )
354
361
e .set_reported ()
355
362
except :
@@ -359,12 +366,9 @@ def main():
359
366
pass
360
367
361
368
finally :
362
- if ( run_stats != None ) :
369
+ if run_stats is not None :
363
370
run_stats .log_end (logger )
364
371
365
372
366
- console_log_handler = init_console_log ()
367
- logger = logging .getLogger ('main' )
368
-
369
373
if __name__ == '__main__' :
370
374
main ()
0 commit comments