Changeset 40949
- Timestamp:
- Oct 18, 2019, 10:35:56 AM (7 years ago)
- Location:
- branches/ipp-259_genericise_backups/tools/backups
- Files:
-
- 8 edited
-
backup.py (modified) (6 diffs)
-
backup_test.py (modified) (23 diffs)
-
confluence_backup.py (modified) (2 diffs)
-
confluence_backup_test.py (modified) (2 diffs)
-
jira_backup.py (modified) (2 diffs)
-
jira_backup_test.py (modified) (3 diffs)
-
utils/config_parse_helper.py (modified) (5 diffs)
-
utils/config_parse_helper_test.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/backup.py
r40945 r40949 10 10 import backups.utils.errors as errs 11 11 import backups.utils.subprocess_utils as sub_utils 12 from backups.utils.config_parse_helper import ConfigSchema Section, ConfigSchemaLine12 from backups.utils.config_parse_helper import ConfigSchema, ConfigSchemaSection, ConfigSchemaLine, ParsedConfig 13 13 14 14 DEFAULT_CONFIG_FILE = path.join(path.dirname(__file__), 'atlassian_backups.config') 15 16 DEFAULT_SCHEMA_SECTION = ConfigSchemaSection('DEFAULT', 17 [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 18 , ConfigSchemaLine('source_machine', True, str) 19 , ConfigSchemaLine('verbose', False, bool) 20 , ConfigSchemaLine('make_dirs', False, bool) 21 ] 22 ) 23 COPY_SCHEMA_SECTION = ConfigSchemaSection('COPY', 24 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 25 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 26 ] 27 ) 28 TAR_SCHEMA_SECTION = ConfigSchemaSection('TAR', 29 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 30 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 31 , ConfigSchemaLine('target_filename', True, str) 32 , ConfigSchemaLine('prefix_date', True, bool) 33 ] 34 ) 35 RSYNC_SCHEMA_SECTION = ConfigSchemaSection('RSYNC', 36 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 37 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 38 ] 39 ) 40 41 MYSQLDUMP_SCHEMA_SECTION = ConfigSchemaSection('MYSQLDUMP', 42 [ ConfigSchemaLine('user', True, str) 43 , ConfigSchemaLine('password', True, str) 44 , ConfigSchemaLine('db_name', True, str) 45 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 46 , ConfigSchemaLine('target_filename', True, str) 47 , ConfigSchemaLine('prefix_date', True, bool) 48 ] 49 ) 50 51 52 def get_backup_schema() -> ConfigSchema: 53 backup_schema = ConfigSchema(None) 54 backup_schema.add_section(DEFAULT_SCHEMA_SECTION) 55 backup_schema.add_section(COPY_SCHEMA_SECTION) 56 backup_schema.add_section(TAR_SCHEMA_SECTION) 57 backup_schema.add_section(RSYNC_SCHEMA_SECTION) 58 backup_schema.add_section(MYSQLDUMP_SCHEMA_SECTION) 59 return backup_schema 15 60 16 61 … … 20 65 """ 21 66 22 def __init__(self, config_file=None ):67 def __init__(self, config_file=None, schema: ConfigSchema=None): 23 68 if config_file is None: 24 69 raise errs.ValidationError("ValidationError: Backup class must be given a config") 25 self.load_config(config_file) 26 27 def load_config(self, config_class_or_filepath): 70 if schema is None: 71 schema = get_backup_schema() 72 print(schema) 73 self.load_config(config_file, schema) 74 75 def load_config(self, config_class_or_filepath, schema): 28 76 """Accepts a filepath to a config file, or """ 29 77 … … 38 86 config.read(config_class_or_filepath) 39 87 40 self.read_default_section(config) 41 if self._has_copy_section(config): 42 self.read_copy_config_section(config) 43 if self._has_tar_section(config): 44 self.read_tar_config_section(config) 45 if self._has_rsync_section(config): 46 self.read_rsync_config_section(config) 47 if self._has_mysqldump_section(config): 48 self.read_mysqldump_config_section(config) 49 50 def _has_copy_section(self, config_parser: ConfigParser) -> bool: 51 section = 'COPY' 52 self.run_copy = config_parser.has_section(section) 53 return self.run_copy 54 55 def _has_tar_section(self, config_parser: ConfigParser) -> bool: 56 section = 'TAR' 57 self.run_tar = config_parser.has_section(section) 58 return self.run_tar 59 60 def _has_rsync_section(self, config_parser: ConfigParser) -> bool: 61 section = 'RSYNC' 62 self.run_rsync = config_parser.has_section(section) 63 return self.run_rsync 64 65 def _has_mysqldump_section(self, config_parser: ConfigParser) -> bool: 66 section = 'MYSQLDUMP' 67 self.run_mysqldump = config_parser.has_section(section) 68 return self.run_mysqldump 69 70 DEFAULT_SCHEMA = ConfigSchemaSection('DEFAULT', 71 [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 72 , ConfigSchemaLine('source_machine', True, str) 73 , ConfigSchemaLine('verbose', False, bool) 74 , ConfigSchemaLine('make_dirs', False, bool) 75 ] 76 ) 77 78 def read_default_section(self, config: ConfigParser, schema: ConfigSchemaSection=DEFAULT_SCHEMA): 79 self.default_dict = cfg_help.parse_config(config, schema) 88 self.read_config_and_schema(config, schema) 89 # self.read_copy_config_section(config, schema) 90 # self.read_tar_config_section(config, schema) 91 # self.read_rsync_config_section(config, schema) 92 # self.read_mysqldump_config_section(config, schema) 93 94 def read_config_and_schema(self, config: ConfigParser, schema: ConfigSchema): 95 parsed_config = ParsedConfig(config, schema) 96 print(parsed_config) 97 self.default_dict = parsed_config.get_section('DEFAULT') 80 98 self.hostname_is_valid() 81 99 self.maybe_make_backup_dirs() 82 100 83 COPY_SCHEMA = ConfigSchemaSection('COPY', 84 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 85 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 86 ] 87 ) 88 89 def read_copy_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=COPY_SCHEMA): 90 self.copy_dict = cfg_help.parse_config(config, schema) 91 92 TAR_SCHEMA = ConfigSchemaSection('TAR', 93 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 94 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 95 , ConfigSchemaLine('target_filename', True, str) 96 , ConfigSchemaLine('prefix_date', True, bool) 97 ] 98 ) 99 100 def read_tar_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=TAR_SCHEMA): 101 self.tar_dict = cfg_help.parse_config(config, schema) 102 103 RSYNC_SCHEMA = ConfigSchemaSection('RSYNC', 104 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 105 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 106 ] 107 ) 108 109 def read_rsync_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=RSYNC_SCHEMA): 110 self.rsync_dict = cfg_help.parse_config(config, schema) 111 112 MYSQLDUMP_SCHEMA = ConfigSchemaSection('MYSQLDUMP', 113 [ ConfigSchemaLine('user', True, str) 114 , ConfigSchemaLine('password', True, str) 115 , ConfigSchemaLine('db_name', True, str) 116 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 117 , ConfigSchemaLine('target_filename', True, str) 118 , ConfigSchemaLine('prefix_date', True, bool) 119 ] 120 ) 121 122 def read_mysqldump_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=MYSQLDUMP_SCHEMA): 123 self.mysqldump_dict = cfg_help.parse_config(config, schema) 101 section = 'COPY' 102 self.should_run_copy = parsed_config.has_section(section) 103 if self.should_run_copy: 104 self.copy_dict = parsed_config.get_section(section) 105 106 section = 'TAR' 107 self.should_run_tar = parsed_config.has_section(section) 108 if self.should_run_tar: 109 self.tar_dict = parsed_config.get_section(section) 110 111 section = 'RSYNC' 112 self.should_run_rsync = parsed_config.has_section(section) 113 if self.should_run_rsync: 114 self.rsync_dict = parsed_config.get_section(section) 115 116 section = 'MYSQLDUMP' 117 self.should_run_mysqldump = parsed_config.has_section(section) 118 if self.should_run_mysqldump: 119 self.mysqldump_dict = parsed_config.get_section(section) 120 121 def run_backup_processes(self): 122 123 if self.should_run_copy: 124 self._run_copy() 125 if self.should_run_tar: 126 self._run_tar() 127 if self.should_run_rsync: 128 self._run_rsync() 129 if self.should_run_mysqldump: 130 self._run_mysqldump() 124 131 125 132 def hostname_is_valid(self) -> bool: … … 146 153 os.makedirs(backup_path) 147 154 148 def run_backup(self):149 self._run_copy()150 self._run_tar()151 self._run_rsync()152 self._run_mysqldump()153 154 155 def _run_copy(self): 155 if self. run_copy is None or False:156 if self.should_run_copy is None or False: 156 157 return 0 157 158 self.target_backup_paths_ok(raise_error=True) … … 177 178 178 179 def _run_tar(self): 179 if self. run_tar is None or False:180 if self.should_run_tar is None or False: 180 181 return 0 181 182 … … 203 204 204 205 def _run_rsync(self): 205 if self. run_rsync is None or False:206 if self.should_run_rsync is None or False: 206 207 return 0 207 208 -
branches/ipp-259_genericise_backups/tools/backups/backup_test.py
r40945 r40949 8 8 import backups.utils.errors as errs 9 9 from backups.backup import Backup 10 10 from backups.utils.config_parse_helper import ConfigSchema 11 11 12 12 TEST_DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'testing/backup_test.config') … … 92 92 def test_loading_only_defaults(self, tmpdir): 93 93 config = make_default_config(tmpdir, ['bck1', 'bck2'], False) 94 backy = Backup(config_file=config) 94 schema = ConfigSchema(None) 95 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 96 backy = Backup(config, schema) 95 97 96 98 assert backy.default_dict['backup_paths'] == \ … … 117 119 def test_nothing_should_be_set_to_run_if_not_present(self, tmpdir): 118 120 config = make_default_config(tmpdir, ['bck1', 'bck2'], False) 119 backy = Backup(config_file=config) 120 121 assert backy.run_copy is False 122 assert backy.run_tar is False 123 assert backy.run_rsync is False 124 assert backy.run_mysqldump is False 121 schema = ConfigSchema(None) 122 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 123 backy = Backup(config, schema) 124 125 assert backy.should_run_copy is False 126 assert backy.should_run_tar is False 127 assert backy.should_run_rsync is False 128 assert backy.should_run_mysqldump is False 125 129 126 130 def test_hostname_assertion_fails(self, tmpdir): 127 131 config = make_default_config(tmpdir, ['bck1', 'bck2']) 128 backy = Backup(config_file=config) 132 schema = ConfigSchema(None) 133 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 134 backy = Backup(config, schema) 129 135 130 136 backy.default_dict['source_machine'] = 'not_a_real_hostname' … … 133 139 def test_hostname_assertion_passes(self, tmpdir): 134 140 config = make_default_config(tmpdir, ['bck1', 'bck2']) 135 backy = Backup(config_file=config) 141 schema = ConfigSchema(None) 142 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 143 backy = Backup(config, schema) 136 144 137 145 actual_hostname = socket.gethostname() … … 168 176 , 'verbose' : 'True' 169 177 } 178 schema = ConfigSchema(None) 179 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 170 180 bck1_path = os.path.join(tmpdir, 'bck1') 171 181 bck1b_path = os.path.join(tmpdir, 'bck1/bobby') … … 177 187 assert not os.path.exists(bck2_path) 178 188 # backup dirs not created automatically 179 Backup(config )189 Backup(config, schema) 180 190 assert not os.path.exists(bck1_path) 181 191 assert not os.path.exists(bck1b_path) … … 190 200 , 'make_dirs' : 'True' 191 201 } 192 backy = Backup(config )202 backy = Backup(config, schema) 193 203 assert backy.default_dict['make_dirs'] is True 194 204 assert os.path.exists(bck1_path) … … 204 214 source = os.path.join(tmpdir, "and_specific_file.jpeg") 205 215 config['COPY'] = {'sources' : f"{source}"} 216 schema = ConfigSchema(None) 217 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 218 schema.add_section(bckup.COPY_SCHEMA_SECTION) 206 219 207 220 # make file to be copied 208 221 Path(source).touch() 209 222 210 backy = Backup(config) 223 backy = Backup(config, schema) 224 assert backy.should_run_copy is True 211 225 212 226 expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg') … … 226 240 , 'additional_args' : "-v --preserve -r" 227 241 } 242 schema = ConfigSchema(None) 243 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 244 schema.add_section(bckup.COPY_SCHEMA_SECTION) 228 245 229 246 os.makedirs(source1) 230 247 Path(source2).touch() 231 248 232 backy = Backup(config )249 backy = Backup(config, schema) 233 250 234 251 expected1dir = os.path.join(tmpdir, 'bck1', 'some_dir') … … 253 270 , 'additional_args' : '-r' 254 271 } 272 schema = ConfigSchema(None) 273 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 274 schema.add_section(bckup.COPY_SCHEMA_SECTION) 255 275 256 276 assert not os.path.exists(source) 257 backy = Backup(config )277 backy = Backup(config, schema) 258 278 with pytest.raises(errs.ValidationError): 259 279 backy._run_copy() … … 271 291 Path(source).touch() 272 292 config['COPY'] = {'sources' : f"{source}"} 293 schema = ConfigSchema(None) 294 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 295 schema.add_section(bckup.COPY_SCHEMA_SECTION) 273 296 274 297 assert not os.path.exists(backup_does_not_exist) 275 backy = Backup(config )298 backy = Backup(config, schema) 276 299 with pytest.raises(errs.ValidationError): 277 300 backy._run_copy() … … 284 307 } 285 308 os.makedirs(source) 309 schema = ConfigSchema(None) 310 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 311 schema.add_section(bckup.COPY_SCHEMA_SECTION) 286 312 287 313 with pytest.raises(errs.SubprocessError): 288 backy = Backup(config )314 backy = Backup(config, schema) 289 315 backy._run_copy() 290 316 … … 293 319 , 'additional_args' : '-r' 294 320 } 295 backy = Backup(config )321 backy = Backup(config, schema) 296 322 297 323 expected1dir = os.path.join(tmpdir, 'bck1', 'some_dir') … … 320 346 } 321 347 322 backy = Backup(config) 348 schema = ConfigSchema(None) 349 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 350 schema.add_section(bckup.TAR_SCHEMA_SECTION) 351 352 backy = Backup(config, schema) 323 353 324 354 expected_tar1 = os.path.join(tmpdir, 'bck1', 'cool_tar.tar') … … 344 374 } 345 375 346 backy = Backup(config) 376 schema = ConfigSchema(None) 377 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 378 schema.add_section(bckup.TAR_SCHEMA_SECTION) 379 380 backy = Backup(config, schema) 381 assert backy.should_run_tar is True 347 382 348 383 date = bckup.get_date() … … 363 398 source = os.path.join(tmpdir, "and_specific_file.jpeg") 364 399 config['RSYNC'] = {'sources' : f"{source}"} 365 366 400 # make file to be copied 367 401 Path(source).touch() 368 402 369 backy = Backup(config) 403 schema = ConfigSchema(None) 404 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 405 schema.add_section(bckup.RSYNC_SCHEMA_SECTION) 406 407 backy = Backup(config, schema) 370 408 371 409 expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg') … … 389 427 Path(source2).touch() 390 428 391 backy = Backup(config) 429 schema = ConfigSchema(None) 430 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 431 schema.add_section(bckup.RSYNC_SCHEMA_SECTION) 432 433 backy = Backup(config, schema) 392 434 393 435 expected1dir = os.path.join(tmpdir, 'bck1', 'some_dir') … … 413 455 } 414 456 457 schema = ConfigSchema(None) 458 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 459 schema.add_section(bckup.RSYNC_SCHEMA_SECTION) 460 415 461 assert not os.path.exists(source) 416 backy = Backup(config )462 backy = Backup(config, schema) 417 463 with pytest.raises(errs.ValidationError): 418 464 backy._run_rsync() … … 431 477 config['RSYNC'] = {'sources' : f"{source}"} 432 478 479 schema = ConfigSchema(None) 480 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 481 schema.add_section(bckup.RSYNC_SCHEMA_SECTION) 482 433 483 assert not os.path.exists(backup_does_not_exist) 434 backy = Backup(config )484 backy = Backup(config, schema) 435 485 with pytest.raises(errs.ValidationError): 436 486 backy._run_rsync() … … 444 494 os.makedirs(source) 445 495 446 backy = Backup(config) 496 schema = ConfigSchema(None) 497 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 498 schema.add_section(bckup.RSYNC_SCHEMA_SECTION) 499 500 backy = Backup(config, schema) 501 447 502 expected1dir = os.path.join(tmpdir, 'bck1', 'some_dir') 448 503 expected2dir = os.path.join(tmpdir, 'bck2', 'some_dir') … … 458 513 } 459 514 460 backy = Backup(config )515 backy = Backup(config, schema) 461 516 expected1dir = os.path.join(tmpdir, 'bck1', 'some_dir') 462 517 expected2dir = os.path.join(tmpdir, 'bck2', 'some_dir') … … 491 546 } 492 547 493 backy = Backup(config) 548 schema = ConfigSchema(None) 549 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 550 schema.add_section(bckup.MYSQLDUMP_SCHEMA_SECTION) 551 552 backy = Backup(config, schema) 553 assert backy.should_run_mysqldump is True 494 554 495 555 date = bckup.get_date() … … 515 575 } 516 576 517 backy = Backup(config) 577 schema = ConfigSchema(None) 578 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 579 schema.add_section(bckup.MYSQLDUMP_SCHEMA_SECTION) 580 581 backy = Backup(config, schema) 518 582 with pytest.raises(errs.SubprocessError): 519 583 backy._run_mysqldump() -
branches/ipp-259_genericise_backups/tools/backups/confluence_backup.py
r40944 r40949 4 4 import os.path as path 5 5 6 import backups.backup as bck 6 7 import backups.utils.subprocess_utils as sub_utils 7 8 import backups.utils.errors as errs 8 9 from backups.backup import Backup 10 from backups.utils.config_parse_helper import ConfigSchema 9 11 10 12 EXPECTED_CONFIG_FILE = path.join(path.dirname(__file__), './confluence_backup.config') 13 14 CONFLUENCE_SCHEMA = bck.get_backup_schema() 15 16 17 def make_confluence_schema(): 18 schema = ConfigSchema(None) 19 schema.add_section(bck.DEFAULT_SCHEMA_SECTION) 20 schema.add_section(bck.COPY_SCHEMA_SECTION) 21 schema.add_section(bck.TAR_SCHEMA_SECTION) 22 schema.add_section(bck.MYSQLDUMP_SCHEMA_SECTION) 23 return schema 11 24 12 25 13 26 class ConfluenceBackup(Backup): 14 27 15 def __init__(self, config_file=EXPECTED_CONFIG_FILE): 16 self.load_config(config_file) 28 def __init__(self, config_file=EXPECTED_CONFIG_FILE, schema=None): 29 if schema is None: 30 schema = make_confluence_schema() 31 self.load_config(config_file, schema) 17 32 18 33 @staticmethod … … 25 40 26 41 def _run_copy(self, verbose=False): 27 if self. run_copy is None or False:42 if self.should_run_copy is None or False: 28 43 return 0 29 44 -
branches/ipp-259_genericise_backups/tools/backups/confluence_backup_test.py
r40944 r40949 6 6 from pathlib import Path 7 7 8 import backups.backup as bckup 8 9 import backups.confluence_backup as jbck_mod 9 10 import backups.utils.errors as errs 10 11 from backups.confluence_backup import ConfluenceBackup 11 12 from backups.utils.errors import ValidationError 12 13 from backups.utils.config_parse_helper import ConfigSchema 13 14 14 15 CONF_TEST_CONFIG = os.path.join(os.path.dirname(__file__), './testing/confluence_test.config') … … 146 147 } 147 148 148 jb = ConfluenceBackup(config_file=config) 149 schema = ConfigSchema(None) 150 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 151 schema.add_section(bckup.COPY_SCHEMA_SECTION) 152 schema.add_section(bckup.TAR_SCHEMA_SECTION) 153 154 jb = ConfluenceBackup(config, schema) 149 155 150 156 assert jb.default_dict == \ -
branches/ipp-259_genericise_backups/tools/backups/jira_backup.py
r40944 r40949 4 4 import os.path as path 5 5 6 import backups.backup as bck 6 7 import backups.utils.subprocess_utils as sub_utils 7 8 import backups.utils.errors as errs 8 9 from backups.backup import Backup 10 from backups.utils.config_parse_helper import ConfigSchema 9 11 10 12 EXPECTED_CONFIG_FILE = path.join(path.dirname(__file__), './jira_backup.config') 11 13 12 14 15 def make_jira_schema(): 16 schema = ConfigSchema(None) 17 schema.add_section(bck.DEFAULT_SCHEMA_SECTION) 18 schema.add_section(bck.COPY_SCHEMA_SECTION) 19 schema.add_section(bck.TAR_SCHEMA_SECTION) 20 schema.add_section(bck.MYSQLDUMP_SCHEMA_SECTION) 21 return schema 22 23 13 24 class JiraBackup(Backup): 14 25 15 def __init__(self, config_file=EXPECTED_CONFIG_FILE): 16 self.load_config(config_file) 26 def __init__(self, config_file=EXPECTED_CONFIG_FILE, schema=None): 27 if schema is None: 28 schema = make_jira_schema() 29 self.load_config(config_file, schema) 17 30 18 31 @staticmethod … … 25 38 26 39 def _run_copy(self, verbose=False): 27 if self. run_copy is None or False:40 if self.should_run_copy is None or False: 28 41 return 0 29 42 -
branches/ipp-259_genericise_backups/tools/backups/jira_backup_test.py
r40944 r40949 6 6 from pathlib import Path 7 7 8 import backups.backup as bckup 8 9 import backups.jira_backup as jbck_mod 9 10 import backups.utils.errors as errs 10 11 from backups.jira_backup import JiraBackup 11 12 from backups.utils.errors import ValidationError 12 13 from backups.utils.config_parse_helper import ConfigSchema 13 14 14 15 JIRA_TEST_CONFIG = os.path.join(os.path.dirname(__file__), './testing/jira_test.config') … … 146 147 } 147 148 148 jb = JiraBackup(config_file=config) 149 schema = ConfigSchema(None) 150 schema.add_section(bckup.DEFAULT_SCHEMA_SECTION) 151 schema.add_section(bckup.COPY_SCHEMA_SECTION) 152 schema.add_section(bckup.TAR_SCHEMA_SECTION) 153 154 jb = JiraBackup(config, schema) 149 155 150 156 assert jb.default_dict == \ … … 212 218 assert not os.path.exists(expected_backup_2_copy) 213 219 220 assert jb.should_run_copy is True 214 221 return_value = jb._run_copy() 215 222 assert return_value == 0 -
branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper.py
r40945 r40949 41 41 def __init__(self, section_name, options: [ConfigSchemaLine]): 42 42 self.section_name = section_name 43 self.options = {} 43 44 self._process_options(options) 44 45 45 46 def _process_options(self, options): 46 self.options = {}47 47 if options is None: 48 48 return 49 49 if type(options) is not list: 50 raise ValidationError( "ValidationError: ConfigSchemaoptions must be '[ConfigSchemaLine]' or 'None'")50 raise ValidationError(f"ValidationError: {self.__class__.__name__} options must be '[ConfigSchemaLine]' or 'None'") 51 51 if len(options) == 0: 52 52 return … … 62 62 def get_schema_line(self, key: str) -> ConfigSchemaLine: 63 63 if not self.key_in_options(key): 64 raise KeyError(f"KeyError: '{key}' is not present in schema ")64 raise KeyError(f"KeyError: '{key}' is not present in schema section") 65 65 return self.options[key] 66 66 67 67 def __str__(self): 68 68 options_strs = [ o.__str__() for o in self.options] 69 return (f'{self.__class__.__name__} with {options_strs}') 69 return (f'{self.__class__.__name__} named "{self.section_name}" with {options_strs}') 70 71 72 class ConfigSchema(): 73 74 def __init__(self, sections: [ConfigSchemaSection]): 75 self.sections = {} 76 self._process_sections(sections) 77 78 def _process_sections(self, sections): 79 if sections is None: 80 return 81 if type(sections) is not list: 82 raise ValidationError(f"ValidationError: {self.__class__.__name__} sections must be '[ConfigSchemaSection]' or 'None'") 83 if len(sections) == 0: 84 return 85 for sec in sections: 86 self.add_section(sec) 87 88 def add_section(self, section: ConfigSchemaSection): 89 self.sections[section.section_name] = section 90 91 def section_exists(self, section_name: str) -> bool: 92 return section_name in self.sections.keys() 93 94 def get_schema_section(self, section_name: str) -> ConfigSchemaSection: 95 if not self.section_exists(section_name): 96 raise KeyError(f"KeyError: '{section_name}' is not present in schema") 97 return self.sections[section_name] 98 99 def __str__(self): 100 section_strs = [ o.__str__() for o in self.sections] 101 return (f'{self.__class__.__name__} with {section_strs}') 70 102 71 103 … … 76 108 77 109 78 def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:110 def _parse_config_section(config: ConfigParser(), schema: ConfigSchemaSection) -> dict: 79 111 """For a given config it will extract all values defined by the schema 80 112 and return them as a dictionary of the keys and the assoicated value as the … … 87 119 csl = schema.get_schema_line(csl_key) 88 120 121 # Parses each line 89 122 if config.has_option(section, csl_key): 90 123 if csl.expected_type is bool: … … 115 148 116 149 return parsed_items 150 151 152 # def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict: 153 # return _parse_config_section(config, schema) 154 155 def _parse_config(config: ConfigParser(), schema: ConfigSchema) -> dict: 156 parsed_items = {} 157 print(config) 158 for c in config: 159 print(c) 160 161 for css_name in schema.sections: 162 print(css_name) 163 css = schema.get_schema_section(css_name) 164 print(css) 165 166 # if config.has_section(css_name): 167 parsed_section = _parse_config_section(config, css) 168 parsed_items[css_name] = parsed_section 169 # else: 170 # raise ConfigError(config, f"ConfigError: Expected section '{css_name}' from schema not present in config") 171 172 return parsed_items 173 174 175 class ParsedConfig(): 176 177 def __init__(self, config: ConfigParser, schema: ConfigSchema): 178 self._section_dict = _parse_config(config, schema) 179 180 def has_section(self, section_name): 181 return section_name in self._section_dict 182 183 def get_section(self, section_name): 184 if self.has_section(section_name): 185 return self._section_dict[section_name] 186 return None -
branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper_test.py
r40945 r40949 156 156 157 157 with pytest.raises(ConfigError): 158 helper. parse_config(config, schema)158 helper._parse_config_section(config, schema) 159 159 160 160 def test_schema_section_with_only_optional_values_is_ok(self): … … 163 163 schema = helper.ConfigSchemaSection('DEFAULT', [csl1, csl2] ) 164 164 config = self.make_simple_config() 165 result = helper. parse_config(config, schema)165 result = helper._parse_config_section(config, schema) 166 166 assert result == {} 167 167 … … 170 170 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 171 171 config = self.make_simple_config() 172 result = helper. parse_config(config, schema)172 result = helper._parse_config_section(config, schema) 173 173 assert result == { 'inty' : 3} 174 174 … … 177 177 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 178 178 config = self.make_simple_config() 179 result = helper. parse_config(config, schema)179 result = helper._parse_config_section(config, schema) 180 180 assert result == { 'floaty' : 3.141 } 181 181 … … 184 184 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 185 185 config = self.make_simple_config() 186 result = helper. parse_config(config, schema)186 result = helper._parse_config_section(config, schema) 187 187 assert result == { 'mr_bool' : True } 188 188 … … 191 191 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 192 192 config = self.make_simple_config() 193 result = helper. parse_config(config, schema)193 result = helper._parse_config_section(config, schema) 194 194 assert result == { 'stringer' : 'bell' } 195 195 … … 199 199 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 200 200 config = self.make_simple_config() 201 result = helper. parse_config(config, schema)201 result = helper._parse_config_section(config, schema) 202 202 assert result == { 'listo_comma' : [ "i" 203 203 , "am" … … 212 212 schema = helper.ConfigSchemaSection('DEFAULT', [csl1] ) 213 213 config = self.make_simple_config() 214 result = helper. parse_config(config, schema)214 result = helper._parse_config_section(config, schema) 215 215 assert result == { 'listo_space' : [ "yo" 216 216 , "it's" … … 232 232 233 233 schema = helper.ConfigSchemaSection('DEFAULT', [csl_float]) 234 result = helper. parse_config(config, schema)234 result = helper._parse_config_section(config, schema) 235 235 assert result == { 'floaty' : 3.141 } 236 236 assert type(result['floaty']) is float … … 238 238 schema = helper.ConfigSchemaSection('DEFAULT', [csl_int]) 239 239 with pytest.raises(ValueError): 240 helper. parse_config(config, schema)240 helper._parse_config_section(config, schema) 241 241 242 242 schema = helper.ConfigSchemaSection('DEFAULT', [csl_bool]) 243 243 with pytest.raises(ValueError): 244 helper. parse_config(config, schema)244 helper._parse_config_section(config, schema) 245 245 246 246 schema = helper.ConfigSchemaSection('DEFAULT', [csl_str]) 247 result = helper. parse_config(config, schema)247 result = helper._parse_config_section(config, schema) 248 248 assert result == { 'floaty' : '3.141' } 249 249 assert type(result['floaty']) is str 250 250 251 251 schema = helper.ConfigSchemaSection('DEFAULT', [csl_list]) 252 result = helper. parse_config(config, schema)252 result = helper._parse_config_section(config, schema) 253 253 assert result == { 'floaty' : ['3.141'] } 254 254 assert type(result['floaty']) is list … … 259 259 config = self.make_simple_config() 260 260 with pytest.raises(ConfigError): 261 helper. parse_config(config, schema)261 helper._parse_config_section(config, schema) 262 262 263 263 def test_all_special_processing_options_are_handled(self): … … 269 269 schema = helper.ConfigSchemaSection('DEFAULT', [csl]) 270 270 271 result = helper. parse_config(config, schema)271 result = helper._parse_config_section(config, schema) 272 272 assert type(result['enum_checker']) is list 273 273 … … 285 285 schema = helper.ConfigSchemaSection('FULL_TEST', [csl1, csl2, csl3, csl4, csl5, csl6]) 286 286 287 result = helper. parse_config(config, schema)287 result = helper._parse_config_section(config, schema) 288 288 assert result == \ 289 289 { 'inty' : 3
Note:
See TracChangeset
for help on using the changeset viewer.
