Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / env / basic / source_manager.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import logging
12 import contextlib
13 from subprocess import CalledProcessError
14 from vstf.common.utils import check_call
15
16 LOG = logging.getLogger(__name__)
17
18
19 @contextlib.contextmanager
20 def my_chdir(file_path):
21     old_cwd = os.path.realpath(os.curdir)
22     os.chdir(file_path)
23     LOG.info("cd %s", file_path)
24     yield
25     os.chdir(old_cwd)
26     LOG.info("cd %s", old_cwd)
27
28
29 class SourceCodeManager(object):
30
31     def __init__(self):
32         super(SourceCodeManager, self).__init__()
33         self.base_path = '/opt/vstf/'
34
35     @staticmethod
36     def _git_pull(url, dest):
37         if not os.path.isdir(dest):
38             check_call("git clone %s %s" % (url, dest), shell=True)
39         else:
40             with my_chdir(dest):
41                 check_call("git pull", shell=True)
42
43     @staticmethod
44     def _install(dest):
45         with my_chdir(dest):
46             try:
47                 check_call("make && make install", shell=True)
48             except CalledProcessError:
49                 LOG.info("retry make again")
50                 check_call("make clean; make && make install", shell=True)
51
52     def src_install(self, cfg):
53         for key, item in cfg.items():
54             repo_type = item['repo_type']
55             url = item['url']
56             install = item['install']
57             if install is True:
58                 LOG.info("installing src repo:%s", key)
59                 if repo_type == "git":
60                     target = self.base_path + key
61                     self._git_pull(url, target)
62                     self._install(target)
63                 else:
64                     raise Exception("unsupported repo type:%s" % repo_type)
65             else:
66                 LOG.info("skip src repo:%s", key)
67         return True
68
69
70 if __name__ == '__main__':
71     import argparse
72     import json
73     parser = argparse.ArgumentParser()
74     parser.add_argument('--config', help='config file to parse')
75     args = parser.parse_args()
76     logging.basicConfig(level=logging.INFO)
77     cfg = json.load(open(args.config))
78     mgr = SourceCodeManager()
79     mgr.src_install(cfg)