def main(args): """Main method""" loglevel = logging.WARNING if args.verbose: loglevel = logging.DEBUG logging.basicConfig(format='%(levelname)s: %(message)s', level=loglevel) if args.list_providers: list_provider_classes() return 0 schema = yaml.load(open(args.schema), Loader=yaml.FullLoader) pg_args = get_pg_args(args) connection = get_connection(pg_args) if args.init_sql: cursor = connection.cursor() logging.info('Executing initialisation sql {}'.format(args.init_sql)) cursor.execute(args.init_sql) cursor.close() start_time = time.time() truncate_tables(connection, schema.get('truncate', [])) anonymize_tables(connection, schema.get('tables', []), verbose=args.verbose, dry_run=args.dry_run) if not args.dry_run: connection.commit() connection.close() end_time = time.time() logging.info('Anonymization took {:.2f}s'.format(end_time - start_time)) if args.dump_file: create_database_dump(args.dump_file, pg_args)
def test(self): mock_cursor = Mock() connection = Mock() connection.cursor.return_value = mock_cursor truncate_tables(connection, ['table_a', 'table_b']) assert mock_cursor.execute.call_args_list == [ call('TRUNCATE TABLE table_a, table_b;'), ] mock_cursor.close.assert_called_once()
def test(self, quote_ident, tables, expected): mock_cursor = Mock() connection = Mock() connection.cursor.return_value = mock_cursor truncate_tables(connection, tables) if tables: connection.cursor.assert_called_once() assert mock_cursor.execute.call_args_list == [call(expected)] mock_cursor.close.assert_called_once() else: connection.cursor.assert_not_called() mock_cursor.execute.assert_not_called() mock_cursor.close.assert_not_called()
def main(): """Main method""" parser = argparse.ArgumentParser(description='Anonymize data of a PostgreSQL database') parser.add_argument('-v', '--verbose', action='count', help='Increase verbosity') parser.add_argument('-l', '--list-providers', action='store_true', help='Show a list of all available providers', default=False) parser.add_argument('--schema', help='A YAML schema file that contains the anonymization rules', default=DEFAULT_SCHEMA_FILE) parser.add_argument('--dbname', help='Name of the database') parser.add_argument('--user', help='Name of the database user') parser.add_argument('--password', default='', help='Password for the database user') parser.add_argument('--host', help='Database hostname', default='localhost') parser.add_argument('--port', help='Port of the database', default='5432') parser.add_argument('--dry-run', action='store_true', help='Don\'t commit changes made on the database', default=False) parser.add_argument('--dump-file', help='Create a database dump file with the given name') args = parser.parse_args() loglevel = logging.WARNING if args.verbose: loglevel = logging.DEBUG logging.basicConfig(format='%(levelname)s: %(message)s', level=loglevel) if args.list_providers: list_provider_classes() sys.exit(0) schema = yaml.load(open(args.schema), Loader=yaml.FullLoader) pg_args = get_pg_args(args) connection = get_connection(pg_args) start_time = time.time() truncate_tables(connection, schema.get('truncate', [])) anonymize_tables(connection, schema.get('tables', []), verbose=args.verbose) if not args.dry_run: connection.commit() connection.close() end_time = time.time() logging.info('Anonymization took {:.2f}s'.format(end_time - start_time)) if args.dump_file: create_database_dump(args.dump_file, pg_args)