|
| 1 | +import sys |
| 2 | +import envconfig |
| 3 | +import importlib |
| 4 | +import inspect |
| 5 | +import autorep |
| 6 | + |
| 7 | +from autorep import target, source, clustering, replication |
| 8 | + |
| 9 | +def get_driver_class(modulename, baseclass): |
| 10 | + """ pack |
| 11 | + """ |
| 12 | + mod = importlib.import_module(modulename) |
| 13 | + klass = None |
| 14 | + |
| 15 | + for klassname in dir(mod): |
| 16 | + tmpklass = getattr(mod, klassname) |
| 17 | + if not inspect.isclass(tmpklass): |
| 18 | + continue |
| 19 | + if issubclass(tmpklass, baseclass) and not tmpklass == baseclass: |
| 20 | + if klass is not None: |
| 21 | + raise envconfig.InvalidConfig("Multiple candidate classes available in %s" % |
| 22 | + modulename) |
| 23 | + klass = tmpklass |
| 24 | + |
| 25 | + return klass |
| 26 | + |
| 27 | +env_vars_def = { |
| 28 | + 'AUTOREP_TARGET_DRIVER_MODULE' : { |
| 29 | + envconfig.Options.parser: envconfig.Parser.String, |
| 30 | + envconfig.Options.required: lambda conf: True, |
| 31 | + }, |
| 32 | + 'AUTOREP_SOURCE_DRIVER_MODULE' : { |
| 33 | + envconfig.Options.parser: envconfig.Parser.String, |
| 34 | + envconfig.Options.required: lambda conf: True, |
| 35 | + }, |
| 36 | + 'AUTOREP_CLUSTERING_DRIVER_MODULE' : { |
| 37 | + envconfig.Options.parser: envconfig.Parser.String, |
| 38 | + envconfig.Options.required: lambda conf: True, |
| 39 | + }, |
| 40 | + 'AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE' : { |
| 41 | + envconfig.Options.parser: envconfig.Parser.String, |
| 42 | + envconfig.Options.required: lambda conf: True, |
| 43 | + }, |
| 44 | + 'AUTOREP_UPDATE_PERIOD' : { |
| 45 | + envconfig.Options.parser: envconfig.Parser.Integer, |
| 46 | + envconfig.Options.required: lambda conf: True, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +try: |
| 51 | + mainconf = envconfig.Parser.parse(env_vars_def) |
| 52 | +except envconfig.InvalidConfig as exce: |
| 53 | + print(exce) |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | +try: |
| 57 | + driverclass = get_driver_class('autorep.target.' + mainconf['AUTOREP_TARGET_DRIVER_MODULE'], |
| 58 | + target.AbstractTargetDriver) |
| 59 | + if driverclass is None: |
| 60 | + raise envconfig.InvalidConfig("No valid driver found in %s" % 'target.' + |
| 61 | + mainconf['AUTOREP_TARGET_DRIVER_MODULE']) |
| 62 | + targetdriver = driverclass() |
| 63 | +except ImportError: |
| 64 | + print("Target Driver \"%s\" does not exist" % mainconf['AUTOREP_TARGET_DRIVER_MODULE']) |
| 65 | + sys.exit(2) |
| 66 | +except envconfig.InvalidConfig as exce: |
| 67 | + print(exce) |
| 68 | + exit(3) |
| 69 | + |
| 70 | +try: |
| 71 | + driverclass = get_driver_class('autorep.source.' + mainconf['AUTOREP_SOURCE_DRIVER_MODULE'], |
| 72 | + source.AbstractSourceDriver) |
| 73 | + if driverclass is None: |
| 74 | + raise envconfig.InvalidConfig("No valid driver found in %s" % 'source.' + |
| 75 | + mainconf['AUTOREP_SOURCE_DRIVER_MODULE']) |
| 76 | + sourcedriver = driverclass() |
| 77 | +except ImportError as exce: |
| 78 | + print("Source Driver \"%s\" does not exist" % mainconf['AUTOREP_SOURCE_DRIVER_MODULE']) |
| 79 | + print(exce) |
| 80 | + sys.exit(4) |
| 81 | +except envconfig.InvalidConfig as exce: |
| 82 | + print(exce) |
| 83 | + exit(5) |
| 84 | + |
| 85 | +try: |
| 86 | + driverclass = get_driver_class('autorep.clustering.' + mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE'], |
| 87 | + clustering.AbstractClusteringDecider) |
| 88 | + if driverclass is None: |
| 89 | + raise envconfig.InvalidConfig("No valid driver found in %s" % 'clustering.' + |
| 90 | + mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE']) |
| 91 | + clusteringdriver = driverclass() |
| 92 | +except ImportError: |
| 93 | + print("Clustering Driver \"%s\" does not exist" % |
| 94 | + mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE']) |
| 95 | + sys.exit(6) |
| 96 | +except envconfig.InvalidConfig as exce: |
| 97 | + print(exce) |
| 98 | + exit(7) |
| 99 | + |
| 100 | +try: |
| 101 | + driverclass = get_driver_class('autorep.replication.' + |
| 102 | + mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE'], |
| 103 | + replication.AbstractReplicationGraphBuilder) |
| 104 | + if driverclass is None: |
| 105 | + raise envconfig.InvalidConfig("No valid driver found in %s" % 'replication.' + |
| 106 | + mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE']) |
| 107 | + graphdriver = driverclass() |
| 108 | +except ImportError as exce: |
| 109 | + print("Replication Graph Driver \"%s\" does not exist" % |
| 110 | + mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE']) |
| 111 | + print(exce) |
| 112 | + sys.exit(8) |
| 113 | +except envconfig.InvalidConfig as exce: |
| 114 | + print(exce) |
| 115 | + exit(9) |
| 116 | + |
| 117 | +autorep.main_loop(mainconf, sourcedriver, targetdriver, graphdriver, clusteringdriver) |
| 118 | + |
0 commit comments