| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
om> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 77 |
try: |
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
delta = working_tree.changes_from(basis_tree, want_unchanged=True, want_unversioned=True) |
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
self._cacheStatus(osutils.splitpath(entry.path[0] or entry.path[1]), 'modified', root=root) |
| 102 |
|
| 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 |
|
| 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 |
|
| 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) |