aboutsummaryrefslogtreecommitdiff
path: root/lava_android_test/repository.py
blob: ce5747069e6be62b742e7efe2769ddb4a38e4faa (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
80
81
82
83
# Copyright (C) 2012 Linaro Limited
#
# Author: Linaro Validation Team <linaro-dev@lists.linaro.org>
#
# This file is part of LAVA Android Test.

# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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 os
import subprocess

from lava_android_test import utils


class RepositoryError(Exception):
    """
    Raise this for repository related errors
    """


class Repository(object):
    '''
    Base class for all repository class used to checkout files.
    This class and sub classes are base the repository command.
    '''

    def __init__(self, url, repo_type, cmds=[]):
        self.url = url
        self.repo_type = repo_type
        self.check_cmds_exist(cmds)

    def checkout(self, target_dir=None):
        """
        Checkout this repository to the specified the target_dir directory
        """
        raise NotImplementedError()

    def check_cmds_exist(self, cmds=[]):
        """
        check whether the necessary commands are existing.
        """
        for cmd in cmds:
            if not utils.check_command_exist(cmd):
                raise RepositoryError(("The necessary command(%) does not"
                                      " exist, Or can't be seen from path")
                                       % cmd)


class GitRepository(Repository):

    git_cmd = 'git'

    def __init__(self, url):
        super(GitRepository, self).__init__(url, 'git', [self.git_cmd])

    def checkout(self, target_dir=None):
        """
        Checkout this git repository to the specified the target_dir directory
        """
        cmds = []
        if not target_dir:
            cmds = [self.git_cmd, 'clone', self.url]

        else:
            if not os.path.exists(target_dir):
                os.makedirs(target_dir)
            cmds = [self.git_cmd, 'clone', self.url, target_dir]

        rc = subprocess.call(cmds)
        if rc != 0:
            raise RepositoryError(("Failed to clone the specified "
                  "repository() with exist staus=%d")
                                   % (self.url, rc))