summaryrefslogtreecommitdiff
path: root/morphlib/plugins/get_chunk_details_plugin.py
blob: e3b9220c087353eb321b943fc323388da4bb9f6c (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
69
70
71
72
73
74
75
76
77
78
79
# Copyright (C) 2015  Codethink Limited
#
# 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; version 2 of the License.
#
# 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 <http://www.gnu.org/licenses/>.

import cliapp
import morphlib

class GetChunkDetailsPlugin(cliapp.Plugin):

    def enable(self):
        self.app.add_subcommand(
            'get-chunk-details', self.get_chunk_details,
            arg_synopsis='[STRATUM] CHUNK')

    def disable(self):
        pass

    def get_chunk_details(self, args):
        '''Print out details for the given chunk

        Command line arguments:

        * `STRATUM` is the stratum to search for chunk (optional).
        * `CHUNK` is the component to obtain a URL for.

        '''

        stratum_name = None

        if len(args) == 1:
            chunk_name = args[0]
        elif len(args) == 2:
            stratum_name = args[0]
            chunk_name = args[1]
        else:
            raise cliapp.AppException(
                'Wrong number of arguments to get-chunk-details command '
                '(see help)')

        definitions_repo = morphlib.definitions_repo.open(
            '.', search_for_root=True, app=self.app)

        aliases = self.app.settings['repo-alias']
        self.resolver = morphlib.repoaliasresolver.RepoAliasResolver(aliases)

        found = 0
        for morph in definitions_repo.load_all_morphologies():
            if morph['kind'] == 'stratum':
                if (stratum_name == None or
                        morph['name'] == stratum_name):
                    for chunk in morph['chunks']:
                        if chunk['name'] == chunk_name:
                            found = found + 1
                            self._print_chunk_details(chunk, morph)

        if found == 0:
            if stratum_name == None:
                print('Chunk `{}` not found'
                      .format(chunk_name))
            else:
                print('Chunk `{}` not found in stratum `{}`'
                      .format(chunk_name, stratum_name))

    def _print_chunk_details(self, chunk, morph):
        repo = self.resolver.pull_url(chunk['repo'])
        print('In stratum {}:'.format(morph['name']))
        print('  Chunk: {}'.format(chunk['name']))
        print('   Repo: {}'.format(repo))
        print('    Ref: {}'.format(chunk['ref']))