optimize dirty files check
authorTimo Savola <tsavola@movial.fi>
Wed, 16 Apr 2008 14:04:40 +0000 (17:04 +0300)
committerTimo Savola <tsavola@movial.fi>
Wed, 16 Apr 2008 14:04:40 +0000 (17:04 +0300)
matrix/components.py
matrix/git.py
matrix/repositories.py

index 3014f7b..6ec1224 100644 (file)
@@ -50,7 +50,7 @@ class Component(object):
        def is_dirty(self):
                if self.__dirty is None:
                        for repo in (self.source, self.meta):
-                               if repo.dirty_files():
+                               if repo.is_dirty():
                                        self.__dirty = True
                                        break
 
index 172401c..fdb2447 100644 (file)
@@ -2,8 +2,10 @@
 # Authors: Timo Savola <tsavola@movial.fi>
 #          Toni Timonen
 
-import os, sys
 import bz2
+import os
+import signal
+import sys
 
 import config
 
@@ -55,7 +57,7 @@ def call(args, workdir=None, quiet=False, fail=False):
 
        return status
 
-def call_output(args, workdir=None, fd=None, lines=True,wait=True):
+def call_output(args, workdir=None, fd=None, lines=True, wait=True, any=False):
        args = ['git'] + args
 
        if config.debug:
@@ -91,20 +93,28 @@ def call_output(args, workdir=None, fd=None, lines=True,wait=True):
 
                os._exit(127)
 
+       killed = False
+
        if fd is None:
                os.close(output)
 
                f = os.fdopen(input, 'r')
-               if lines:
-                       contents = f.readlines()
-               else:
-                       contents = f.read()
-               f.close()
+               try:
+                       if lines:
+                               contents = f.readlines()
+                       elif any:
+                               contents = f.read(1)
+                               os.kill(pid, signal.SIGTERM)
+                               killed = True
+                       else:
+                               contents = f.read()
+               finally:
+                       f.close()
 
        if not wait and fd is not None:
                return
        pid, status = os.waitpid(pid, 0)
-       if status != 0:
+       if status != 0 and not killed:
                raise Error(args, workdir)
 
        if fd is None:
@@ -180,14 +190,20 @@ def ls_tree(name, treeish, name_only=False, recursive=False):
 
        return call_output(['ls-tree'] + options + [treeish], workdir=name)
 
-def ls_files(name, options, exclude=None):
+def __ls_files(name, options, exclude, any):
        cmd = ['ls-files'] + options
 
        if exclude:
                for e in exclude:
                        cmd.append('--exclude=%s' % e)
 
-       return call_output(cmd, workdir=name)
+       return call_output(cmd, workdir=name, any=any)
+
+def ls_files(name, options, exclude=None):
+       return __ls_files(name, options, exclude, any=False)
+
+def any_files(name, options, exclude=None):
+       return __ls_files(name, options, exclude, any=True)
 
 def archive(name, arch_name, prefix=None, treeish='HEAD'):
        cmd = ['archive', '--format=tar']
index ccc290b..aa09188 100644 (file)
@@ -95,14 +95,12 @@ class Repository(object):
 
                git.pull(self.path)
 
-       def ls_files(self, options):
-               return git.ls_files(self.path, options, exclude=self.exclude)
-
        def other_files(self):
-               return self.ls_files(['-o'])
+               return git.ls_files(self.path, ['-o'], exclude=self.exclude)
 
-       def dirty_files(self):
-               return self.ls_files(['-m', '-d'])
+       def is_dirty(self):
+               return git.any_files(self.path, ['-m', '-d'],
+                                    exclude=self.exclude)
 
        def clean(self):
                print 'Cleaning', self