blob: 0428b8064fbd73c8813ad136d23710ab09e2a7bd [file] [log] [blame]
mbligh798d6512007-12-04 22:45:42 +00001"""
2This module defines a class for handling building from git repos
Eric Li6f27d4f2010-09-29 10:55:17 -07003
4@author: Ryan Harper (ryanh@us.ibm.com)
5@copyright: IBM 2007
mbligh798d6512007-12-04 22:45:42 +00006"""
7
Eric Li6f27d4f2010-09-29 10:55:17 -07008import os, warnings, logging
9from autotest_lib.client.common_lib import error, revision_control
10from autotest_lib.client.bin import os_dep
mblighccb9e182008-04-17 15:42:10 +000011from autotest_lib.server import utils, installable_object
mblighccb9e182008-04-17 15:42:10 +000012
mbligh798d6512007-12-04 22:45:42 +000013
Eric Li6f27d4f2010-09-29 10:55:17 -070014class InstallableGitRepo(installable_object.InstallableObject):
jadmanski0afbb632008-06-06 21:10:57 +000015 """
Eric Li6f27d4f2010-09-29 10:55:17 -070016 This class helps to pick a git repo and install it in a host.
jadmanski0afbb632008-06-06 21:10:57 +000017 """
Eric Li6f27d4f2010-09-29 10:55:17 -070018 def __init__(self, repodir, giturl, weburl=None):
19 self.repodir = repodir
jadmanski0afbb632008-06-06 21:10:57 +000020 self.giturl = giturl
jadmanski0afbb632008-06-06 21:10:57 +000021 self.weburl = weburl
Eric Li6f27d4f2010-09-29 10:55:17 -070022 self.git_repo = revision_control.GitRepo(self.repodir, self.giturl,
23 self.weburl)
jadmanski0afbb632008-06-06 21:10:57 +000024 # default to same remote path as local
Eric Li6f27d4f2010-09-29 10:55:17 -070025 self._build = os.path.dirname(self.repodir)
mbligh798d6512007-12-04 22:45:42 +000026
27
jadmanski0afbb632008-06-06 21:10:57 +000028 # base install method
29 def install(self, host, builddir=None):
Eric Li6f27d4f2010-09-29 10:55:17 -070030 """
31 Install a git repo in a host. It works by pushing the downloaded source
32 code to the host.
33
34 @param host: Host object.
35 @param builddir: Directory on the host filesystem that will host the
36 source code.
37 """
jadmanski0afbb632008-06-06 21:10:57 +000038 # allow override of target remote dir
39 if builddir:
Eric Li6f27d4f2010-09-29 10:55:17 -070040 self._build = builddir
mbligh798d6512007-12-04 22:45:42 +000041
jadmanski0afbb632008-06-06 21:10:57 +000042 # push source to host for install
Eric Li6f27d4f2010-09-29 10:55:17 -070043 logging.info('Pushing code dir %s to host %s', self.source_material,
44 self._build)
45 host.send_file(self.source_material, self._build)
mbligh798d6512007-12-04 22:45:42 +000046
47
jadmanski0afbb632008-06-06 21:10:57 +000048 def gitcmd(self, cmd, ignore_status=False):
Eric Li6f27d4f2010-09-29 10:55:17 -070049 """
50 Wrapper for a git command.
51
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)
mbligh798d6512007-12-04 22:45:42 +000058
59
jadmanski0afbb632008-06-06 21:10:57 +000060 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.
Eric Li6f27d4f2010-09-29 10:55:17 -070067
68 @param **kwargs: Dictionary of parameters to the method get.
jadmanski0afbb632008-06-06 21:10:57 +000069 """
jadmanski0afbb632008-06-06 21:10:57 +000070 self.source_material = self.repodir
Eric Li6f27d4f2010-09-29 10:55:17 -070071 return self.git_repo.get(**kwargs)
mbligh798d6512007-12-04 22:45:42 +000072
73
jadmanski0afbb632008-06-06 21:10:57 +000074 def get_local_head(self):
Eric Li6f27d4f2010-09-29 10:55:17 -070075 """
76 Get the top commit hash of the current local git branch.
mbligh798d6512007-12-04 22:45:42 +000077
Eric Li6f27d4f2010-09-29 10:55:17 -070078 @return: Top commit hash of local git branch
79 """
80 return self.git_repo.get_local_head()
mbligh798d6512007-12-04 22:45:42 +000081
82
jadmanski0afbb632008-06-06 21:10:57 +000083 def get_remote_head(self):
Eric Li6f27d4f2010-09-29 10:55:17 -070084 """
85 Get the top commit hash of the current remote git branch.
mbligh798d6512007-12-04 22:45:42 +000086
Eric Li6f27d4f2010-09-29 10:55:17 -070087 @return: Top commit hash of remote git branch
88 """
89 return self.git_repo.get_remote_head()
mbligh798d6512007-12-04 22:45:42 +000090
91
jadmanski0afbb632008-06-06 21:10:57 +000092 def is_out_of_date(self):
Eric Li6f27d4f2010-09-29 10:55:17 -070093 """
94 Return whether this branch is out of date with regards to remote branch.
mbligh798d6512007-12-04 22:45:42 +000095
Eric Li6f27d4f2010-09-29 10:55:17 -070096 @return: False, if the branch is outdated, True if it is current.
97 """
98 return self.git_repo.is_out_of_date()
mbligh798d6512007-12-04 22:45:42 +000099
100
jadmanski0afbb632008-06-06 21:10:57 +0000101 def is_repo_initialized(self):
Eric Li6f27d4f2010-09-29 10:55:17 -0700102 """
103 Return whether the git repo was already initialized (has a top commit).
mbligh798d6512007-12-04 22:45:42 +0000104
Eric Li6f27d4f2010-09-29 10:55:17 -0700105 @return: False, if the repo was initialized, True if it was not.
106 """
107 return self.git_repo.is_repo_initialized()
mbligh798d6512007-12-04 22:45:42 +0000108
Eric Li6f27d4f2010-09-29 10:55:17 -0700109
110 def get_revision(self):
111 """
112 Return current HEAD commit id
113 """
114 return self.git_repo.get_revision()
115
116
117 def checkout(self, remote, local=None):
118 """
119 Check out the git commit id, branch, or tag given by remote.
120
121 Optional give the local branch name as local.
122
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
126 """
127 return self.git_repo.checkout(remote, local)
128
129
130 def get_branch(self, all=False, remote_tracking=False):
131 """
132 Show the branches.
133
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.
137 """
138 return self.git_repo.get_branch(all, remote_tracking)