mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 1 | """ |
| 2 | This module defines a class for handling building from git repos |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 3 | |
| 4 | @author: Ryan Harper (ryanh@us.ibm.com) |
| 5 | @copyright: IBM 2007 |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 6 | """ |
| 7 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 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 |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 11 | from autotest_lib.server import utils, installable_object |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 12 | |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 13 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 14 | class InstallableGitRepo(installable_object.InstallableObject): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 15 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 16 | This class helps to pick a git repo and install it in a host. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 17 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 18 | def __init__(self, repodir, giturl, weburl=None): |
| 19 | self.repodir = repodir |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 20 | self.giturl = giturl |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 21 | self.weburl = weburl |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 22 | self.git_repo = revision_control.GitRepo(self.repodir, self.giturl, |
| 23 | self.weburl) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 24 | # default to same remote path as local |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 25 | self._build = os.path.dirname(self.repodir) |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 26 | |
| 27 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | # base install method |
| 29 | def install(self, host, builddir=None): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 30 | """ |
| 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 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | # allow override of target remote dir |
| 39 | if builddir: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 40 | self._build = builddir |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 41 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | # push source to host for install |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 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) |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 46 | |
| 47 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 48 | def gitcmd(self, cmd, ignore_status=False): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 49 | """ |
| 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) |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 58 | |
| 59 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 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. |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 67 | |
| 68 | @param **kwargs: Dictionary of parameters to the method get. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 69 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | self.source_material = self.repodir |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 71 | return self.git_repo.get(**kwargs) |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 72 | |
| 73 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 74 | def get_local_head(self): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 75 | """ |
| 76 | Get the top commit hash of the current local git branch. |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 77 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 78 | @return: Top commit hash of local git branch |
| 79 | """ |
| 80 | return self.git_repo.get_local_head() |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 81 | |
| 82 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 83 | def get_remote_head(self): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 84 | """ |
| 85 | Get the top commit hash of the current remote git branch. |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 86 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 87 | @return: Top commit hash of remote git branch |
| 88 | """ |
| 89 | return self.git_repo.get_remote_head() |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 90 | |
| 91 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 92 | def is_out_of_date(self): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 93 | """ |
| 94 | Return whether this branch is out of date with regards to remote branch. |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 95 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 96 | @return: False, if the branch is outdated, True if it is current. |
| 97 | """ |
| 98 | return self.git_repo.is_out_of_date() |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 99 | |
| 100 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 101 | def is_repo_initialized(self): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 102 | """ |
| 103 | Return whether the git repo was already initialized (has a top commit). |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 104 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 105 | @return: False, if the repo was initialized, True if it was not. |
| 106 | """ |
| 107 | return self.git_repo.is_repo_initialized() |
mbligh | 798d651 | 2007-12-04 22:45:42 +0000 | [diff] [blame] | 108 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 109 | |
| 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) |