Debian Codegraph @

lib/statuscache.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007 Lukáš Lalinský <[email protected]om>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 import sys
21
22 from breezy import (
23 errors,
24 osutils,
25 workingtree,
26 )
27 from PyQt5 import QtCore
28
29
30 class CacheEntry(object):
31
32 __slots__ = ['status', 'children']
33
34 def __init__(self):
35 self.status = 'unknown'
36 self.children = {}
37
38 def __repr__(self):
39 return '(%s, %r)' % (self.status, self.children)
40
41
42 class StatusCache(QtCore.QObject):
43
44 def __init__(self, window):
45 QtCore.QObject.__init__(self)
46 self.fileSystemWatcher = QtCore.QFileSystemWatcher(self)
47 self.fileSystemWatcher.directoryChanged['QString'].connect(self.invalidateDirectory)
48 self.autoRefreshPath = None
49 self.autoRefreshTimer = QtCore.QTimer(self)
50 self.autoRefreshTimer.setSingleShot(True)
51 self.autoRefreshTimer.timeout.connect(self.autoRefresh)
52 self.window = window
53 self.cache = CacheEntry()
54
55 def _cacheStatus(self, path, status, root=None):
56 if root is None:
57 root = self.cache
58 entry = root
59 for part in path:
60 if part not in entry.children:
61 entry.children[part] = CacheEntry()
62 entry = entry.children[part]
63 entry.status = status
64 return entry
65
66 def _getCacheEntry(self, parts):
67 entry = self.cache
68 for part in parts:
69 entry = entry.children[part]
70 return entry
71
72 def _cacheDirectoryStatus(self, path):
73 p = '/'.join(path)
74 if sys.platform != 'win32':
75 p = '/' + p
76 # print "caching", p
77 try:
78 # to stop bzr-svn from trying to give status on svn checkouts
79 # if not QtCore.QDir(p).exists('.bzr'):
80 # raise errors.NotBranchError(p)
81 working_tree, relpath = workingtree.WorkingTree.open_containing(p)
82 except errors.BzrError:
83 self.fileSystemWatcher.addPath(p)
84 return self._cacheStatus(path, 'non-versioned')
85 self.fileSystemWatcher.addPath(working_tree.basedir)
86 basis_tree = working_tree.basis_tree()
87 root = self._cacheStatus(osutils.splitpath(working_tree.basedir), 'branch')
88 # delta will be a TreeDelta: commit 7389 makes TreeDelta HOLD TreeChange objects in
89 # list member variables called added, removed, renamed, copied, kind_changes, modified
90 # unchanged, unversioned and missing
91 delta = working_tree.changes_from(basis_tree, want_unchanged=True, want_unversioned=True)
92 # ... and thus entry will be a TreeChange object, with the path in,
93 # perhaps unsurprisingly, 'path' rather than entry[0]. Path is a tuple of
94 # old and new path, so get whatever works
95 for entry in delta.added:
96 self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'added', root=root)
97 for entry in delta.removed:
98 # BUGBUG - RJLRJL this FIXME might be why the test_tree_widget rename and
99 # move_and_rename and its cousins are failing
100 # FIXME
101 self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'modified', root=root)
102 # self._cacheStatus(osutils.splitpath(entry[0]), 'removed', root=root)
103 for entry in delta.modified:
104 self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'modified', root=root)
105 for entry in delta.unchanged:
106 self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'unchanged', root=root)
107 # self._cacheStatus(osutils.splitpath(entry[0]), 'unchanged', root=root)
108 for entry in delta.unversioned:
109 self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'non-versioned', root=root)
110 try:
111 return self._getCacheEntry(path)
112 except KeyError:
113 self.fileSystemWatcher.addPath(p)
114 return self._cacheStatus(path, 'non-versioned')
115
116 def getFileStatus(self, path, name):
117 try:
118 parent_entry = self._getCacheEntry(path)
119 except KeyError:
120 parent_entry = None
121 if parent_entry is None or parent_entry.status == 'unknown':
122 parent_entry = self._cacheDirectoryStatus(path)
123 try:
124 entry = parent_entry.children[name]
125 except KeyError:
126 if parent_entry.status == 'non-versioned':
127 return 'non-versioned'
128 else:
129 if sys.platform == 'win32':
130 return 'non-versioned'
131 print("NOW WHAT??")
132 return entry.status
133
134 def getDirectoryStatus(self, path, name):
135 path = path + [name]
136 try:
137 entry = self._getCacheEntry(path)
138 except KeyError:
139 entry = self._cacheDirectoryStatus(path)
140 else:
141 if entry.status == 'unknown':
142 entry = self._cacheDirectoryStatus(path)
143 # print path, entry.status
144 return entry.status
145
146 def invalidateDirectory(self, path):
147 path = str(path)
148 try:
149 parts = osutils.splitpath(path)
150 entry = self.cache
151 for part in parts[:-1]:
152 entry = entry.children[part]
153 print("Removing", path, "from the cache")
154 del entry.children[parts[-1]]
155 except KeyError:
156 pass
157 else:
158 self.autoRefreshPath = path
159 self.autoRefreshTimer.start(1000)
160
161 def autoRefresh(self):
162 self.window.autoRefresh(self.autoRefreshPath)