blob: ddb74ab1b6433de461c05ee79c406cc6a48517dd [file] [log] [blame]
Fred Drakec7b5b3c1999-10-29 17:23:15 +00001\section{\module{filecmp} ---
Fred Drake353aaad2000-07-03 08:24:49 +00002 File and Directory Comparisons}
Fred Drakec7b5b3c1999-10-29 17:23:15 +00003
4\declaremodule{standard}{filecmp}
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drakec7b5b3c1999-10-29 17:23:15 +00006\modulesynopsis{Compare files efficiently.}
7
Fred Drake353aaad2000-07-03 08:24:49 +00008
Fred Drakeba7e2c82001-05-11 17:01:32 +00009The \module{filecmp} module defines functions to compare files and
10directories, with various optional time/correctness trade-offs.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000011
Fred Drakeba7e2c82001-05-11 17:01:32 +000012The \module{filecmp} module defines the following functions:
Fred Drakec7b5b3c1999-10-29 17:23:15 +000013
14\begin{funcdesc}{cmp}{f1, f2\optional{, shallow\optional{, use_statcache}}}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000015Compare the files named \var{f1} and \var{f2}, returning \code{True} if
16they seem equal, \code{False} otherwise.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000017
18Unless \var{shallow} is given and is false, files with identical
Fred Drake353aaad2000-07-03 08:24:49 +000019\function{os.stat()} signatures are taken to be equal. If
Fred Drakec7b5b3c1999-10-29 17:23:15 +000020\var{use_statcache} is given and is true,
Fred Drake353aaad2000-07-03 08:24:49 +000021\function{statcache.stat()} will be called rather then
22\function{os.stat()}; the default is to use \function{os.stat()}.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000023
24Files that were compared using this function will not be compared again
25unless their \function{os.stat()} signature changes. Note that using
26\var{use_statcache} true will cause the cache invalidation mechanism to
27fail --- the stale stat value will be used from \refmodule{statcache}'s
28cache.
29
Fred Drake353aaad2000-07-03 08:24:49 +000030Note that no external programs are called from this function, giving it
Fred Drakec7b5b3c1999-10-29 17:23:15 +000031portability and efficiency.
32\end{funcdesc}
33
Fred Drake353aaad2000-07-03 08:24:49 +000034\begin{funcdesc}{cmpfiles}{dir1, dir2, common\optional{,
35 shallow\optional{, use_statcache}}}
36Returns three lists of file names: \var{match}, \var{mismatch},
37\var{errors}. \var{match} contains the list of files match in both
38directories, \var{mismatch} includes the names of those that don't,
39and \var{errros} lists the names of files which could not be
40compared. Files may be listed in \var{errors} because the user may
41lack permission to read them or many other reasons, but always that
42the comparison could not be done for some reason.
43
Neal Norwitz1abca4a2002-03-20 18:55:09 +000044The \var{common} parameter is a list of file names found in both directories.
Fred Drake353aaad2000-07-03 08:24:49 +000045The \var{shallow} and \var{use_statcache} parameters have the same
46meanings and default values as for \function{filecmp.cmp()}.
47\end{funcdesc}
48
Fred Drakec7b5b3c1999-10-29 17:23:15 +000049Example:
50
51\begin{verbatim}
52>>> import filecmp
53>>> filecmp.cmp('libundoc.tex', 'libundoc.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000054True
Fred Drakec7b5b3c1999-10-29 17:23:15 +000055>>> filecmp.cmp('libundoc.tex', 'lib.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000056False
Fred Drakec7b5b3c1999-10-29 17:23:15 +000057\end{verbatim}
Fred Drake353aaad2000-07-03 08:24:49 +000058
59
60\subsection{The \protect\class{dircmp} class \label{dircmp-objects}}
61
Fred Drakeba7e2c82001-05-11 17:01:32 +000062\class{dircmp} instances are built using this constructor:
63
Fred Drake353aaad2000-07-03 08:24:49 +000064\begin{classdesc}{dircmp}{a, b\optional{, ignore\optional{, hide}}}
65Construct a new directory comparison object, to compare the
66directories \var{a} and \var{b}. \var{ignore} is a list of names to
67ignore, and defaults to \code{['RCS', 'CVS', 'tags']}. \var{hide} is a
Skip Montanaro9483bed2001-01-18 10:44:08 +000068list of names to hide, and defaults to \code{[os.curdir, os.pardir]}.
Fred Drake353aaad2000-07-03 08:24:49 +000069\end{classdesc}
70
Fred Drakeba7e2c82001-05-11 17:01:32 +000071The \class{dircmp} class provides the following methods:
72
Fred Drake353aaad2000-07-03 08:24:49 +000073\begin{methoddesc}[dircmp]{report}{}
74Print (to \code{sys.stdout}) a comparison between \var{a} and \var{b}.
75\end{methoddesc}
76
77\begin{methoddesc}[dircmp]{report_partial_closure}{}
78Print a comparison between \var{a} and \var{b} and common immediate
79subdirctories.
80\end{methoddesc}
81
82\begin{methoddesc}[dircmp]{report_full_closure}{}
83Print a comparison between \var{a} and \var{b} and common
84subdirctories (recursively).
85\end{methoddesc}
86
Fred Drakeba7e2c82001-05-11 17:01:32 +000087
88The \class{dircmp} offers a number of interesting attributes that may
89be used to get various bits of information about the directory trees
90being compared.
91
92Note that via \method{__getattr__()} hooks, all attributes are
93computed lazilly, so there is no speed penalty if only those
94attributes which are lightweight to compute are used.
95
Fred Drake353aaad2000-07-03 08:24:49 +000096\begin{memberdesc}[dircmp]{left_list}
97Files and subdirectories in \var{a}, filtered by \var{hide} and
98\var{ignore}.
99\end{memberdesc}
100
101\begin{memberdesc}[dircmp]{right_list}
102Files and subdirectories in \var{b}, filtered by \var{hide} and
103\var{ignore}.
104\end{memberdesc}
105
106\begin{memberdesc}[dircmp]{common}
107Files and subdirectories in both \var{a} and \var{b}.
108\end{memberdesc}
109
110\begin{memberdesc}[dircmp]{left_only}
111Files and subdirectories only in \var{a}.
112\end{memberdesc}
113
114\begin{memberdesc}[dircmp]{right_only}
115Files and subdirectories only in \var{b}.
116\end{memberdesc}
117
118\begin{memberdesc}[dircmp]{common_dirs}
119Subdirectories in both \var{a} and \var{b}.
120\end{memberdesc}
121
122\begin{memberdesc}[dircmp]{common_files}
123Files in both \var{a} and \var{b}
124\end{memberdesc}
125
126\begin{memberdesc}[dircmp]{common_funny}
127Names in both \var{a} and \var{b}, such that the type differs between
128the directories, or names for which \function{os.stat()} reports an
129error.
130\end{memberdesc}
131
132\begin{memberdesc}[dircmp]{same_files}
133Files which are identical in both \var{a} and \var{b}.
134\end{memberdesc}
135
136\begin{memberdesc}[dircmp]{diff_files}
137Files which are in both \var{a} and \var{b}, whose contents differ.
138\end{memberdesc}
139
140\begin{memberdesc}[dircmp]{funny_files}
141Files which are in both \var{a} and \var{b}, but could not be
142compared.
143\end{memberdesc}
144
145\begin{memberdesc}[dircmp]{subdirs}
146A dictionary mapping names in \member{common_dirs} to
147\class{dircmp} objects.
148\end{memberdesc}