From: stevenknight Date: Tue, 11 Dec 2001 02:32:21 +0000 (+0000) Subject: Add a Node.FS.__cmp__() method, fix the Node.FS.Entry.__cmp__() method, and add a... X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=e069ede0ff39ae884a238fc7ed10ce9241c06e34;p=scons.git Add a Node.FS.__cmp__() method, fix the Node.FS.Entry.__cmp__() method, and add a Node.FS.Entry.__hash__() method. git-svn-id: http://scons.tigris.org/svn/scons/trunk@135 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/src/engine/SCons/Node/FS.py b/src/engine/SCons/Node/FS.py index 67a5ae72..a898f01b 100644 --- a/src/engine/SCons/Node/FS.py +++ b/src/engine/SCons/Node/FS.py @@ -122,6 +122,9 @@ class FS: self.Top.path_ = os.path.join('.', '') self.cwd = self.Top + def __cmp__(self, other): + return cmp(self.__dict__, other.__dict__) + def __doLookup(self, fsclass, name, directory=None): """This method differs from the File and Dir factory methods in one important way: the meaning of the directory parameter. @@ -274,6 +277,18 @@ class Entry(SCons.Node.Node): """A FS node's string representation is its path name.""" return self.path + def __cmp__(self, other): + if type(self) != types.StringType and type(other) != types.StringType: + try: + if self.__class__ != other.__class__: + return 1 + except: + return 1 + return cmp(str(self), str(other)) + + def __hash__(self): + return hash(self.abspath_) + def exists(self): return os.path.exists(self.abspath) diff --git a/src/engine/SCons/Node/FSTests.py b/src/engine/SCons/Node/FSTests.py index 49920990..d9d128e9 100644 --- a/src/engine/SCons/Node/FSTests.py +++ b/src/engine/SCons/Node/FSTests.py @@ -307,6 +307,57 @@ class FSTestCase(unittest.TestCase): assert not f1.dir.exists() f1.build() assert f1.dir.exists() + + # Test comparison of FS objects + fs1 = SCons.Node.FS.FS() + fs2 = SCons.Node.FS.FS() + os.chdir('..') + fs3 = SCons.Node.FS.FS() + assert fs1 == fs2 + assert fs1 == fs3 + + # Test comparison of Entry objects + e1 = fs3.Entry('cmp/entry') + e2 = fs3.Entry('cmp/../cmp/entry') + e3 = fs3.Entry('entry') + assert e1 == e2 + assert e1 != e3 + assert e1 == os.path.normpath("cmp/entry"), e1 + assert e1 != os.path.normpath("c/entry"), e1 + + # Test comparison of Dir objects + d1 = fs3.Dir('cmp/dir') + d2 = fs3.Dir('cmp/../cmp/dir') + d3 = fs3.Dir('dir') + assert d1 == d2 + assert d1 != d3 + assert d1 == os.path.normpath("cmp/dir"), d1 + assert d1 != os.path.normpath("c/dir"), d1 + + # Test comparison of File objects + f1 = fs3.File('cmp/file') + f2 = fs3.File('cmp/../cmp/file') + f3 = fs3.File('file') + assert f1 == f2 + assert f1 != f3 + assert f1 == os.path.normpath("cmp/file"), f1 + assert f1 != os.path.normpath("c/file"), f1 + + # Test comparison of different type objects + f1 = fs1.File('cmp/xxx') + d2 = fs2.Dir('cmp/xxx') + assert f1 != d2, "%s == %s" % (f1.__class__, d2.__class__) + + # Test hashing FS nodes + f = fs1.File('hash/f') + d = fs1.Dir('hash/d') + e = fs1.Entry('hash/e') + val = {} + val[f] = 'f' + val[d] = 'd' + val[e] = 'e' + for k, v in val.items(): + assert k == "hash/" + v #XXX test exists()