summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/env/basic/source_manager.py
blob: 4267cbd22572fb8da1491bf715204661f67c936e (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
"""
Created on 2015-8-27

@author: y00228926
"""
import os
import logging
import contextlib
from subprocess import CalledProcessError
from vstf.common.utils import check_call

LOG = logging.getLogger(__name__)


@contextlib.contextmanager
def my_chdir(file_path):
    old_cwd = os.path.realpath(os.curdir)
    os.chdir(file_path)
    LOG.info("cd %s", file_path)
    yield
    os.chdir(old_cwd)
    LOG.info("cd %s", old_cwd)


class SourceCodeManager(object):
    def __init__(self):
        super(SourceCodeManager, self).__init__()
        self.base_path = '/opt/vstf/'

    @staticmethod
    def _git_pull(url, dest):
        if not os.path.isdir(dest):
            check_call("git clone %s %s" % (url, dest), shell=True)
        else:
            with my_chdir(dest):
                check_call("git pull", shell=True)

    @staticmethod
    def _install(dest):
        with my_chdir(dest):
            try:
                check_call("make && make install", shell=True)
            except CalledProcessError:
                LOG.info("retry make again")
                check_call("make clean; make && make install", shell=True)

    def src_install(self, cfg):
        for key, item in cfg.items():
            repo_type = item['repo_type']
            url = item['url']
            install = item['install']
            if install is True:
                LOG.info("installing src repo:%s", key)
                if repo_type == "git":
                    target = self.base_path + key
                    self._git_pull(url, target)
                    self._install(target)
                else:
                    raise Exception("unsupported repo type:%s" % repo_type)
            else:
                LOG.info("skip src repo:%s", key)
        return True


if __name__ == '__main__':
    import argparse
    import json
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', help='config file to parse')
    args = parser.parse_args()
    logging.basicConfig(level=logging.INFO)
    cfg = json.load(open(args.config))
    mgr = SourceCodeManager()
    mgr.src_install(cfg)