forked from turnkeylinux/tklbam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirindex.py
209 lines (158 loc) · 5.57 KB
/
dirindex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#
# Copyright (c) 2010-2013 Liraz Siri <[email protected]>
#
# This file is part of TKLBAM (TurnKey GNU/Linux BAckup and Migration).
#
# TKLBAM is open source 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 3 of
# the License, or (at your option) any later version.
#
import re
import os
import stat
from os.path import *
from pathmap import PathMap
class Error(Exception):
pass
class DirIndex(dict):
class Record:
def __init__(self, path, mod, uid, gid, size, mtime,
symlink=None):
self.path = path
self.mod = mod
self.uid = uid
self.gid = gid
self.size = size
self.mtime = mtime
self.symlink = symlink
@classmethod
def frompath(cls, path):
st = os.lstat(path)
symlink = os.readlink(path) \
if stat.S_ISLNK(st.st_mode) else None
rec = cls(path,
st.st_mode,
st.st_uid, st.st_gid,
st.st_size, st.st_mtime,
symlink)
return rec
@classmethod
def fromline(cls, line):
vals = line.strip().split('\t')
if len(vals) not in (6, 7):
raise Error("bad index record: " + line)
path = vals[0]
del vals[0]
vals = [ int(val, 16) for val in vals[:5] ] + vals[5:]
return cls(path, *vals)
def fmt(self):
vals = [ self.path ]
for val in ( self.mod, self.uid, self.gid, self.size, self.mtime ):
vals.append("%x" % val)
if self.symlink:
vals.append(self.symlink)
return "\t".join(vals)
def __repr__(self):
return "DirIndex.Record(%s, mod=%s, uid=%d, gid=%d, size=%d, mtime=%d)" % \
(`self.path`, oct(self.mod), self.uid, self.gid, self.size, self.mtime)
@classmethod
def create(cls, path_index, paths):
"""create index from paths"""
di = cls()
di.walk(*paths)
di.save(path_index)
return di
def __init__(self, fromfile=None):
if fromfile:
for line in file(fromfile).readlines():
if not line.strip():
continue
rec = DirIndex.Record.fromline(line)
self[rec.path] = rec
def add_path(self, path):
"""add a single path to the DirIndex"""
self[path] = DirIndex.Record.frompath(path)
def walk(self, *paths):
"""walk paths and add files to index"""
pathmap = PathMap(paths)
def _walk(dir):
dentries = []
for dentry in os.listdir(dir):
path = join(dir, dentry)
if path in pathmap.excludes:
continue
dentries.append(dentry)
if not islink(path) and isdir(path):
for val in _walk(path):
yield val
yield dir, dentries
for path in pathmap.includes:
if not lexists(path):
continue
self.add_path(path)
if islink(path) or not isdir(path):
continue
for dpath, dentries in _walk(path):
for dentry in dentries:
path = join(dpath, dentry)
self.add_path(path)
def prune(self, *paths):
"""prune index down to paths that are included AND not excluded"""
pathmap = PathMap(paths)
for path in self.keys():
if not path in pathmap:
del self[path]
def save(self, tofile):
fh = file(tofile, "w")
paths = self.keys()
paths.sort()
for path in paths:
print >> fh, self[path].fmt()
def diff(self, other):
a = set(self)
b = set(other)
files_new = []
paths_stat = []
for path in (b - a):
mod = other[path].mod
# ignore Unix sockets
if stat.S_ISSOCK(mod):
continue
if stat.S_ISDIR(mod):
paths_stat.append(path)
else:
files_new.append(path)
paths_in_both = b & a
files_edited = []
def attrs_equal(attrs, a, b):
for attr in attrs:
if getattr(a, attr) != getattr(b, attr):
return False
return True
def symlink_equal(a, b):
if a.symlink and (a.symlink == b.symlink):
return True
return False
for path in paths_in_both:
if not attrs_equal(('size', 'mtime'), self[path], other[path]):
mod = other[path].mod
if not (stat.S_ISDIR(mod) or stat.S_ISSOCK(mod)) \
and not symlink_equal(self[path], other[path]):
files_edited.append(path)
continue
if not attrs_equal(('mod', 'uid', 'gid'), self[path], other[path]):
paths_stat.append(path)
return files_new, files_edited, paths_stat
create = DirIndex.create
def read_paths(fh):
paths = []
for line in fh.readlines():
path = re.sub(r'#.*', '', line).strip()
if not path:
continue
# only accept absolute paths
if not re.match(r'^-?/', path):
raise Error(`path` + " is not an absolute path")
paths.append(path)
return paths