source: nappy/trunk/nappy/utils/compare.py @ 3466

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/compare.py@3466
Revision 3466, 2.6 KB checked in by astephen, 12 years ago (diff)

New comparison utilities for use in the test suite.

Line 
1#!/usr/bin/env python
2
3"""
4ompare.py
5=========
6
7Tool to compare contents of files or directories full of files.
8
9Usage:
10======
11
12    compare.py <item1> <item2>
13
14Where:
15======
16
17    <item1> and <item2> can either be a text file or directory.
18"""
19
20# Import standard library modules
21import os
22import sys
23import re
24
25file_exclusion_patterns = (".*CSV.*", ".*svn.*", "\..*", ".*\.pyc$", ".*~$") 
26file_exclusions = [re.compile(pattn) for pattn in file_exclusion_patterns]
27dir_exclusion_patterns = (".*CSV.*", ".*svn.*")
28dir_exclusions = [re.compile(pattn) for pattn in dir_exclusion_patterns]
29
30
31def exitNicely(msg):
32    "Tidy exit."
33    print __doc__
34    print msg
35    sys.exit()
36
37
38def compare(i1, i2):
39    """
40    Compares items whether files or directories.
41    Reports any differences at the command line but
42    also returns them in a dictionary as:
43    ???
44    """
45    if os.path.isfile(i1):
46        compFiles(i1, i2)
47    elif os.path.isdir(i1):
48        compDirs(i1, i2)
49    else:
50        exitNicely("Cannot recognise/find item '" + i1 + "'.")
51
52
53def compDirs(d1, d2):
54    """
55    Compares directories by looping through and then calling
56    compFiles() for each pair of files found.
57    """
58    dname = os.path.split(d1)[-1]
59    # Ignore anything that is in exclusion list
60    for excl in dir_exclusions:
61        if excl.match(dname):
62            print "IGNORING EXCLUDED Directory:", d1
63            return
64
65    items = os.listdir(d1)
66   
67    for item in items:
68
69        d1f = os.path.join(d1, item)
70        d2f = os.path.join(d2, item)
71       
72        if not os.path.exists(d2f):
73            print "WARNING: cannot find item:", d2f
74            continue
75
76        if os.path.isdir(d1f): 
77            compDirs(d1f, d2f)
78            continue
79
80        compFiles(d1f, d2f)
81
82
83def compFiles(f1, f2):
84    """
85    Compares contents of two files.
86    """ 
87    name = os.path.split(f1)[-1]
88    # Ignore anything that is in exclusion list
89    for excl in file_exclusions:
90        if excl.match(name):
91            print "IGNORING EXCLUDED file:", f1
92            return
93
94    # Check they exist
95    for f in (f1, f2):
96        if not os.path.isfile(f):
97            exitNicely("CANNOT compare files as item does not exist:" + f)
98           
99
100    l1 = open(f1).readlines()
101    l2 = open(f2).readlines()
102
103    leng = len(l1)
104    if len(l2) < leng: 
105        leng=len(l2)
106
107    print "\n>>>", f1, "\n<<<", f2
108
109    for i in range(leng):
110        if l1[i] != l2[i]:
111            print "Line %s:" % (i+1)
112            print ">>>", l1[i]
113            print "<<<", l2[i]
114
115   
116if __name__=="__main__":
117
118    args = sys.argv[1:]
119    if len(args) != 2:
120       exitNicely("Must provide two items to compare as command-line arguments.")
121
122    compare(args[0], args[1])
Note: See TracBrowser for help on using the repository browser.