summaryrefslogtreecommitdiff
path: root/morphlib/plugins/diff_plugin.py
blob: 4763ba40a75fd44d278769c667b3ec01517d8855 (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
# -*- coding: utf-8 -*-
# Copyright © 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

from morphlib.cmdline_parse_utils import parse_cmdline_system_lists
from morphlib.morphologyfinder import MorphologyFinder
from morphlib.morphloader import MorphologyLoader
from morphlib.util import new_repo_caches


class DiffPlugin(cliapp.Plugin):

    def enable(self):
        self.app.add_subcommand(
            'definition-diff', self.definition_diff,
            arg_synopsis='REPO REF [SYSTEM]... - REPO REF [SYSTEM]...')

    def disable(self):
        pass

    def definition_diff(self, args):
        '''Show the difference between definitions.

        When given two definition file specifiers, prints the logical
        differences between the definitions.

        '''
        # NOTE: It would be more useful to have this operate at the graphed
        #       dependency level, so you could use it to compare two different
        #       systems, and avoid duplicated logic to interpret how
        #       definitions are parsed.
        #       However at the time of writing the data model does not support:
        #       1.  Separately loading definition files from (repo, ref) and
        #           having the loaded definitions being shared between computed
        #           sources.
        #       2.  Parsing definitions for multiple systems together.
        #           This is because parameters from the parent definition (i.e.
        #           arch) are passed down to included definitions, but this is
        #           not taken into account for the lookup table, which is keyed
        #           on name, so it can't handle two chunks with the same name
        #           but different architectures.
        from_spec, to_spec = parse_definition_lists(
                                 definition_list_name_list=('from', 'to'),
                                 args=args)

        lrc, rrc = new_repo_caches(self.app)
        ml = MorphologyLoader()


        def load_morphset((reponame, ref, definitions)):
            repo = lrc.get_updated_repo(reponame, ref=ref)
            mf = MorphologyFinder(gitdir=repo.gitdir, ref=ref)
            if not definitions:
                definitions = mf.list_morphologies()
            ms = MorphologySet()
            for definition in definitions:
                m = ml.parse_morphology_text(mf.read_morphology(definition),
                                             definition)
                ms.add_morphology(m)
        from_morphset = load_morphset(from_spec)
        to_morphset = load_morphset(to_spec)