KVM test: tests_base.cfg: Introduce parameter 'vm_type'
[autotest-zwu.git] / server / git.py
blob0428b8064fbd73c8813ad136d23710ab09e2a7bd
1 """
2 This module defines a class for handling building from git repos
4 @author: Ryan Harper (ryanh@us.ibm.com)
5 @copyright: IBM 2007
6 """
8 import os, warnings, logging
9 from autotest_lib.client.common_lib import error, revision_control
10 from autotest_lib.client.bin import os_dep
11 from autotest_lib.server import utils, installable_object
14 class InstallableGitRepo(installable_object.InstallableObject):
15 """
16 This class helps to pick a git repo and install it in a host.
17 """
18 def __init__(self, repodir, giturl, weburl=None):
19 self.repodir = repodir
20 self.giturl = giturl
21 self.weburl = weburl
22 self.git_repo = revision_control.GitRepo(self.repodir, self.giturl,
23 self.weburl)
24 # default to same remote path as local
25 self._build = os.path.dirname(self.repodir)
28 # base install method
29 def install(self, host, builddir=None):
30 """
31 Install a git repo in a host. It works by pushing the downloaded source
32 code to the host.
34 @param host: Host object.
35 @param builddir: Directory on the host filesystem that will host the
36 source code.
37 """
38 # allow override of target remote dir
39 if builddir:
40 self._build = builddir
42 # push source to host for install
43 logging.info('Pushing code dir %s to host %s', self.source_material,
44 self._build)
45 host.send_file(self.source_material, self._build)
48 def gitcmd(self, cmd, ignore_status=False):
49 """
50 Wrapper for a git command.
52 @param cmd: Git subcommand (ex 'clone').
53 @param ignore_status: Whether we should supress error.CmdError
54 exceptions if the command did return exit code !=0 (True), or
55 not supress them (False).
56 """
57 return self.git_repo.gitcmd(cmd, ignore_status)
60 def get(self, **kwargs):
61 """
62 This method overrides baseclass get so we can do proper git
63 clone/pulls, and check for updated versions. The result of
64 this method will leave an up-to-date version of git repo at
65 'giturl' in 'repodir' directory to be used by build/install
66 methods.
68 @param **kwargs: Dictionary of parameters to the method get.
69 """
70 self.source_material = self.repodir
71 return self.git_repo.get(**kwargs)
74 def get_local_head(self):
75 """
76 Get the top commit hash of the current local git branch.
78 @return: Top commit hash of local git branch
79 """
80 return self.git_repo.get_local_head()
83 def get_remote_head(self):
84 """
85 Get the top commit hash of the current remote git branch.
87 @return: Top commit hash of remote git branch
88 """
89 return self.git_repo.get_remote_head()
92 def is_out_of_date(self):
93 """
94 Return whether this branch is out of date with regards to remote branch.
96 @return: False, if the branch is outdated, True if it is current.
97 """
98 return self.git_repo.is_out_of_date()
101 def is_repo_initialized(self):
103 Return whether the git repo was already initialized (has a top commit).
105 @return: False, if the repo was initialized, True if it was not.
107 return self.git_repo.is_repo_initialized()
110 def get_revision(self):
112 Return current HEAD commit id
114 return self.git_repo.get_revision()
117 def checkout(self, remote, local=None):
119 Check out the git commit id, branch, or tag given by remote.
121 Optional give the local branch name as local.
123 @param remote: Remote commit hash
124 @param local: Local commit hash
125 @note: For git checkout tag git version >= 1.5.0 is required
127 return self.git_repo.checkout(remote, local)
130 def get_branch(self, all=False, remote_tracking=False):
132 Show the branches.
134 @param all: List both remote-tracking branches and local branches (True)
135 or only the local ones (False).
136 @param remote_tracking: Lists the remote-tracking branches.
138 return self.git_repo.get_branch(all, remote_tracking)