class TestFileWalker(unittest.TestCase): def setUp(self): self._files = FilesCollection( "fc" ) def tearDown(self): self._files.dropDB() def testEmptyDir(self): d = randomutils.RandomDir() filewalker.processFiles( self._files, d()) self.assertTrue( self._files.inFilesCollection(d())) self.assertEqual( 1, self._files.count()) d.rmdir() def testOneFileDir(self): # debug.debugon() # debug.traceon() d = randomutils.RandomDir() f = randomutils.RandomFile(d()) filewalker.processFiles( self._files, d()) self.assertTrue( self._files.inFilesCollection( f())) self.assertEqual( 2, self._files.count()) f.rm() d.rmdir() def testTree(self): root = randomutils.RandomDir() t = randomtree.RandomTree( root(), dirsPerDir=0, depth=1 ) filewalker.processFiles( self._files, t.rootDir()) # print "root : %s" % root() # print "Tree Files : %s" % sorted( [ x for x in t.allPaths() ] ) # print "Db Files : %s" % sorted( [ x for x in self._files.allPaths()]) self.assertEqual( t.totalCount(), self._files.count()) t.rm()
def main(argv=None): '''Command line options.''' if argv is None: argv = sys.argv else: sys.argv.extend(argv) try: # Setup argument parser parser = ArgumentParser(description="dejooper 0.1beta", formatter_class=RawDescriptionHelpFormatter) parser.add_argument( "-s", "--scan", dest="scandir", help="scan a directory for content") parser.add_argument( "-u", "--dedupe", dest="dedupe", action="store_true", help="produce a list of duplicate files") parser.add_argument( "-d", "--drop", dest="flush", action="store_true", help="drop th existing databases before scanning") parser.add_argument( "-p", "--putone", dest="onefile", help="add a single file to the DB") parser.add_argument( "-w", "--webupload", action="store_true", help="upload files to web db") parser.add_argument( "-g", "--sizegreater", dest="sizegreater", type=int, help="files greater than X bytes in size") parser.add_argument( "-l", "--sizelesser", dest="sizelesser", type=int, help="files less than X bytes in size") parser.add_argument( "-m", "--samename", dest="samename", action="store_true", help="report files with the same name") parser.add_argument( "-x", "--host", dest="serverHost", help="host to upload to") parser.add_argument( "-n", "--dbname", dest="dbname", default="filemetadata", help="which database do we want to use") # Process arguments args = parser.parse_args() serverHost="" if args.serverHost is None : serverHost = "http://localhost:8080/" else: serverHost = args.serverHost scandir = args.scandir dedupe = args.dedupe flush = args.flush oneFile = args.onefile webUpload = args.webupload sizeGreater = args.sizegreater sizeLesser = args.sizelesser sameName = args.samename dbName = args.dbname if sizeGreater is None : sizeGreater = 0 if sizeLesser is None or sizeLesser == 0 : sizeLesser = sys.maxint files = FilesCollection( dbname = dbName ) if flush : files.dropDB() if oneFile: checksum = files.addFile( oneFile ) if webUpload : uploadFile( serverHost, checksum, os.path.abspath( oneFile )) sys.exit(0) if scandir : scanFiles( files, args.scandir, webUpload, serverHost ) if args.sizegreater or args.sizelesser : print "size: g:%i l:%i" % (sizeGreater, sizeLesser ) for (path, size ) in files.sizes( sizeGreater, sizeLesser ): print "%s %i" % (path, size ) if dedupe: findDuplicates( files ) if sameName: for f in files.sameFilenames() : print f except KeyboardInterrupt: ### handle keyboard interrupt ### return 0