Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / agent / env / basic / source_manager.py
1 """
2 Created on 2015-8-27
3
4 @author: y00228926
5 """
6 import os
7 import logging
8 import contextlib
9 from subprocess import CalledProcessError
10 from vstf.common.utils import check_call
11
12 LOG = logging.getLogger(__name__)
13
14
15 @contextlib.contextmanager
16 def my_chdir(file_path):
17     old_cwd = os.path.realpath(os.curdir)
18     os.chdir(file_path)
19     LOG.info("cd %s", file_path)
20     yield
21     os.chdir(old_cwd)
22     LOG.info("cd %s", old_cwd)
23
24
25 class SourceCodeManager(object):
26     def __init__(self):
27         super(SourceCodeManager, self).__init__()
28         self.base_path = '/opt/vstf/'
29
30     @staticmethod
31     def _git_pull(url, dest):
32         if not os.path.isdir(dest):
33             check_call("git clone %s %s" % (url, dest), shell=True)
34         else:
35             with my_chdir(dest):
36                 check_call("git pull", shell=True)
37
38     @staticmethod
39     def _install(dest):
40         with my_chdir(dest):
41             try:
42                 check_call("make && make install", shell=True)
43             except CalledProcessError:
44                 LOG.info("retry make again")
45                 check_call("make clean; make && make install", shell=True)
46
47     def src_install(self, cfg):
48         for key, item in cfg.items():
49             repo_type = item['repo_type']
50             url = item['url']
51             install = item['install']
52             if install is True:
53                 LOG.info("installing src repo:%s", key)
54                 if repo_type == "git":
55                     target = self.base_path + key
56                     self._git_pull(url, target)
57                     self._install(target)
58                 else:
59                     raise Exception("unsupported repo type:%s" % repo_type)
60             else:
61                 LOG.info("skip src repo:%s", key)
62         return True
63
64
65 if __name__ == '__main__':
66     import argparse
67     import json
68     parser = argparse.ArgumentParser()
69     parser.add_argument('--config', help='config file to parse')
70     args = parser.parse_args()
71     logging.basicConfig(level=logging.INFO)
72     cfg = json.load(open(args.config))
73     mgr = SourceCodeManager()
74     mgr.src_install(cfg)