aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/apps/migrate.py
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2022-12-18 14:20:25 +0100
committerMatthias Baumgartner <dev@igsor.net>2022-12-18 14:20:25 +0100
commite94368c75468e3e94382b12705e55d396249eaca (patch)
treee9bfe27e5a641c040cfa8fe747a7cbb28091079c /bsfs/apps/migrate.py
parent12d95ed8bda18f2ef9d36190919cb838bfb5efcf (diff)
downloadbsfs-e94368c75468e3e94382b12705e55d396249eaca.tar.gz
bsfs-e94368c75468e3e94382b12705e55d396249eaca.tar.bz2
bsfs-e94368c75468e3e94382b12705e55d396249eaca.zip
bsfs applications
Diffstat (limited to 'bsfs/apps/migrate.py')
-rw-r--r--bsfs/apps/migrate.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/bsfs/apps/migrate.py b/bsfs/apps/migrate.py
new file mode 100644
index 0000000..91c1661
--- /dev/null
+++ b/bsfs/apps/migrate.py
@@ -0,0 +1,67 @@
+"""
+
+Part of the BlackStar filesystem (bsfs) module.
+A copy of the license is provided with the project.
+Author: Matthias Baumgartner, 2022
+"""
+# imports
+import argparse
+import json
+import logging
+import sys
+import typing
+
+# bsfs imports
+import bsfs
+
+# exports
+__all__: typing.Sequence[str] = (
+ 'main',
+ )
+
+
+## code ##
+
+logger = logging.getLogger(__name__)
+
+def main(argv):
+ """Migrate a storage structure to a modified schema."""
+ parser = argparse.ArgumentParser(description=main.__doc__, prog='migrate')
+ parser.add_argument('--remove', action='store_true', default=False,
+ help='Remove classes that are not specified in the provided schema.')
+ parser.add_argument('config', type=str, default=None,
+ help='Path to the storage config file.')
+ parser.add_argument('schema', nargs=argparse.REMAINDER,
+ help='Paths to schema files. Reads from standard input if no file is supplied.')
+ args = parser.parse_args(argv)
+
+ # load storage config
+ with open(args.config, mode='rt', encoding='UTF-8') as ifile:
+ config = json.load(ifile)
+ # open bsfs storage
+ graph = bsfs.Open(config)
+
+ # initialize schema
+ schema = bsfs.schema.Schema.Empty()
+ if len(args.schema) == 0:
+ # assemble schema from standard input
+ schema = schema + bsfs.schema.Schema.from_string(sys.stdin.read())
+ else:
+ # assemble schema from input files
+ for pth in args.schema:
+ with open(pth, mode='rt', encoding='UTF-8') as ifile:
+ schema = schema + bsfs.schema.Schema.from_string(ifile.read())
+
+ # migrate schema
+ graph.migrate(schema, not args.remove)
+
+ # return the migrated storage
+ return graph
+
+
+## main ##
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
+
+## EOF ##