summaryrefslogtreecommitdiff
path: root/morphlib/plugins/lorry_plugin.py
blob: 3e6d5e9db586cb470138f14fe556de1ce475a33b (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
# Copyright (C) 2013  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 os
import cliapp
import logging

import morphlib
from morphlib.util import OrderedDict, json

class LorryPlugin(cliapp.Plugin):

    def enable(self):
        self.app.add_subcommand(
            'lorry', self._lorry, arg_synopsis='NAME URL TYPE [ARG...]')

    def disable(self):
        pass

    def _lorry(self, args, **kwargs):
        '''Ingest a new component into git in the Baserock way

        Command line arguments:

        * `NAME` is used for the resulting lorry file and git repo
        * `URL` specifies the upstream (repo or tarball)
        * `TYPE` specifies the upstream type eg tar, bzr, svn, git
        * `ARG` is a Lorry command argument.

        This creates a NAME.lorry file, and then runs Lorry on it

        Example:

            morph foo http://code.liw.fi/foo/bzr/trunk/ bzr

        '''

        if len(args) < 3:
            raise cliapp.AppException('morph lorry needs a lorry name,'
                                      ' url and type as paramaters')
        chunk = args[0]
        url = args[1]
        type = args[2]

        lorrydir = os.path.join(self.app.settings['cachedir'],'lorries')
        if not os.path.exists(lorrydir):
            os.makedirs(lorrydir)

        lorry = {chunk: {'type': type, 'url': url}}
        f = open(os.path.join(lorrydir, chunk + '.lorry'), "w")
        f.write(json.dumps(lorry, indent=4, sort_keys=True))
        f.close()

        self.app.runcmd(['lorry', '--verbose', '--pull-only', f.name,
            '-w', lorrydir], stdin=None, stdout=None, stderr=None)