%PDF- %PDF-
Direktori : /proc/3/root/opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/ |
Current File : //proc/3/root/opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/router.py |
""" This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see <https://www.imunify360.com/legal/eula> Provide Router for db migrations.""" import os import re from contextlib import suppress from peewee_migrate import Router as PeeweeRouter from peewee_migrate.compat import exec_in from peewee_migrate.router import VOID __all__ = ["Router"] class Router(PeeweeRouter): """Like peewee_migrate.Router but supports multiple migrations dirs.""" # this is a slightly edited version from peewee_migrate.router.Router def __init__(self, database, migrations_dirs, **kwargs): super().__init__(database, migrate_dir=migrations_dirs[0], **kwargs) self.migrations_dirs = migrations_dirs @property def todo(self): """Scan migrations in file system.""" for migrate_dir in self.migrations_dirs: if not os.path.exists(migrate_dir): self.logger.warn( "Migration directory: %s does not exist.", migrate_dir ) os.makedirs(migrate_dir) migration_names = [] for migrate_dir in self.migrations_dirs: migration_names += sorted( f[: -len(".py")] for f in os.listdir(migrate_dir) if self.filemask.match(f) ) return migration_names def read(self, name): """Read migration from file.""" scope = {} for migrate_dir in self.migrations_dirs: with suppress(FileNotFoundError): with open(os.path.join(migrate_dir, name + ".py")) as f: code = f.read() exec_in(code, scope) return scope.get("migrate", VOID), scope.get("rollback", VOID)