diff options
Diffstat (limited to 'gitautodeploy/wrappers')
-rw-r--r-- | gitautodeploy/wrappers/__init__.py | 2 | ||||
-rw-r--r-- | gitautodeploy/wrappers/git.py | 62 | ||||
-rw-r--r-- | gitautodeploy/wrappers/process.py | 31 |
3 files changed, 95 insertions, 0 deletions
diff --git a/gitautodeploy/wrappers/__init__.py b/gitautodeploy/wrappers/__init__.py new file mode 100644 index 0000000..d7df44b --- /dev/null +++ b/gitautodeploy/wrappers/__init__.py @@ -0,0 +1,2 @@ +from git import * +from process import *
\ No newline at end of file diff --git a/gitautodeploy/wrappers/git.py b/gitautodeploy/wrappers/git.py new file mode 100644 index 0000000..04ec94a --- /dev/null +++ b/gitautodeploy/wrappers/git.py @@ -0,0 +1,62 @@ +class GitWrapper(): + """Wraps the git client. Currently uses git through shell command + invocations.""" + + def __init__(self): + pass + + @staticmethod + def pull(repo_config): + """Pulls the latest version of the repo from the git server""" + import logging + from process import ProcessWrapper + + logger = logging.getLogger() + logger.info("Post push request received") + + # Only pull if there is actually a local copy of the repository + if 'path' not in repo_config: + logger.info('No local repository path configured, no pull will occure') + return 0 + + logger.info('Updating ' + repo_config['path']) + + cmd = 'unset GIT_DIR ' + \ + '&& git fetch ' + repo_config['remote'] + \ + '&& git reset --hard ' + repo_config['remote'] + '/' + repo_config['branch'] + ' ' + \ + '&& git submodule init ' + \ + '&& git submodule update' + + # '&& git update-index --refresh ' +\ + res = ProcessWrapper().call([cmd], cwd=repo_config['path'], shell=True) + logger.info('Pull result: ' + str(res)) + + return int(res) + + @staticmethod + def clone(url, branch, path): + from process import ProcessWrapper + ProcessWrapper().call(['git', + 'clone', + '--recursive', + url, + '-b', branch, + path], shell=True) + + @staticmethod + def deploy(repo_config): + """Executes any supplied post-pull deploy command""" + from process import ProcessWrapper + import logging + logger = logging.getLogger() + + if 'path' in repo_config: + path = repo_config['path'] + + logger.info('Executing deploy command(s)') + + # Use repository path as default cwd when executing deploy commands + cwd = (repo_config['path'] if 'path' in repo_config else None) + + for cmd in repo_config['deploy_commands']: + ProcessWrapper().call([cmd], cwd=cwd, shell=True)
\ No newline at end of file diff --git a/gitautodeploy/wrappers/process.py b/gitautodeploy/wrappers/process.py new file mode 100644 index 0000000..30adc36 --- /dev/null +++ b/gitautodeploy/wrappers/process.py @@ -0,0 +1,31 @@ +class ProcessWrapper(): + """Wraps the subprocess popen method and provides logging.""" + + def __init__(self): + pass + + @staticmethod + def call(*popenargs, **kwargs): + """Run command with arguments. Wait for command to complete. Sends + output to logging module. The arguments are the same as for the Popen + constructor.""" + + from subprocess import Popen, PIPE + import logging + logger = logging.getLogger() + + kwargs['stdout'] = PIPE + kwargs['stderr'] = PIPE + + p = Popen(*popenargs, **kwargs) + stdout, stderr = p.communicate() + + if stdout: + for line in stdout.strip().split("\n"): + logger.info(line) + + if stderr: + for line in stderr.strip().split("\n"): + logger.error(line) + + return p.returncode
\ No newline at end of file |