summaryrefslogtreecommitdiff
path: root/morphlib/plugins/update_gits_plugin.py
blob: ac7beaa84b92bb35bfea305fb41cab59312cc00a (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
# Copyright (C) 2012  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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import cliapp
import os

import morphlib


class UpdateGitsPlugin(cliapp.Plugin):

    def enable(self):
        self.app.add_subcommand('update-gits',
                                self.update_gits,
                                arg_synopsis='(REPO REF MORPHOLOGY)...')

    def disable(self):
        pass

    def update_gits(self, args):
        '''Update cached git repositories.

        Parse the given morphologies, and their dependencies, and
        update all the git repositories referred to by them in the
        morph cache directory.

        '''

        app = self.app
        if not os.path.exists(app.settings['cachedir']):
            os.mkdir(app.settings['cachedir'])
        cachedir = os.path.join(app.settings['cachedir'], 'gits')
        repo_resolver = morphlib.repoaliasresolver.RepoAliasResolver(
            app.settings['repo-alias'])
        bundle_base_url = app.settings['bundle-server']
        cache = morphlib.localrepocache.LocalRepoCache(
            app, cachedir, repo_resolver, bundle_base_url)

        subs_to_process = set()

        def visit(reponame, ref, filename, absref, morphology):
            app.status(msg='Updating %(repo_name)s %(ref)s %(filename)s',
                       repo_name=reponame, ref=ref, filename=filename)
            assert cache.has_repo(reponame)
            cached_repo = cache.get_repo(reponame)
            try:
                submodules = morphlib.git.Submodules(app, cached_repo.path,
                                                     absref)
                submodules.load()
            except morphlib.git.NoModulesFileError:
                pass
            else:
                for submod in submodules:
                    subs_to_process.add((submod.url, submod.commit))

        app.traverse_morphs(app.itertriplets(args), cache, None,
                            update=True, visit=visit)

        done = set()
        for url, ref in subs_to_process:
            app.cache_repo_and_submodules(cache, url, ref, done)