Login | Register
My pages Projects Community openCollabNet

Discussions > Commits > svn commit: r153 - trunk/repoguard/src/repoguard/tools/checker.py

repoguard
Discussion topic

Back to topic list

svn commit: r153 - trunk/repoguard/src/repoguard/tools/checker.py

Author schlauch
Full name Tobias Schlauch
Date 2012-12-10 09:18:13 PST
Message Author: schlauch
Date: 2012-12-10 09:18:13-0800
New Revision: 153

Modified:
   trunk/repoguard/src/​repoguard/tools/chec​ker.py

Log:
Fixes issue #53:
- Implemented new default behavior when an unexpected exception occurred: The pre-commit script succeeds to avoid unnecessary out takes.
- This behavior can be changed when specifying "--halt-on-exception".- Configuration problems are handled as usual: They are directly showed and the commit is prevented.

Modified: trunk/repoguard/src/​repoguard/tools/chec​ker.py
Url: http://repoguard.tig​ris.org/source/brows​e/repoguard/trunk/re​poguard/src/repoguar​d/tools/checker.py?v​iew=diff&pathrev​=153&r1=152&​r2=153
====================​====================​====================​==================
--- trunk/repoguard/src/​repoguard/tools/chec​ker.py (original)
+++ trunk/repoguard/src/​repoguard/tools/chec​ker.py 2012-12-10 09:18:13-0800
@@ -21,7 +21,7 @@
 import os
 import tempfile
 
-from validate import ValidateError
+import validate
 
 from repoguard.core import constants
 from repoguard.core.checker import RepoGuard
@@ -35,10 +35,10 @@
 _USAGE = """
   repoguard %s [options] repo_path [txn_name]
 Arguments:
- repo_path\tThe path to this repository
+ repo_path\tThe path to this repository.
   txn_name\tThe name of the transaction about to be committed or
- \t\tthe revision if you use repoguard as command line tool. If you
- \t\tomit the option, the revision revision will be used.
+ \t\tthe revision number. If you omit the option, the HEAD
+ \t\trevision will be used.
 """
 
 os.environ['PYTHON_EGG_CACHE'] = tempfile.gettempdir()
@@ -49,7 +49,7 @@
     """
     
     def __init__(self):
- Tool.__init__(self, "Checker tools v0.1")
+ Tool.__init__(self, "Checker tools v0.2")
         
     @Tool.command_method(
         command = constants.PRECOMMIT,
@@ -76,20 +76,27 @@
             "-p", "--profile", dest="profile_name", default=None,
             help="Concrete profile which should be executed."
         )
+ parser.add_option(
+ "--halt-on-exception", action="store_false", dest="halt_on_exception",
+ help=(
+ "Causes the hook to return an error when an unexpected exception occurs.\n"
+ "Default behavior: Exception are logged but the hook succeeds.")
+ )
         
         options, args = parser.parse_args()
- if not len(args) in [2, 3]:
- parser.print_help()
- return 1
         if len(args) == 3:
             hook, repo_path, txn_name = args
- if len(args) == 2:
+ elif len(args) == 2:
             hook, repo_path = args
             txn_name = None
- return self.checker(hook, repo_path, txn_name, options.profile_name)
+ else:
+ parser.print_help()
+ return 1
+
+ return self.checker(hook, repo_path, txn_name, options.profile_name, options.halt_on_exception)
     
     @staticmethod
- def checker(hook, repo_path, txn_name, profile_name):
+ def checker(hook, repo_path, txn_name, profile_name, halt_on_exception):
         """
         Function to singularize the repoguard in precommit or postcommit mode.
         
@@ -101,20 +108,20 @@
         
         :param txn_name: The name of the current transaction.
         :type txn_name: string
+
+ :param halt_on_exception: Flag which indicates whether we halt on unexpected exceptions or not.
+ :type halt_on_exception: boolean
         """
         
         logger = LoggerFactory().crea​te('%s.tools.checker​' % constants.NAME)
-
- hooks_path = os.path.abspath(os.p​ath.join(repo_path, "hooks"))
- project_config = os.path.join(hooks_path, constants.CONFIG_FILENAME)
+ try:
+ hooks_path = os.path.abspath(os.p​ath.join(repo_path, "hooks"))
+ project_config = os.path.join(hooks_path, constants.CONFIG_FILENAME)
+ os.chdir(hooks_path)
             
- # Working directory change to the hooks location.
- os.chdir(hooks_path)
+ logger.debug("RepoGuard initializing...")
+ repoguard = RepoGuard(hook, repo_path)
         
- result = constants.ERROR
- logger.debug("RepoGuard initializing...")
- repoguard = RepoGuard(hook, repo_path)
- try:
             logger.debug("Loading transaction...")
             repoguard.load_trans​action(txn_name)
     
@@ -122,6 +129,7 @@
             main_config = RepoGuardConfig(cons​tants.CONFIG_PATH)
             repoguard.load_confi​g(main_config.templa​te_dirs, project_config)
             
+ logger.debug("Validating configuration...")
             if main_config.validate:
                 repoguard.validate()
             else:
@@ -132,15 +140,19 @@
                 result = repoguard.run_profil​e(profile_name)
             else:
                 result = repoguard.run()
- except ValidateError, exc:
- msg = "Configuration validation error cause: %s"
- logger.exception(msg, str(exc))
- except Exception, exc:
- msg = "%s exception cause: '%s'"
- logger.exception(msg, exc.__class__.__name__, str(exc))
-
- logger.debug("RepoGuard finished with %s.", result)
- if result == constants.SUCCESS:
- return 0
- else:
+
+ logger.debug("RepoGuard finished with %s.", result)
+ if result == constants.SUCCESS:
+ return 0
+ else:
+ return 1
+ except validate.ValidateError:
+ logger.exception("The configuration is invalid!")
             return 1
+ except: # pylint: disable=W0702
+ logger.exception(
+ "An unexpected error occurred during the RepoGuard run! Halt on exception is '%s'." % halt_on_exception)
+ if not halt_on_exception:
+ return 0
+ else:
+ return 1

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r153 - trunk/repoguard/src/repoguard/tools/checker.py schlauch Tobias Schlauch 2012-12-10 09:18:13 PST
Messages per page: