Home | History | Annotate | Download | only in server
      1 """
      2 This module defines a class for handling building from git repos
      3 
      4 @author: Ryan Harper (ryanh (at] us.ibm.com)
      5 @copyright: IBM 2007
      6 """
      7 
      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
     12 
     13 
     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)
     26 
     27 
     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.
     33 
     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
     41 
     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)
     46 
     47 
     48     def gitcmd(self, cmd, ignore_status=False):
     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)
     58 
     59 
     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.
     67 
     68         @param **kwargs: Dictionary of parameters to the method get.
     69         """
     70         self.source_material = self.repodir
     71         return self.git_repo.get(**kwargs)
     72 
     73 
     74     def get_local_head(self):
     75         """
     76         Get the top commit hash of the current local git branch.
     77 
     78         @return: Top commit hash of local git branch
     79         """
     80         return self.git_repo.get_local_head()
     81 
     82 
     83     def get_remote_head(self):
     84         """
     85         Get the top commit hash of the current remote git branch.
     86 
     87         @return: Top commit hash of remote git branch
     88         """
     89         return self.git_repo.get_remote_head()
     90 
     91 
     92     def is_out_of_date(self):
     93         """
     94         Return whether this branch is out of date with regards to remote branch.
     95 
     96         @return: False, if the branch is outdated, True if it is current.
     97         """
     98         return self.git_repo.is_out_of_date()
     99 
    100 
    101     def is_repo_initialized(self):
    102         """
    103         Return whether the git repo was already initialized (has a top commit).
    104 
    105         @return: False, if the repo was initialized, True if it was not.
    106         """
    107         return self.git_repo.is_repo_initialized()
    108 
    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)
    139