cache module
authorTimo Savola <tsavola@movial.fi>
Sat, 5 Apr 2008 22:18:37 +0000 (01:18 +0300)
committerTimo Savola <tsavola@movial.fi>
Sat, 5 Apr 2008 22:18:46 +0000 (01:18 +0300)
matrix/cache.py [new file with mode: 0644]
matrix/matrix.py

diff --git a/matrix/cache.py b/matrix/cache.py
new file mode 100644 (file)
index 0000000..4d11299
--- /dev/null
@@ -0,0 +1,91 @@
+# Copyright (C) 2006-2008 Movial Oy
+# Authors: Timo Savola <tsavola@movial.fi>
+
+import os
+import re
+from sets import Set as set
+
+import config
+import git
+
+Error = RuntimeError
+
+regex = re.compile(r'([^\s]+)[\s]?([^\s]*)')
+
+def contains(c):
+       if c.from_platform:
+               return True
+
+       for repo in (c.repo, c.meta):
+               if git.ls_files(repo.path, ['-m', '-d']):
+                       if config.force:
+                               return False
+                       else:
+                               raise Error('Dirty files in ' + repo.path)
+
+       path = get_path(c)
+       if not os.path.exists(path):
+               return False
+
+       file = open(path)
+       try:
+               line = file.readline()
+       finally:
+               file.close()
+
+       match = regex.match(line)
+       if not match:
+               return False
+
+       hash, flagstring = match.groups()
+
+       if hash != get_component_hash(c):
+               if config.debug:
+                       print 'New changes in', c.repo.path
+               return False
+
+       if flagstring:
+               flags = flagstring.split(',')
+       else:
+               flags = []
+
+       if set(flags) != set(c.flags):
+               if config.debug:
+                       print 'Obsolete flags for', c.repo.path
+               return False
+
+       return True
+
+def update(c):
+       path = get_path(c)
+
+       dir = os.path.dirname(path)
+       if not os.path.exists(dir):
+               os.makedirs(dir)
+
+       file = open(path, 'w')
+       done = False
+       try:
+               print >>file, get_component_hash(c), ','.join(c.flags)
+               done = True
+       finally:
+               file.close()
+               if not done:
+                       os.remove(path)
+
+def remove(c):
+       path = get_path(c)
+       if os.path.exists(path):
+               os.remove(path)
+
+def get_path(c):
+       return os.path.join(config.cache_dir, c.name)
+
+def get_component_hash(c):
+       return '%s+%s' % (get_repository_hash(c.repo),
+                         get_repository_hash(c.meta))
+
+def get_repository_hash(repo):
+       if repo.active_hash is None:
+               repo.active_hash = git.rev_parse(repo.path, 'HEAD')
+       return repo.active_hash
index 133ae36..b1afa44 100644 (file)
@@ -12,6 +12,7 @@ import sys
 import tarfile
 from sets import Set as set
 
 import tarfile
 from sets import Set as set
 
+import cache
 import config
 import git
 from rootfs import RootFS
 import config
 import git
 from rootfs import RootFS
@@ -505,7 +506,7 @@ class Builder(object):
                return True
 
        def try_cache(self, c):
                return True
 
        def try_cache(self, c):
-               if not component_cached(c):
+               if not cache.contains(c):
                        return False
 
                c.active_state = 'done'
                        return False
 
                c.active_state = 'done'
@@ -572,78 +573,11 @@ class Builder(object):
 
                        c.active_state = 'done'
                        self.in_install = None
 
                        c.active_state = 'done'
                        self.in_install = None
-                       update_cache(c)
+                       cache.update(c)
 
                elif c.active_state != 'killed':
                        raise Error('Unexpected state: %s' % c.active_state)
 
 
                elif c.active_state != 'killed':
                        raise Error('Unexpected state: %s' % c.active_state)
 
-def component_cached(c):
-       if c.from_platform:
-               return True
-
-       for repo in (c.repo, c.meta):
-               if git.ls_files(repo.path, ['-m', '-d']):
-                       if config.force:
-                               return False
-                       else:
-                               raise Error('Dirty files in ' + repo.path)
-
-       path = os.path.join(config.cache_dir, c.name)
-       if not os.path.exists(path):
-               return False
-
-       file = open(path, 'r')
-       line = file.readline()
-       file.close()
-
-       match = re.match(r'([^\s]+)[\s]?([^\s]*)', line)
-       if not match:
-               return False
-
-       hash, flagstring = match.groups()
-
-       if hash != get_component_hash(c):
-               if config.debug:
-                       print 'Component has been changed:', c.name
-               return False
-
-       if flagstring:
-               flags = flagstring.split(',')
-       else:
-               flags = []
-
-       if set(flags) != set(c.flags):
-               if config.debug:
-                       print 'Component flags have been changed:', c.name
-               return False
-
-       return True
-
-def update_cache(c):
-       path = os.path.join(config.cache_dir, c.name)
-
-       dir = os.path.dirname(path)
-       if not os.path.exists(dir):
-               os.makedirs(dir)
-
-       file = open(path, 'w')
-       done = False
-       try:
-               print >>file, get_component_hash(c), ','.join(c.flags)
-               done = True
-       finally:
-               file.close()
-               if not done:
-                       os.remove(path)
-
-def get_component_hash(c):
-       return '%s+%s' % (get_repository_hash(c.repo), get_repository_hash(c.meta))
-
-def get_repository_hash(repo):
-       if repo.active_hash is None:
-               repo.active_hash = git.rev_parse(repo.path, 'HEAD')
-       return repo.active_hash
-
 def spawn_make(c, jobs, target):
        board = config.boards[config.board]
 
 def spawn_make(c, jobs, target):
        board = config.boards[config.board]
 
@@ -682,15 +616,13 @@ def clean_components(targets):
                c = config.components[name]
                print 'Cleaning', c.repo.path
 
                c = config.components[name]
                print 'Cleaning', c.repo.path
 
+               cache.remove(c)
+
                files = git.ls_files(c.repo.path, ['-o'])
                paths = [os.path.join(c.repo.path, i) for i in files]
                paths.sort()
                paths.reverse()
 
                files = git.ls_files(c.repo.path, ['-o'])
                paths = [os.path.join(c.repo.path, i) for i in files]
                paths.sort()
                paths.reverse()
 
-               cache = os.path.join(config.cache_dir, name)
-               if os.path.exists(cache):
-                       paths.append(cache)
-
                for path in paths:
                        if git.contains_database(path):
                                continue
                for path in paths:
                        if git.contains_database(path):
                                continue