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
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.
14 # Copyright 2018 Adam Stevko
18 # translate.py - Translate component FMRI to component path
21 from __future__ import print_function
29 class MappingDatabase(object):
30 def __init__(self, data=None):
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)
39 def get_path_by_fmri(self, fmri):
40 for item in self._data:
41 if item['fmri'] == fmri:
46 workspace_default_path = os.path.dirname(sys.argv[0]).rsplit('/', 1)[0]
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')
54 args = parser.parse_args()
56 workspace_path = args.workspace
58 mapping_file = args.mapping
61 mapping_file_path = os.path.join(workspace_path, subdir, mapping_file)
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)))
68 db = MappingDatabase().load_from_file(mapping_file_path)
70 component_path = db.get_path_by_fmri(fmri=fmri)
75 if __name__ == '__main__':