Rfaulk has uploaded a new change for review.
https://gerrit.wikimedia.org/r/83382
Change subject: mv - init functionality into config.py.
......................................................................
mv - init functionality into config.py.
Change-Id: Iee231f31f51efb46312a23b6fd46bf96fe187fbc
---
A sartoris/config.py
M sartoris/sartoris.py
2 files changed, 142 insertions(+), 100 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/sartoris refs/changes/82/83382/1
diff --git a/sartoris/config.py b/sartoris/config.py
new file mode 100644
index 0000000..f68b894
--- /dev/null
+++ b/sartoris/config.py
@@ -0,0 +1,133 @@
+"""
+Config file for Sartoris.
+"""
+
+__authors__ = {
+ 'Ryan Faulkner': '[email protected]',
+ 'Patrick Reilly': '[email protected]',
+ 'Ryan Lane': '[email protected]',
+}
+__date__ = '2013-09-08'
+
+from dulwich.config import StackedConfig
+import subprocess
+import sys
+import logging
+
+# Native git call
+GIT_CALL = '/usr/bin/git'
+
+# Codes emitted on exit conditions
+exit_codes = {
+ 1: 'Operation failed. Exiting.',
+ 2: 'A deployment has already been started. Exiting.',
+ 3: 'Please enter valid arguments. Exiting.',
+ 4: 'Missing lock file. Exiting.',
+ 5: 'Could not reset. Exiting.',
+ 6: 'Diff failed. Exiting.',
+ 7: 'Missing tag(s). Exiting.',
+ 8: 'Could not find last deploy tag. Exiting.',
+ 9: 'Could not get listing from deploy target. Exiting.',
+ 10: 'Please specify number of deploy tags to emit with -c. Exiting',
+ 11: 'Could not find any deploys. Exiting',
+ 12: 'Tagging failed. Exiting',
+ 20: 'Cannot find top level directory for the git repository. Exiting.',
+ 21: 'Missing system configuration item "hook-dir". Exiting.',
+ 22: 'Missing repo configuration item "tag-prefix". '
+ 'Please configure this using:'
+ '\n\tgit config tag-prefix <repo>',
+ 23: 'Missing system configuration item "path". Exiting.',
+ 24: 'Missing system configuration item "user". Exiting.',
+ 25: 'Missing system configuration item "target". Exiting.',
+ 26: 'Missing system configuration item "remote". Exiting.',
+ 27: 'Missing system configuration item "branch". Exiting.',
+ 28: 'Missing system configuration item "user.name". Exiting.',
+ 29: 'Missing system configuration item "user.email". Exiting.',
+ 30: 'No deploy started. Please run: git deploy start',
+ 31: 'Failed to write tag on sync. Exiting.',
+ 32: 'Failed to write the .deploy file. Exiting.',
+ 40: 'Failed to run sync script. Exiting.',
+ 50: 'Failed to read the .deploy file. Exiting.',
+}
+
+
+# NullHandler was added in Python 3.1.
+try:
+ NullHandler = logging.NullHandler
+except AttributeError:
+ class NullHandler(logging.Handler):
+ def emit(self, record):
+ pass
+
+# Add a do-nothing NullHandler to the module logger to prevent "No handlers
+# could be found" errors. The calling code can still add other, more useful
+# handlers, or otherwise configure logging.
+log = logging.getLogger(__name__)
+log.addHandler(NullHandler())
+
+
+def set_log(args, out, err):
+ """
+ Sets the logger.
+
+ Parameters:
+
+ args - command line args
+ out - stdout
+ err - stderr
+ """
+ level = logging.WARNING - ((args.verbose - args.quiet) * 10)
+ if args.silent:
+ level = logging.CRITICAL + 1
+
+ log_format = "%(asctime)s %(levelname)-8s %(message)s"
+ handler = logging.StreamHandler(err)
+ handler.setFormatter(logging.Formatter(fmt=log_format,
+ datefmt='%b-%d %H:%M:%S'))
+ log.addHandler(handler)
+ log.setLevel(level)
+
+
+def configure():
+ """ Parse configuration from git config """
+ sc = StackedConfig(StackedConfig.default_backends())
+ config = {}
+
+ # Get top level directory of project
+ proc = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ config['top_dir'] = proc.communicate()[0].strip()
+
+ if proc.returncode != 0:
+ exit_code = 20
+ log.error("{0} :: {1}".format(__name__, exit_codes[exit_code]))
+ sys.exit(exit_code)
+
+ config['deploy_file'] = config['top_dir'] + '/.git/.deploy'
+
+ # Define the key names, git config names, and error codes
+ config_elements = {
+ 'hook_dir': ('deploy', 'hook-dir', 21),
+ 'path': ('deploy', 'path', 23),
+ 'user': ('deploy', 'user', 24),
+ 'target': ('deploy', 'target', 25),
+ 'repo_name': ('deploy', 'tag-prefix', 22),
+ 'remote': ('deploy', 'remote', 26),
+ 'branch': ('deploy', 'branch', 27),
+ 'user.name': ('user', 'name', 28),
+ 'user.email': ('user', 'email', 29),
+ }
+
+ # Assign the values of each git config element
+ for key, value in config_elements.iteritems():
+ try:
+ config[key] = sc.get(value[0], value[1])
+ except KeyError:
+ exit_code = value[2]
+ log.error("{0} :: {1}".format(__name__, exit_codes[exit_code]))
+ sys.exit(exit_code)
+
+ config['sync_dir'] = '{0}/sync'.format(config['hook_dir'])
+
+ return config
diff --git a/sartoris/sartoris.py b/sartoris/sartoris.py
index 3e25dbb..2baf72a 100755
--- a/sartoris/sartoris.py
+++ b/sartoris/sartoris.py
@@ -1,9 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-"""`This`_ is a tool to manage using git as a deployment management tool
+
+"""
+
+`This`_ is a tool to manage using git as a deployment management tool
.. _This: https://gerrit.wikimedia.org/r/gitweb?p=sartoris.git
+
"""
+
__license__ = """\
Copyright (c) 2012-2013 Wikimedia Foundation <[email protected]>
@@ -20,51 +25,18 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\
"""
-import logging
import argparse
import os
import stat
import sys
from re import search
import subprocess
-from dulwich.config import StackedConfig
from dulwich.repo import Repo
from dulwich.objects import Tag, Commit, parse_timezone
from datetime import datetime
import json
from time import time
-
-exit_codes = {
- 1: 'Operation failed. Exiting.',
- 2: 'A deployment has already been started. Exiting.',
- 3: 'Please enter valid arguments. Exiting.',
- 4: 'Missing lock file. Exiting.',
- 5: 'Could not reset. Exiting.',
- 6: 'Diff failed. Exiting.',
- 7: 'Missing tag(s). Exiting.',
- 8: 'Could not find last deploy tag. Exiting.',
- 9: 'Could not get listing from deploy target. Exiting.',
- 10: 'Please specify number of deploy tags to emit with -c. Exiting',
- 11: 'Could not find any deploys. Exiting',
- 12: 'Tagging failed. Exiting',
- 20: 'Cannot find top level directory for the git repository. Exiting.',
- 21: 'Missing system configuration item "hook-dir". Exiting.',
- 22: 'Missing repo configuration item "tag-prefix". '
- 'Please configure this using:'
- '\n\tgit config tag-prefix <repo>',
- 23: 'Missing system configuration item "path". Exiting.',
- 24: 'Missing system configuration item "user". Exiting.',
- 25: 'Missing system configuration item "target". Exiting.',
- 26: 'Missing system configuration item "remote". Exiting.',
- 27: 'Missing system configuration item "branch". Exiting.',
- 28: 'Missing system configuration item "user.name". Exiting.',
- 29: 'Missing system configuration item "user.email". Exiting.',
- 30: 'No deploy started. Please run: git deploy start',
- 31: 'Failed to write tag on sync. Exiting.',
- 32: 'Failed to write the .deploy file. Exiting.',
- 40: 'Failed to run sync script. Exiting.',
- 50: 'Failed to read the .deploy file. Exiting.',
-}
+from config import set_log, log, configure, exit_codes
class SartorisError(Exception):
@@ -76,14 +48,6 @@
@property
def exit_code(self):
return self._exit_code
-
-# NullHandler was added in Python 3.1.
-try:
- NullHandler = logging.NullHandler
-except AttributeError:
- class NullHandler(logging.Handler):
- def emit(self, record):
- pass
def remove_readonly(fn, path, excinfo):
@@ -97,12 +61,6 @@
elif fn is os.remove:
os.chmod(path, stat.S_IWRITE)
os.remove(path)
-
-# Add a do-nothing NullHandler to the module logger to prevent "No handlers
-# could be found" errors. The calling code can still add other, more useful
-# handlers, or otherwise configure logging.
-log = logging.getLogger(__name__)
-log.addHandler(NullHandler())
def parseargs():
@@ -188,47 +146,7 @@
return cls.__instance
def _configure(self):
- """ Parse configuration from git config """
- sc = StackedConfig(StackedConfig.default_backends())
- self.config = {}
-
- # Get top level directory of project
- proc = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- self.config['top_dir'] = proc.communicate()[0].strip()
-
- if proc.returncode != 0:
- exit_code = 20
- log.error("{0} :: {1}".format(__name__, exit_codes[exit_code]))
- sys.exit(exit_code)
-
- self.config['deploy_file'] = self.config['top_dir'] + \
- '/.git/.deploy'
-
- # Define the key names, git config names, and error codes
- config_elements = {
- 'hook_dir': ('deploy', 'hook-dir', 21),
- 'path': ('deploy', 'path', 23),
- 'user': ('deploy', 'user', 24),
- 'target': ('deploy', 'target', 25),
- 'repo_name': ('deploy', 'tag-prefix', 22),
- 'remote': ('deploy', 'remote', 26),
- 'branch': ('deploy', 'branch', 27),
- 'user.name': ('user', 'name', 28),
- 'user.email': ('user', 'email', 29),
- }
-
- # Assign the values of each git config element
- for key, value in config_elements.iteritems():
- try:
- self.config[key] = sc.get(value[0], value[1])
- except KeyError:
- exit_code = value[2]
- log.error("{0} :: {1}".format(__name__, exit_codes[exit_code]))
- sys.exit(exit_code)
-
- self.config['sync_dir'] = '{0}/sync'.format(self.config['hook_dir'])
+ self.config = configure()
def _get_current_lock_user(self):
"""
@@ -682,16 +600,7 @@
if err is None: # pragma: nocover
err = sys.stderr
args = parseargs()
- level = logging.WARNING - ((args.verbose - args.quiet) * 10)
- if args.silent:
- level = logging.CRITICAL + 1
-
- log_format = "%(asctime)s %(levelname)-8s %(message)s"
- handler = logging.StreamHandler(err)
- handler.setFormatter(logging.Formatter(fmt=log_format,
- datefmt='%b-%d %H:%M:%S'))
- log.addHandler(handler)
- log.setLevel(level)
+ set_log(args, out, err)
log.debug("Sartoris is ready to run")
--
To view, visit https://gerrit.wikimedia.org/r/83382
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iee231f31f51efb46312a23b6fd46bf96fe187fbc
Gerrit-PatchSet: 1
Gerrit-Project: sartoris
Gerrit-Branch: master
Gerrit-Owner: Rfaulk <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits