Marcel Telka
2024-04-05 408e5d05305fc7032ab39e1c56263723cfa4fce3
commit | author | age
de89cf 1 #!/usr/bin/python3.9
405551 2 #
3 # This file and its contents are supplied under the terms of the
4 # Common Development and Distribution License ("CDDL"), version 1.0.
5 # You may only use this file in accordance with the terms of version
6 # 1.0 of the CDDL.
7 #
8 # A full copy of the text of the CDDL should have accompanied this
9 # source.  A copy of the CDDL is also available via the Internet at
10 # http://www.illumos.org/license/CDDL.
11 #
12
13 #
14 # Copyright 2018 Adam Stevko
15 #
16
17 #
18 # translate.py - Translate component FMRI to component path
19 #
20
21 from __future__ import print_function
22
23 import argparse
24 import os
25 import json
26 import sys
27
28
29 class MappingDatabase(object):
30     def __init__(self, data=None):
31         self._data = data
32
33     @classmethod
34     def load_from_file(self, file_path):
35         with open(file_path, 'r') as f:
36             data = json.loads(f.read())
37         return self(data=data)
38
39     def get_path_by_fmri(self, fmri):
40         for item in self._data:
41             if item['fmri'] == fmri:
42                 return item['path']
43
44
45 def main():
46     workspace_default_path = os.path.dirname(sys.argv[0]).rsplit('/', 1)[0]
47
48     parser = argparse.ArgumentParser()
49     parser.add_argument('-w', '--workspace', default=workspace_default_path, help='Path to workspace')
50     parser.add_argument('--subdir', default='components', help='Directory holding components')
51     parser.add_argument('--mapping', default='mapping.json', help='Mapping file')
52     parser.add_argument('--fmri', default=None, help='Component FMRI')
53
54     args = parser.parse_args()
55
56     workspace_path = args.workspace
57     subdir = args.subdir
58     mapping_file = args.mapping
59     fmri = args.fmri
60
61     mapping_file_path = os.path.join(workspace_path, subdir, mapping_file)
62
63     if not os.path.exists(mapping_file_path):
64         sys.stderr.write('Mapping file {} does not exist, build it by running "gmake mapping.json" in {}'.format(
65             mapping_file_path, os.path.join(workspace_path, subdir)))
66         sys.exit(2)
67
68     db = MappingDatabase().load_from_file(mapping_file_path)
69
70     component_path = db.get_path_by_fmri(fmri=fmri)
71     if component_path:
72         print(component_path)
73
74
75 if __name__ == '__main__':
76     main()