aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo@ubuntu.com>2013-01-31 11:51:17 +0100
committerMilo Casagrande <milo@ubuntu.com>2013-01-31 11:51:17 +0100
commit8c258322499c2117adab7696ecf60ced1a9ca542 (patch)
treeae1ac1311f6fcbd83d151043eae599c6e01751cd
parent6f57880fd5b33cb9a3b5d94636f02860e56151e8 (diff)
Added preliminary logging support.
-rw-r--r--scripts/update-repos78
1 files changed, 68 insertions, 10 deletions
diff --git a/scripts/update-repos b/scripts/update-repos
index 154e0fa..bd9102c 100644
--- a/scripts/update-repos
+++ b/scripts/update-repos
@@ -5,6 +5,8 @@ import argparse
import os
import re
import subprocess
+import logging
+from logging.handlers import TimedRotatingFileHandler
from tempfile import gettempdir
@@ -15,6 +17,12 @@ GIT_DIRECTORY_ENDS = re.compile(".+\.git$")
LOCK_FILE_NAME = "update-repos.lock"
LOCK_FILE = os.path.join(gettempdir(), LOCK_FILE_NAME)
+DEFAULT_LOG_DIR = "/var/log/rhodecode"
+LOG_FILE_NAME = str(__file__) + ".log"
+
+# Default logger.
+logger = logging.getLogger(__file__)
+
def args_parser():
"""Sets up the argument parser."""
@@ -24,18 +32,59 @@ def args_parser():
help="The directory where repositories are stored.")
parser.add_argument("--user",
help="User to run the commands as.")
+ parser.add_argument("--log-dir",
+ default=DEFAULT_LOG_DIR,
+ help="Directory to store logs. Defaults to '%s'." %
+ DEFAULT_LOG_DIR)
+ parser.add_argument("--debug",
+ action="store_true",
+ help="Print debugging statements.")
return parser
-def fetch_updates():
- """Traverse the file system and fetc updates."""
- for root, dirs, files in os.walk(os.path.abspath(args.repos_dir)):
+def setup_logging(debug, log_dir):
+ """Sets up logging.
+
+ :param debug: If the level should be set to DEBUG.
+ :type bool
+ :param log_dir: Where to store file based logs.
+ """
+ th_formatter = "%(ascitime)s %(levelname)-8s %(message)s"
+ ch_formatter = "%(message)s"
+ log_file = os.path.join(log_dir, LOG_FILE_NAME)
+
+ timed_handler = logging.TimedRotatingFileHandler(log_file,
+ when='midnight')
+ timed_handler.setFormatter(logging.Formatter(th_formatter))
+
+ console_handler = logging.StreamHandler()
+ console_handler.setLevel(logging.WARNING)
+ console_handler.setFormatter(logging.Formatter(ch_formatter))
+
+ if debug:
+ logger.setLevel(logging.DEBUG)
+ timed_handler.setLevel(logging.DEBUG)
+ else:
+ logger.setLevel(logging.INFO)
+ timed_handler.setLevel(logging.INFO)
+
+ logger.addHandler(timed_handler)
+ logger.addHandler(console_handler)
+
+
+def fetch_updates(repos_dir, user):
+ """Traverse the file system and fetch updates.
+
+ :param repos_dir: The directory holding repositories.
+ :param user: The use to run the commands as.
+ """
+ for root, dirs, files in os.walk(os.path.abspath(repos_dir)):
if GIT_DIRECTORY_ENDS.match(root):
if files:
# We really are in a git repository.
cmd_args = []
- if args.user:
- cmd_args = ["sudo", "-u", args.user, "-H"]
+ if user:
+ cmd_args = ["sudo", "-u", user, "-H"]
cmd_args += ["git", "fetch", "--all", "-q"]
repo_name = os.path.basename(root)
process = subprocess.Popen(cmd_args,
@@ -43,26 +92,35 @@ def fetch_updates():
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
- print "Updating '%s' repository..." % repo_name
+ logger.info("Fetching updates for '%s' repository." % repo_name)
p_out, p_err = process.communicate()
if process.returncode != 0:
- print ("Error fetching updates for repository '%s'" % \
- repo_name)
+ logger.error("Error fetching updates for repository "
+ "'%s'." % repo_name)
+ logger.debug(p_err)
else:
# git repositories always have a HEAD file, or it means they
# are empty.
+ logger.debug("Skipping directory '%s': valid git one, but "
+ "looks empty." % root)
continue
+ else:
+ logger.debug("Skipped directory '%s', not matching a git "
+ "one." % root)
if __name__ == '__main__':
parser = args_parser()
args = parser.parse_args()
+ setup_logging(args.debug)
+
if os.path.exists(LOCK_FILE):
- print "Another process is still running: cannot acquire lock."
+ logger.warning("Another process is still running: cannot acquire "
+ "lock.")
else:
try:
with open(LOCK_FILE, 'w'):
- fetch_updates()
+ fetch_updates(args.repos_dir, args.user)
finally:
os.unlink(LOCK_FILE)