[geany/infrastructure] 542d69: Add 'geany_commit_utils' module to reduce code duplication

Enrico Tröger git-noreply at xxxxx
Sun Apr 6 08:25:20 UTC 2014


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Sun, 06 Apr 2014 08:25:20 UTC
Commit:      542d6988cea50c46e1fc93cf0be10a87bc7adaf4
             https://github.com/geany/infrastructure/commit/542d6988cea50c46e1fc93cf0be10a87bc7adaf4

Log Message:
-----------
Add 'geany_commit_utils' module to reduce code duplication

This module just holds some helper functions used in multiple scripts
like logging setup and command execution.


Modified Paths:
--------------
    scripts/git_hooks/geany_commit_utils.py
    scripts/git_hooks/post_commit_hook.py
    scripts/git_hooks/update_repositories.py

Modified: scripts/git_hooks/geany_commit_utils.py
47 files changed, 47 insertions(+), 0 deletions(-)
===================================================================
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+#
+# Author:  Enrico Tröger
+# License: GPLv2
+#
+'''
+Utility functions for the Geany GIT hook/mirror scripts
+'''
+
+
+from subprocess import Popen, PIPE
+import logging
+
+
+#----------------------------------------------------------------------
+def setup_file_logging(name, logfile):
+    logger = logging.getLogger(name)
+    logger.setLevel(logging.DEBUG)
+    file_handler = logging.FileHandler(logfile)
+    file_handler.setLevel(logging.DEBUG)
+    formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s: %(message)s')
+    file_handler.setFormatter(formatter)
+    logger.addHandler(file_handler)
+
+    return logger
+
+
+#----------------------------------------------------------------------
+def run_command(repository_path, command, run_as=None, logger=None):
+    if run_as:
+        command = ('sudo', '-u', run_as) + command
+    process = Popen(command, cwd=repository_path, stdout=PIPE, stderr=PIPE)
+    stdout, stderr = process.communicate()
+    output = u''
+    if stdout:
+        output = u'%s\nStdout:\n%s' % (output, stdout)
+    if stderr:
+        output = u'%s\nStderr:\n%s' % (output, stderr)
+    if logger:
+        logger.debug(u'Command "%s": %s' % (' '.join(command), output))
+
+
+#----------------------------------------------------------------------
+def update_repository(repository, repository_path, logger, run_as=None):
+    logger.info(u'Updating repository %s' % repository)
+    run_command(repository_path, ('git', 'remote', 'update'), run_as=run_as, logger=logger)
+    run_command(repository_path, ('git', 'update-server-info'), run_as=run_as, logger=logger)


Modified: scripts/git_hooks/post_commit_hook.py
27 files changed, 3 insertions(+), 24 deletions(-)
===================================================================
@@ -14,10 +14,10 @@
 
 
 from cgi import FieldStorage
+from geany_commit_utils import setup_file_logging, update_repository
 from json import loads
 from os import unlink
 from os.path import exists
-from subprocess import Popen, PIPE
 import github_commit_mail
 import logging
 import logging.handlers
@@ -34,13 +34,7 @@
 
 #----------------------------------------------------------------------
 def setup_logging():
-    logger = logging.getLogger('post_commit_hook')
-    logger.setLevel(logging.DEBUG)
-    file_handler = logging.FileHandler(LOG_FILENAME)
-    file_handler.setLevel(logging.DEBUG)
-    formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s: %(message)s')
-    file_handler.setFormatter(formatter)
-    logger.addHandler(file_handler)
+    logger = setup_file_logging('post_commit_hook', LOG_FILENAME)
     # mail
     mail_handler = logging.handlers.SMTPHandler(
         u'localhost',
@@ -68,10 +62,7 @@ def handle_repository_update(repository):
         logger.info(u'Not updating repository %s because it is locked, leaving a notify' % repository)
     else:
         lock_file = open(lock_file_path, 'w')
-        # update the repository
-        logger.info(u'Updating repository %s' % repository)
-        run_command(repository_path, ('git', 'remote', 'update'))
-        run_command(repository_path, ('git', 'update-server-info'))
+        update_repository(repository, repository_path, logger)
         # remove lockfile
         lock_file.close()
         unlink(lock_file_path)
@@ -91,18 +82,6 @@ def process_commit_mails(content):
 
 
 #----------------------------------------------------------------------
-def run_command(repository_path, command):
-    process = Popen(command, cwd=repository_path, stdout=PIPE, stderr=PIPE)
-    stdout, stderr = process.communicate()
-    output = u''
-    if stdout:
-        output = u'%s\nStdout:\n%s' % (output, stdout)
-    if stderr:
-        output = u'%s\nStderr:\n%s' % (output, stderr)
-    logger.debug(u'Command "%s": %s' % (' '.join(command), output))
-
-
-#----------------------------------------------------------------------
 def main():
     # parse query string
     arguments = FieldStorage(keep_blank_values=True)


Modified: scripts/git_hooks/update_repositories.py
36 files changed, 7 insertions(+), 29 deletions(-)
===================================================================
@@ -12,10 +12,9 @@
 post_commit_hook script to be out-of-date.
 '''
 
-
+from geany_commit_utils import setup_file_logging, update_repository
 from os import listdir, unlink
 from os.path import exists, join
-from subprocess import Popen, PIPE
 import logging
 
 
@@ -27,18 +26,12 @@
 
 #----------------------------------------------------------------------
 def setup_logging():
-    logger = logging.getLogger('update_repositories')
-    logger.setLevel(logging.DEBUG)
-    ch = logging.StreamHandler()
-    ch.setLevel(logging.DEBUG)
-    formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s: %(message)s')
-    ch.setFormatter(formatter)
-    logger.addHandler(ch)
-    fh = logging.FileHandler(LOG_FILENAME)
-    fh.setLevel(logging.DEBUG)
+    logger = setup_file_logging('update_repositories', LOG_FILENAME)
+    handler = logging.StreamHandler()
+    handler.setLevel(logging.DEBUG)
     formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s: %(message)s')
-    fh.setFormatter(formatter)
-    logger.addHandler(fh)
+    handler.setFormatter(formatter)
+    logger.addHandler(handler)
 
     return logger
 
@@ -57,10 +50,7 @@ def handle_repository_update(repository):
         need_update = update_notify_file.read() == '1'
         if need_update:
             lock_file = open(lock_file_path, 'w')
-            # update the repository
-            logger.info(u'Updating repository %s' % repository)
-            run_command(repository_path, ('sudo', '-u', 'www-data', 'git', 'remote', 'update'))
-            run_command(repository_path, ('sudo', '-u', 'www-data', 'git', 'update-server-info'))
+            update_repository(repository, repository_path, logger, run_as='www-data')
             # remove lockfile
             lock_file.close()
             unlink(lock_file_path)
@@ -70,18 +60,6 @@ def handle_repository_update(repository):
 
 
 #----------------------------------------------------------------------
-def run_command(repository_path, command):
-    process = Popen(command, cwd=repository_path, stdout=PIPE, stderr=PIPE)
-    stdout, stderr = process.communicate()
-    output = u''
-    if stdout:
-        output = u'%s\nStdout:\n%s' % (output, stdout)
-    if stderr:
-        output = u'%s\nStderr:\n%s' % (output, stderr)
-    logger.debug(u'Command "%s": %s' % (' '.join(command), output))
-
-
-#----------------------------------------------------------------------
 def main():
     repositories = listdir(REPOSITORY_BASE_PATH)
     for repository in repositories:



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list