Created
January 6, 2010 01:16
-
-
Save scooby/269914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# vim: shiftwidth=4 expandtab ft=python | |
# Written by Ben Samuel, 2010. Released into public domain. | |
# Use at your own risk. | |
import os, sys, stat | |
import os.path as path | |
def statOrNone(ent): | |
try: | |
return os.lstat(ent) | |
except: | |
return None | |
def tryListing(path): | |
try: | |
return os.listdir(path) | |
except: | |
return [] | |
queue = [tuple(sys.argv[1:3])] | |
while queue: | |
(this, that) = queue.pop(0) | |
(this_stat, that_stat) = map(statOrNone, (this, that)) | |
if this_stat is None and that_stat is None: | |
print "Same: %s and %s are missing" % (this, that) | |
continue | |
if this_stat is None and that_stat is not None: | |
print "That: %s is missing, but %s exists" % (this, that) | |
continue | |
if this_stat is not None and that_stat is None: | |
print "This: %s exists, but %s is missing" % (this, that) | |
continue | |
if this_stat[stat.ST_INO] == that_stat[stat.ST_INO] \ | |
and this_stat[stat.ST_DEV] == that_stat[stat.ST_DEV]: | |
continue | |
(this_mode, that_mode) = (this_stat[stat.ST_MODE], that_stat[stat.ST_MODE]) | |
(this_dir, that_dir) = map(stat.S_ISDIR, (this_mode, that_mode)) | |
if this_dir and that_dir: | |
# Reading it backwards: | |
# Read in the directory listings of this and that, join them together | |
# Step through the combined listing (for ent in) | |
# With each item, make a pair object of this/ent and that/ent | |
# Create a set of those entries to remove duplicates | |
# Extend the queue with the contents of that set | |
queue.extend(set(((path.join(this, ent), path.join(that, ent)) | |
for ent in tryListing(this) + tryListing(that)))) | |
continue | |
if this_dir != that_dir: | |
print "%s is %sa directory but %s is %sa directory" % ( | |
this, '' if this_dir else 'not', | |
that, '' if that_dir else 'not') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment