mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 1 | """ |
| 2 | This module defines the GitKernel class |
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 | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 6 | """ |
| 7 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 8 | import os, logging |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 9 | import git, source_kernel |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 10 | |
| 11 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 12 | class GitKernel(git.InstallableGitRepo): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 13 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 14 | This class represents an installable git kernel repo. |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 15 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 16 | It is used to pull down a local copy of a git repo, check if the local repo |
| 17 | is up-to-date, if not update and then build the kernel from the git repo. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 18 | """ |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 19 | def __init__(self, repodir, giturl, weburl=None): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 20 | super(GitKernel, self).__init__(repodir, giturl, weburl) |
| 21 | self._patches = [] |
| 22 | self._config = None |
| 23 | self._build = None |
| 24 | self._branch = None |
| 25 | self._revision = None |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 26 | |
| 27 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | def configure(self, config): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 29 | self._config = config |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 30 | |
| 31 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 32 | def patch(self, patch): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 33 | self._patches.append(patch) |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 34 | |
| 35 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 36 | def checkout(self, revision, local=None): |
| 37 | """ |
| 38 | Checkout the commit id, branch, or tag. |
| 39 | |
| 40 | @param revision: Name of the git remote branch, revision or tag |
| 41 | @param local: Name of the local branch, implies -b |
| 42 | """ |
| 43 | logging.info('Checking out %s', revision) |
| 44 | super(GitKernel, self).checkout(revision) |
| 45 | self._revision = super(GitKernel, self).get_revision() |
| 46 | self._branch = super(GitKernel, self).get_branch() |
| 47 | logging.info('Checked out %s on branch %s', self._revision, |
| 48 | self._branch) |
| 49 | |
| 50 | |
| 51 | def show_branch(self): |
| 52 | """ |
| 53 | Print the current local branch name. |
| 54 | """ |
| 55 | self._branch = super(GitKernel, self).get_branch() |
| 56 | logging.info(self._branch) |
| 57 | |
| 58 | |
| 59 | def show_branches(self, all=True): |
| 60 | """ |
| 61 | Print the local and remote branches. |
| 62 | |
| 63 | @param all: Whether to show all branches (True) or only local branches |
| 64 | (False). |
| 65 | """ |
| 66 | self._branch = super(GitKernel, self).get_branch() |
| 67 | logging.info(super(GitKernel, self).get_branch(all=all)) |
| 68 | |
| 69 | |
| 70 | def show_revision(self): |
| 71 | """ |
| 72 | Show the current git revision. |
| 73 | """ |
| 74 | self._revision = super(GitKernel, self).get_revision() |
| 75 | logging.info(self._revision) |
| 76 | |
| 77 | |
| 78 | def install(self, host, build=True, builddir=None, revision=None): |
| 79 | """ |
| 80 | Install the git tree in a host. |
| 81 | |
| 82 | @param host: Host object |
| 83 | @param build: Whether to build the source tree |
| 84 | @param builddir: Specify a build dir. If no build dir is specified, |
| 85 | the job temporary directory will be used, so the build won't |
| 86 | be persistent. |
| 87 | @param revision: Desired commit hash. If ommited, will build from HEAD |
| 88 | of the branch. |
| 89 | """ |
| 90 | if revision: |
| 91 | self.checkout(revision) |
| 92 | self._revision = super(GitKernel, self).get_revision() |
| 93 | logging.info('Checked out revision: %s', self._revision) |
| 94 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 95 | if not builddir: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 96 | self._build = os.path.join(host.get_tmp_dir(), "build") |
| 97 | logging.warning('Builddir %s is not persistent (it will be erased ' |
| 98 | 'in future jobs)', self._build) |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 99 | else: |
| 100 | self._build = builddir |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 101 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 102 | # push source to host for install |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 103 | logging.info('Pushing %s to host', self.source_material) |
| 104 | host.send_file(self.source_material, self._build) |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 105 | remote_source_material= os.path.join(self._build, |
| 106 | os.path.basename(self.source_material)) |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 107 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 108 | # use a source_kernel to configure, patch, build and install. |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 109 | sk = source_kernel.SourceKernel(remote_source_material) |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 110 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 111 | if build: |
| 112 | # apply patches |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 113 | for p in self._patches: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 114 | sk.patch(p) |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 115 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 116 | # configure |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 117 | sk.configure(self._config) |
mbligh | 3e0eaf1 | 2007-12-04 22:45:03 +0000 | [diff] [blame] | 118 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 119 | # build |
| 120 | sk.build(host) |
| 121 | |
| 122 | # install |
| 123 | sk.install(host) |