Skip to content

Commit 7d8cf26

Browse files
committed
Reworked compare function to allow users to specify what they want to check for. Defaults to name, size, and hash.
1 parent 3fdbb62 commit 7d8cf26

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

setup.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
setup(
44
name='xfl',
55
packages=['xfl'],
6-
url='',
7-
license='',
8-
author='',
9-
author_email='',
6+
url='https://github.com/fireflash38/xfl',
7+
license='CeCILL v2',
8+
maintainer='Ashley Straw',
9+
maintainer_email='[email protected]',
1010
description='',
1111
version='.06',
1212
install_requires='path.py',
13-
zip_safe=True,
1413
scripts=['bin/create_manifest.py']
1514
)

xfl/xfl.py

+53-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# - DirTree options to handle owner, hash, ...
3232

3333
#--- IMPORTS ------------------------------------------------------------------
34+
import logging
3435
import sys
3536

3637
# path module to easily handle files and dirs:
@@ -198,26 +199,76 @@ def compare_files(et1, et2):
198199
return False
199200
if et1.tag == TAG_DIR:
200201
if et1.get(ATTR_NAME) != et2.get(ATTR_NAME):
202+
print ("Directory name verification failed: {0} != {1}".format(et1.get(ATTR_NAME), et2.get(ATTR_NAME)))
201203
return False
202204
else:
203205
return True
204206
elif et1.tag == TAG_FILE:
205207
if et1.get(ATTR_NAME) != et2.get(ATTR_NAME):
206208
#Compare names
209+
print ("File name verification failed: {0} != {1}".format(et1.get(ATTR_NAME), et2.get(ATTR_NAME)))
207210
return False
208211
if et1.get(ATTR_SIZE) != et2.get(ATTR_SIZE):
209212
#Compare sizes
213+
print ("File {0} failed size verification".format(et1.get(ATTR_NAME)))
210214
return False
211215
if et1.get(ATTR_HASH) != et2.get(ATTR_HASH):
212216
#Compare file hash (if the one has a hash and the other does not, it'll return false)
217+
print ("File {0} failed hash verification".format(et1.get(ATTR_NAME)))
213218
return False
214219
else:
215220
return True
216221
else:
217222
raise TypeError
218223

219224

220-
def compare_DT(dirTree1, dirTree2):
225+
def comp_filesv2(tree1, tree2, values=None, log=None):
226+
"""
227+
Compares 2 tree elements.
228+
229+
:param tree1:
230+
:param tree2:
231+
:param values: List of what to check.
232+
Default: ATTR_NAME, ATTR_HASH, ATTR_SIZE
233+
:param log: Logger. Defaults to logging.getLogger(__name__)
234+
:return:
235+
"""
236+
if log is None:
237+
#Default to root logger.
238+
log = logging.getLogger()
239+
240+
if values is None:
241+
#Default to checking name, size, and hash.
242+
values = [ATTR_NAME, ATTR_HASH, ATTR_SIZE]
243+
else:
244+
for value in values:
245+
#Check to make sure that the list of values is valid.
246+
if value not in [ATTR_SIZE, ATTR_HASH, ATTR_NAME, ATTR_MTIME, ATTR_OWNER, ATTR_TIME]:
247+
raise TypeError("Checking value %s is INVALID" % value)
248+
249+
if tree1.tag != tree2.tag:
250+
return False
251+
252+
if tree1.tag == TAG_DIR:
253+
#Means this is a directory, compare names.
254+
if tree1.get(ATTR_NAME) != tree2.get(ATTR_NAME):
255+
log.info("Directory name verification failed: %s != %s", tree1.get(ATTR_NAME), tree2.get(ATTR_NAME))
256+
return False
257+
else:
258+
return True
259+
elif tree1.tag == TAG_FILE:
260+
#Tagged as a file
261+
ret_value = True
262+
for value in values:
263+
#If any one of these checks fails, the whole thing fails.
264+
if tree1.get(value) != tree2.get(value):
265+
log.info("%s verification failed: %s != %s", value, tree1.get(value), tree2.get(value))
266+
ret_value = False
267+
268+
return ret_value
269+
270+
271+
def compare_DT(dirTree1, dirTree2, values=None, log=None):
221272
"""
222273
to compare two DirTrees, and report which files have changed.
223274
returns a tuple of 4 lists of paths: same files, different files,
@@ -236,7 +287,7 @@ def compare_DT(dirTree1, dirTree2):
236287
# path is in the 2 DT, we have to compare file info
237288
f1 = dirTree1.dict[p]
238289
f2 = dirTree2.dict[p]
239-
if compare_files(f1, f2):
290+
if comp_filesv2(f1, f2, values, log):
240291
# files/dirs are the same
241292
same.append(p)
242293
else:

0 commit comments

Comments
 (0)