2626#
2727
2828import argparse
29- from collections import defaultdict
30- from configparser import ConfigParser
31- from enum import Enum
3229import importlib
3330import os
34- import pip
3531import sys
32+ from collections import defaultdict
33+ from configparser import ConfigParser
34+ from enum import Enum
3635from types import SimpleNamespace
37- from pip ._internal .req .req_file import parse_requirements
3836
37+ import pip
38+ from pip ._internal .req .req_file import parse_requirements
3939
4040_SCRIPT_DIR = os .path .dirname (os .path .abspath (__file__ ))
41- _LOCAL_MIG_BASE = os .path .normpath (os .path .join (_SCRIPT_DIR , ' ../..' ))
41+ _LOCAL_MIG_BASE = os .path .normpath (os .path .join (_SCRIPT_DIR , " ../.." ))
4242
4343sys .path .append (_LOCAL_MIG_BASE )
4444
45- FEATURES_FILE = os .path .join (_LOCAL_MIG_BASE , 'FEATURES.ini' )
46- FEATURES_REQUIREMENTS_DIR = os .path .join (_LOCAL_MIG_BASE , 'mig/install/requirements' )
45+ FEATURES_FILE = os .path .join (_LOCAL_MIG_BASE , "FEATURES.ini" )
46+ FEATURES_REQUIREMENTS_DIR = os .path .join (
47+ _LOCAL_MIG_BASE , "mig/install/requirements"
48+ )
4749PIP_OVERRIDES = {
48- ' CLOUD' : {
49- ' openstacksdk' : ' OPENSTACKSDK_VERSION_OVERRIDE' ,
50+ " CLOUD" : {
51+ " openstacksdk" : " OPENSTACKSDK_VERSION_OVERRIDE" ,
5052 },
51- ' MIGUX' : {
52- ' migux' : ' MIGUX_VERSION_OVERRIDE' ,
53+ " MIGUX" : {
54+ " migux" : " MIGUX_VERSION_OVERRIDE" ,
5355 },
5456}
55- _VERSIONCHARS = ('=' , '<' , '>' )
56- _TRUTH_STRINGS = set ((' True' , ' true' , ' yes' , '1' ))
57+ _VERSIONCHARS = ("=" , "<" , ">" )
58+ _TRUTH_STRINGS = set ((" True" , " true" , " yes" , "1" ))
5759
5860
59- def warn (msg = '' ):
61+ def warn (msg = "" ):
6062 """
6163 Wrapper function for printing to stderr.
6264 """
@@ -76,10 +78,17 @@ def __init__(self, interpretation_by_feature_name, overrides_supported):
7678 self ._overrides_by_feature = {}
7779 self ._overrides_supported = overrides_supported
7880
79- for feature_name , interpretation in interpretation_by_feature_name .items ():
81+ for (
82+ feature_name ,
83+ interpretation ,
84+ ) in interpretation_by_feature_name .items ():
8085 self ._enabled_by_feature [feature_name ] = interpretation .enabled
81- self ._requirements_by_feature [feature_name ] = interpretation .requirements
82- self ._requirements_file_by_feature [feature_name ] = interpretation .requirements_file
86+ self ._requirements_by_feature [feature_name ] = (
87+ interpretation .requirements
88+ )
89+ self ._requirements_file_by_feature [feature_name ] = (
90+ interpretation .requirements_file
91+ )
8392
8493 def apply_enabled (self , enabled_by_feature_name ):
8594 """
@@ -117,8 +126,9 @@ def generate_pip_args(self, detect_installed=False):
117126 per_package_args = []
118127
119128 for feature_name in self .list_enabled_features ():
120- installable_packages = self .generate_pip_args_for_feature (feature_name ,
121- detect_installed = detect_installed )
129+ installable_packages = self .generate_pip_args_for_feature (
130+ feature_name , detect_installed = detect_installed
131+ )
122132 if not installable_packages :
123133 continue
124134 per_package_args .append (installable_packages )
@@ -136,7 +146,7 @@ def generate_pip_args_for_feature(self, feature_name, *, detect_installed):
136146 # no overrides detected and we do not need to check for the
137147 # dependencies being already installed therefore we can install
138148 # by simply using the requirements file as-is
139- return ['-r' , self ._requirements_file_by_feature [feature_name ]]
149+ return ["-r" , self ._requirements_file_by_feature [feature_name ]]
140150
141151 package_args = []
142152 overridden_package_names = set (overrides .keys ())
@@ -173,29 +183,46 @@ def list_enabled_features(self, return_as=list):
173183 Return the names of features recorded as enabled.
174184 """
175185
176- return return_as ((feature_name for feature_name in self .feature_names
177- if self ._enabled_by_feature [feature_name ]))
186+ return return_as (
187+ (
188+ feature_name
189+ for feature_name in self .feature_names
190+ if self ._enabled_by_feature [feature_name ]
191+ )
192+ )
178193
179194 def required_package_names (self , feature_name ):
180195 """
181196 Return the set of required packages for a named feature.
182197 """
183- return set ((Features ._strip_version_if_present (entry .requirement )
184- for entry in self ._requirements_by_feature [feature_name ]))
198+ return set (
199+ (
200+ Features ._strip_version_if_present (entry .requirement )
201+ for entry in self ._requirements_by_feature [feature_name ]
202+ )
203+ )
185204
186205 @staticmethod
187- def _interpret_feature_definition (feature_name , feature_definition , requirements_dir ):
206+ def _interpret_feature_definition (
207+ feature_name , feature_definition , requirements_dir
208+ ):
188209 """
189210 Convert a named feature section within the features file to a
190211 structured intepretation suitable for consumption by the logic.
191212 """
192213
193- enabled = feature_definition .getboolean ('default_on' , fallback = False )
194- has_requirements = feature_definition .getboolean ('has_requirements' , fallback = True )
214+ enabled = feature_definition .getboolean ("default_on" , fallback = False )
215+ has_requirements = feature_definition .getboolean (
216+ "has_requirements" , fallback = True
217+ )
195218
196219 if has_requirements :
197- requirements_file = os .path .join (requirements_dir , f"{ feature_name .lower ()} -requirements.txt" )
198- requirements = list (parse_requirements (requirements_file , session = None ))
220+ requirements_file = os .path .join (
221+ requirements_dir , f"{ feature_name .lower ()} -requirements.txt"
222+ )
223+ requirements = list (
224+ parse_requirements (requirements_file , session = None )
225+ )
199226 else :
200227 requirements = []
201228
@@ -240,13 +267,17 @@ def expand_definitions(definitions, requirements_dir):
240267 definitions_iterator = iter (definitions .items ())
241268 next (definitions_iterator ) # skip default section
242269
243- return {feature_name : Features ._interpret_feature_definition (feature_name ,
244- feature_definition ,
245- requirements_dir )
246- for feature_name , feature_definition in definitions_iterator }
270+ return {
271+ feature_name : Features ._interpret_feature_definition (
272+ feature_name , feature_definition , requirements_dir
273+ )
274+ for feature_name , feature_definition in definitions_iterator
275+ }
247276
248277 @classmethod
249- def from_definitions_file (cls , features_file , requirements_dir , overrides_supported = {}):
278+ def from_definitions_file (
279+ cls , features_file , requirements_dir , overrides_supported = {}
280+ ):
250281 """
251282 Return a Features instance populated with the features declared
252283 within the specified definitions file.
@@ -256,7 +287,10 @@ def from_definitions_file(cls, features_file, requirements_dir, overrides_suppor
256287 with open (features_file ) as thefile :
257288 definitions = ConfigParser ()
258289 definitions .read_file (thefile )
259- return cls (Features .expand_definitions (definitions , requirements_dir ), overrides_supported )
290+ return cls (
291+ Features .expand_definitions (definitions , requirements_dir ),
292+ overrides_supported ,
293+ )
260294
261295 @staticmethod
262296 def match_env_dict (features , env_dict ):
@@ -278,17 +312,23 @@ def enabled_or_fallback(feature_name):
278312 overrides_by_feature_name = defaultdict (dict )
279313
280314 for feature_name in features .feature_names :
281- enabled_by_feature_name [feature_name ] = enabled_or_fallback (feature_name )
315+ enabled_by_feature_name [feature_name ] = enabled_or_fallback (
316+ feature_name
317+ )
282318
283- env_override_flags = features ._overrides_supported .get (feature_name , None )
319+ env_override_flags = features ._overrides_supported .get (
320+ feature_name , None
321+ )
284322 if not env_override_flags :
285323 continue
286324
287325 for package_name , flag_name in env_override_flags .items ():
288326 override_version = env_dict .get (flag_name , None )
289327 if not override_version :
290328 continue
291- overrides_by_feature_name [feature_name ][package_name ] = override_version
329+ overrides_by_feature_name [feature_name ][
330+ package_name
331+ ] = override_version
292332
293333 return enabled_by_feature_name , overrides_by_feature_name
294334
@@ -312,16 +352,23 @@ def match_configuration_file(features, configuration_file):
312352 """
313353
314354 from mig .shared .conf import get_configuration_object
315- configuration = get_configuration_object (configuration_file , skip_log = True , disable_auth_log = True )
355+
356+ configuration = get_configuration_object (
357+ configuration_file , skip_log = True , disable_auth_log = True
358+ )
316359
317360 def enabled_or_fallback (feature_name ):
318361 try :
319- return getattr (configuration , f"site_enable_{ feature_name .lower ()} " )
362+ return getattr (
363+ configuration , f"site_enable_{ feature_name .lower ()} "
364+ )
320365 except AttributeError :
321366 return features .feature_is_enabled (feature_name )
322367
323- enabled_by_feature_name = {feature_name : enabled_or_fallback (feature_name )
324- for feature_name in features .feature_names }
368+ enabled_by_feature_name = {
369+ feature_name : enabled_or_fallback (feature_name )
370+ for feature_name in features .feature_names
371+ }
325372 return enabled_by_feature_name , {}
326373
327374
@@ -331,16 +378,24 @@ def subcommand_enabled(features, args, print=print, warn=warn):
331378 """
332379
333380 if args .c :
334- enabled_by_feature_name = Features .match_configuration_file (features , args .c )
381+ enabled_by_feature_name = Features .match_configuration_file (
382+ features , args .c
383+ )
335384 features .apply_enabled (enabled_by_feature_name )
336385 elif args .dotenv :
337- enabled_by_feature_name , _ = Features .match_dotenv_file (features , args .dotenv )
386+ enabled_by_feature_name , _ = Features .match_dotenv_file (
387+ features , args .dotenv
388+ )
338389 features .apply_enabled (enabled_by_feature_name )
339390 elif args .env :
340- enabled_by_feature_name , overrides_by_feature_name = Features .match_env_dict (features , args .env )
391+ enabled_by_feature_name , overrides_by_feature_name = (
392+ Features .match_env_dict (features , args .env )
393+ )
341394 features .apply_enabled (enabled_by_feature_name )
342395 else :
343- warn ("no feature coniguration available; showing those enabled by default only" )
396+ warn (
397+ "no feature coniguration available; showing those enabled by default only"
398+ )
344399 print (f"enabled features: { ', ' .join (features .list_enabled_features ())} " )
345400
346401 return 0
@@ -352,20 +407,30 @@ def subcommand_install(features, args, print=print, warn=warn):
352407 """
353408
354409 if args .c :
355- enabled_by_feature_name = Features .match_configuration_file (features , args .c )
410+ enabled_by_feature_name = Features .match_configuration_file (
411+ features , args .c
412+ )
356413 features .apply_enabled (enabled_by_feature_name )
357414 elif args .dotenv :
358- enabled_by_feature_name , overrides_by_feature_name = Features .match_dotenv_file (features , args .dotenv )
415+ enabled_by_feature_name , overrides_by_feature_name = (
416+ Features .match_dotenv_file (features , args .dotenv )
417+ )
359418 features .apply_enabled (enabled_by_feature_name )
360419 elif args .env :
361- enabled_by_feature_name , overrides_by_feature_name = Features .match_env_dict (features , args .env )
420+ enabled_by_feature_name , overrides_by_feature_name = (
421+ Features .match_env_dict (features , args .env )
422+ )
362423 features .apply_enabled (enabled_by_feature_name )
363424 features .apply_overrides (overrides_by_feature_name )
364425 else :
365- warn ("no feature coniguration available; showing those enabled by default only" )
426+ warn (
427+ "no feature coniguration available; showing those enabled by default only"
428+ )
366429 warn ()
367430
368- all_pip_args = features .generate_pip_args (detect_installed = args .detect_installed )
431+ all_pip_args = features .generate_pip_args (
432+ detect_installed = args .detect_installed
433+ )
369434
370435 if args .check :
371436 for pip_args in all_pip_args :
@@ -396,21 +461,27 @@ def main(argv):
396461 """
397462
398463 parser = argparse .ArgumentParser ()
399- subparsers = parser .add_subparsers (dest = ' command' )
464+ subparsers = parser .add_subparsers (dest = " command" )
400465
401- show_command = subparsers .add_parser (' show' )
466+ show_command = subparsers .add_parser (" show" )
402467
403- enabled_command = subparsers .add_parser ('enabled' )
404- enabled_command .add_argument ('-c' , default = None )
405- enabled_command .add_argument ('--dotenv' , default = None , type = os .path .abspath )
406- enabled_command .add_argument ('--env' , action = 'store_const' , const = os .environ )
468+ enabled_command = subparsers .add_parser ("enabled" )
469+ enabled_command .add_argument ("-c" , default = None )
470+ enabled_command .add_argument ("--dotenv" , default = None , type = os .path .abspath )
471+ enabled_command .add_argument (
472+ "--env" , action = "store_const" , const = os .environ
473+ )
407474
408- install_command = subparsers .add_parser ('install' )
409- install_command .add_argument ('-c' , default = None )
410- install_command .add_argument ('--check' , action = 'store_true' , default = False )
411- install_command .add_argument ('--detect_installed' , action = 'store_true' , default = False )
412- install_command .add_argument ('--dotenv' , default = None , type = os .path .abspath )
413- install_command .add_argument ('--env' , action = 'store_const' , const = os .environ )
475+ install_command = subparsers .add_parser ("install" )
476+ install_command .add_argument ("-c" , default = None )
477+ install_command .add_argument ("--check" , action = "store_true" , default = False )
478+ install_command .add_argument (
479+ "--detect_installed" , action = "store_true" , default = False
480+ )
481+ install_command .add_argument ("--dotenv" , default = None , type = os .path .abspath )
482+ install_command .add_argument (
483+ "--env" , action = "store_const" , const = os .environ
484+ )
414485
415486 args = parser .parse_args (args = argv )
416487
@@ -420,6 +491,7 @@ def main(argv):
420491
421492 return args_main (parser .parse_args (args = argv ))
422493
494+
423495def args_main (args , * , print = print , warn = warn , features = None ):
424496 """
425497 Internal helper for executing the parsed arguments.
@@ -440,5 +512,5 @@ def args_main(args, *, print=print, warn=warn, features=None):
440512 return 1
441513
442514
443- if __name__ == ' __main__' :
515+ if __name__ == " __main__" :
444516 sys .exit (main (sys .argv [1 :]))
0 commit comments