From: Cleber Rosa <[email protected]> So that a wider audiency can make use of that code. New API methods and params were added to GitRepoHelper:
* get_top_commit - Returns the topmost commit * get_top_tag - Returns the topmost tag * checkout - Accepts now an optional branch/commit Signed-off-by: Cleber Rosa <[email protected]> Signed-off-by: Lucas Meneghel Rodrigues <[email protected]> --- client/common_lib/git.py | 182 +++++++++++++++++++++++++++++++++++++++++++++ client/virt/virt_utils.py | 151 ++----------------------------------- 2 files changed, 190 insertions(+), 143 deletions(-) create mode 100644 client/common_lib/git.py diff --git a/client/common_lib/git.py b/client/common_lib/git.py new file mode 100644 index 0000000..224821c --- /dev/null +++ b/client/common_lib/git.py @@ -0,0 +1,182 @@ +""" +Code that helps to deal with content from git repositories +""" + + +import os, logging +import error +from autotest_lib.client.bin import utils + + +__all__ = ["GitRepoHelper", "get_repo"] + + +class GitRepoHelper(object): + ''' + Helps to deal with git repos, mostly fetching content from a repo + ''' + def __init__(self, uri, branch='master', lbranch='master', commit=None, + destination_dir=None, base_uri=None): + ''' + Instantiates a new GitRepoHelper + + @type uri: string + @param uri: git repository url + @type branch: string + @param branch: git remote branch + @type destination_dir: string + @param destination_dir: path of a dir where to save downloaded code + @type commit: string + @param commit: specific commit to download + @type lbranch: string + @param lbranch: git local branch name, if different from remote + @type base_uri: string + @param base_uri: a closer, usually local, git repository url from where to + fetch content first + ''' + self.uri = uri + self.base_uri = base_uri + self.branch = branch + self.commit = commit + if destination_dir is None: + uri_basename = uri.split("/")[-1] + self.destination_dir = os.path.join("/tmp", uri_basename) + else: + self.destination_dir = destination_dir + if lbranch is None: + self.lbranch = branch + else: + self.lbranch = lbranch + + + def init(self): + ''' + Initializes a directory for receiving a verbatim copy of git repo + + This creates a directory if necessary, and either resets or inits + the repo + ''' + if not os.path.exists(self.destination_dir): + logging.debug('Creating directory %s for git repo %s', + self.destination_dir, self.uri) + os.makedirs(self.destination_dir) + + os.chdir(self.destination_dir) + + if os.path.exists('.git'): + logging.debug('Resetting previously existing git repo at %s for ' + 'receiving git repo %s', + self.destination_dir, self.uri) + utils.system('git reset --hard') + else: + logging.debug('Initializing new git repo at %s for receiving ' + 'git repo %s', + self.destination_dir, self.uri) + utils.system('git init') + + + def fetch(self, uri): + ''' + Performs a git fetch from the remote repo + ''' + logging.info("Fetching git [REP '%s' BRANCH '%s'] -> %s", + uri, self.branch, self.destination_dir) + os.chdir(self.destination_dir) + utils.system("git fetch -q -f -u -t %s %s:%s" % (uri, + self.branch, + self.lbranch)) + + + def get_top_commit(self): + ''' + Returns the topmost commit id for the current branch. + + @return: Commit id. + ''' + os.chdir(self.destination_dir) + return utils.system_output('git log --pretty=format:"%H" -1').strip() + + + def get_top_tag(self): + ''' + Returns the topmost tag for the current branch. + + @return: Tag. + ''' + os.chdir(self.destination_dir) + try: + return utils.system_output("git describe").strip() + except error.CmdError: + return None + + + def checkout(self, branch=None, commit=None): + ''' + Performs a git checkout for a given branch and start point (commit) + + @param branch: Remote branch name. + @param commit: Specific commit hash. + ''' + os.chdir(self.destination_dir) + + if branch is None: + branch = self.branch + + logging.debug('Checking out branch %s', branch) + utils.system("git checkout %s" % branch) + + if commit is None: + commit = self.commit + + if commit is not None: + logging.debug('Checking out commit %s', self.commit) + utils.system("git checkout %s" % self.commit) + else: + logging.debug('Specific commit not specified') + + top_commit = self.get_top_commit() + top_tag = self.get_top_tag() + if top_tag is None: + top_tag_desc = 'no tag found' + else: + top_tag_desc = 'tag %s' % top_tag + logging.info("git commit ID is %s (%s)", top_commit, top_tag_desc) + + + def execute(self): + ''' + Performs all steps necessary to initialize and download a git repo. + + This includes the init, fetch and checkout steps in one single + utility method. + ''' + self.init() + if self.base_uri is not None: + self.fetch(self.base_uri) + self.fetch(self.uri) + self.checkout() + + +def get_repo(uri, branch='master', lbranch='master', commit=None, + destination_dir=None, base_uri=None): + """ + Utility function that retrieves a given git code repository. + + @type uri: string + @param uri: git repository url + @type branch: string + @param branch: git remote branch + @type destination_dir: string + @param destination_dir: path of a dir where to save downloaded code + @type commit: string + @param commit: specific commit to download + @type lbranch: string + @param lbranch: git local branch name, if different from remote + @type base_uri: string + @param uri: a closer, usually local, git repository url from where to + fetch content first from + """ + repo = GitRepoHelper(uri, branch, lbranch, commit, destination_dir, + base_uri) + repo.execute() + return repo.destination_dir diff --git a/client/virt/virt_utils.py b/client/virt/virt_utils.py index 8b84a2b..073c056 100644 --- a/client/virt/virt_utils.py +++ b/client/virt/virt_utils.py @@ -9,7 +9,7 @@ import fcntl, shelve, ConfigParser, threading, sys, UserDict, inspect, tarfile import struct, shutil, glob from autotest_lib.client.bin import utils, os_dep from autotest_lib.client.common_lib import error, logging_config -from autotest_lib.client.common_lib import logging_manager +from autotest_lib.client.common_lib import logging_manager, git import rss_client, aexpect try: import koji @@ -466,42 +466,6 @@ def kill_process_tree(pid, sig=signal.SIGKILL): safe_kill(pid, signal.SIGCONT) -def get_git_branch(repository, branch, srcdir, commit=None, lbranch=None): - """ - Retrieves a given git code repository. - - @param repository: Git repository URL - """ - logging.info("Fetching git [REP '%s' BRANCH '%s' COMMIT '%s'] -> %s", - repository, branch, commit, srcdir) - if not os.path.exists(srcdir): - os.makedirs(srcdir) - os.chdir(srcdir) - - if os.path.exists(".git"): - utils.system("git reset --hard") - else: - utils.system("git init") - - if not lbranch: - lbranch = branch - - utils.system("git fetch -q -f -u -t %s %s:%s" % - (repository, branch, lbranch)) - utils.system("git checkout %s" % lbranch) - if commit: - utils.system("git checkout %s" % commit) - - h = utils.system_output('git log --pretty=format:"%H" -1') - try: - desc = "tag %s" % utils.system_output("git describe") - except error.CmdError: - desc = "no tag found" - - logging.info("Commit hash for %s is %s (%s)", repository, h.strip(), desc) - return srcdir - - def check_kvm_source_dir(source_dir): """ Inspects the kvm source directory and verifies its disposition. In some @@ -2444,110 +2408,7 @@ def mount(src, mount_point, type, perm="rw"): return False -class GitRepoHelper(object): - ''' - Helps to deal with git repos, mostly fetching content from a repo - ''' - def __init__(self, uri, branch, destination_dir, commit=None, lbranch=None, - base_uri=None): - ''' - Instantiates a new GitRepoHelper - - @type uri: string - @param uri: git repository url - @type branch: string - @param branch: git remote branch - @type destination_dir: string - @param destination_dir: path of a dir where to save downloaded code - @type commit: string - @param commit: specific commit to download - @type lbranch: string - @param lbranch: git local branch name, if different from remote - ''' - self.uri = uri - self.base_uri = base_uri - self.branch = branch - self.destination_dir = destination_dir - self.commit = commit - if lbranch is None: - self.lbranch = branch - - - def init(self): - ''' - Initializes a directory for receiving a verbatim copy of git repo - - This creates a directory if necessary, and either resets or inits - the repo - ''' - if not os.path.exists(self.destination_dir): - logging.debug('Creating directory %s for git repo %s', - self.destination_dir, self.uri) - os.makedirs(self.destination_dir) - - os.chdir(self.destination_dir) - - if os.path.exists('.git'): - logging.debug('Resetting previously existing git repo at %s for ' - 'receiving git repo %s', - self.destination_dir, self.uri) - utils.system('git reset --hard') - else: - logging.debug('Initializing new git repo at %s for receiving ' - 'git repo %s', - self.destination_dir, self.uri) - utils.system('git init') - - - def fetch(self, uri): - ''' - Performs a git fetch from the remote repo - ''' - logging.info("Fetching git [REP '%s' BRANCH '%s'] -> %s", - uri, self.branch, self.destination_dir) - os.chdir(self.destination_dir) - utils.system("git fetch -q -f -u -t %s %s:%s" % (uri, - self.branch, - self.lbranch)) - - - def checkout(self): - ''' - Performs a git checkout for a given branch and start point (commit) - ''' - os.chdir(self.destination_dir) - - logging.debug('Checking out local branch %s', self.lbranch) - utils.system("git checkout %s" % self.lbranch) - - if self.commit is not None: - logging.debug('Checking out commit %s', self.commit) - utils.system("git checkout %s" % self.commit) - - h = utils.system_output('git log --pretty=format:"%H" -1').strip() - try: - desc = "tag %s" % utils.system_output("git describe") - except error.CmdError: - desc = "no tag found" - - logging.info("Commit hash for %s is %s (%s)", self.name, h, desc) - - - def execute(self): - ''' - Performs all steps necessary to initialize and download a git repo - - This includes the init, fetch and checkout steps in one single - utility method. - ''' - self.init() - if self.base_uri is not None: - self.fetch(self.base_uri) - self.fetch(self.uri) - self.checkout() - - -class GitRepoParamHelper(GitRepoHelper): +class GitRepoParamHelper(git.GitRepoHelper): ''' Helps to deal with git repos specified in cartersian config files @@ -2589,7 +2450,11 @@ class GitRepoParamHelper(GitRepoHelper): 'prefix is %s' % (self.name, config_prefix)) self.base_uri = self.params.get('%s_base_uri' % config_prefix) - logging.debug('Git repo %s base uri: %s' % (self.name, self.base_uri)) + if self.base_uri is None: + logging.debug('Git repo %s base uri is not set' % self.name) + else: + logging.debug('Git repo %s base uri: %s' % (self.name, + self.base_uri)) self.uri = self.params.get('%s_uri' % config_prefix) logging.debug('Git repo %s uri: %s' % (self.name, self.uri)) @@ -3264,7 +3129,7 @@ def install_host_kernel(job, params): elif install_type == 'git': logging.info('Chose to install host kernel through git, proceeding') repodir = os.path.join("/tmp", 'kernel_src') - r = get_git_branch(git_repo, git_branch, repodir, git_commit) + r = git.get_git_branch(git_repo, git_branch, repodir, git_commit) host_kernel = job.kernel(r) if patch_list: host_kernel.patch(patch_list) -- 1.7.7.3 _______________________________________________ Autotest mailing list [email protected] http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
