WIP: implement check subcommand
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 7 Jul 2014 12:09:59 +0000 (15:09 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 24 Sep 2014 11:51:41 +0000 (14:51 +0300)
Change-Id: Id18d3c81387f2070096657cbbf4360e19782b961
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
repocache_adm/adm.py
repocache_adm/cmd_check.py [new file with mode: 0644]

index 5d93815a37cf7b8e130bb154366b79cf90f8ded7..14707a889787336e26276e7dcbf146a55df24f7a 100755 (executable)
@@ -24,6 +24,7 @@ import sys
 
 from argparse import ArgumentParser
 
+from repocache_adm.cmd_check import Check
 from repocache_adm.cmd_stat import Stat
 
 
@@ -38,7 +39,7 @@ def parse_args(argv):
     subparsers = parser.add_subparsers()
 
     # Add subcommands
-    for subcommand in (Stat,):
+    for subcommand in (Stat, Check,):
         subcommand.add_subparser(subparsers)
 
     return parser.parse_args(argv)
diff --git a/repocache_adm/cmd_check.py b/repocache_adm/cmd_check.py
new file mode 100644 (file)
index 0000000..dfc969c
--- /dev/null
@@ -0,0 +1,84 @@
+#!/usr/bin/python -u
+# vim:fileencoding=utf-8:et:ts=4:sw=4:sts=4
+#
+# Copyright (C) 2013 Intel Corporation <markus.lehtonen@linux.intel.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+"""The check subcommand"""
+
+import logging
+import os
+
+from repocache_adm.common import AdmError, SubcommandBase, repo_is_bare
+
+class Check(SubcommandBase):
+    """Subcommand for checking the repo cache"""
+
+    name = 'check'
+    description = 'Check consistency of repocache'
+    help_msg = None
+
+    @classmethod
+    def main(cls, args):
+        """Entry point for 'check' subcommand"""
+
+        log = get_logger(cls.name)
+
+        if not os.path.isdir(args.cache_dir):
+            log.error("repocache basedir '%s' not found" % args.cache_dir)
+            return 1
+
+        log.info("Checking repository cache in '%s'" % args.cache_dir)
+
+        basedirs = []
+        basefiles = []
+        for fname in os.listdir(args.cache_dir):
+            if os.path.isdir(os.path.join(args.cache_dir, fname)):
+                basedirs.append(fname)
+            else:
+                basefiles.append(fname)
+        if basefiles:
+            log.warning("Non-directory files found in repocache base:\n%s" %
+                        '\n'.join(['  ' + fname for fname in basefiles]))
+
+        for dname in basedirs:
+            for fname in os.listdir(os.path.join(args.cache_dir, dname)):
+                rel_path = os.path.join(dname, fname)
+                full_path = os.path.join(args.cache_dir, rel_path)
+                if os.path.isdir(full_path):
+                    try:
+                        repo_is_bare(full_path)
+                    except AdmError:
+                        log.warning("Cache contains directory %s that doesn't "
+                                    "look like a Git repository" % rel_path)
+                    else:
+                        if not os.path.isfile(full_path + '.lock'):
+                            log.warning("Repo '%s' doesn't have a lock file" %
+                                         rel_path)
+                else:
+                    stat = os.stat(full_path)
+                    basename, ext = os.path.splitext(full_path)
+                    if ext != '.lock':
+                        log.warning("Cache contains regular file that doesn't "
+                                    "look like a lock file: %s" % rel_path)
+                    else:
+                        if not os.path.isdir(basename):
+                            log.warning("Orphan lock file '%s'" % rel_path)
+                        if stat.st_size != 0:
+                            log.warning("Non-empty lock file: %s (%d bytes)" %
+                                        (rel_path, stat.st_size))
+        return 0
+