aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/apps/migrate.py
blob: cb62542c891dc155f223b16c8afd40f075b68b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""

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()
    if len(args.schema) == 0:
        # assemble schema from standard input
        schema = schema + bsfs.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.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 ##