def test_new_make_match_re(self): """ Test utility for making matched file name regexes. """ match_re = make_ex_re(None) self.assertIsNotNone(match_re) matches = [] matches.append('foo*') matches.append('*bar') matches.append('junk*') match_re = make_ex_re(matches) self.do_test_for_expected_matches( match_re, ['foo', 'foolish', 'roobar', 'junky']) self.do_test_for_expected_match_failures( match_re, [' foo', 'roobarf', 'myjunk']) matches = ['*.tgz'] match_re = make_ex_re(matches) self.do_test_for_expected_matches( match_re, ['junk.tgz', 'notSoFoolish.tgz']) self.do_test_for_expected_match_failures( match_re, ['junk.tar.gz', 'foolish.tar.gz']) matches = ['*.tgz', '*.tar.gz'] match_re = make_ex_re(matches) self.do_test_for_expected_matches( match_re, ['junk.tgz', 'notSoFoolish.tgz', 'junk.tar.gz', 'ohHello.tar.gz']) self.do_test_for_expected_match_failures( match_re, ['junk.gz', 'foolish.tar'])
def test_new_make_ex_re(self): """ Test utility for making excluded file name regexes. """ # test the null pattern, which should not match anything ex_re = make_ex_re(None) self.assertIsNotNone(ex_re) self.assertIsNone(ex_re.match('bar')) self.assertIsNone(ex_re.match('foo')) exc = [] exc.append('foo*') exc.append('*bar') exc.append('junk*') exc.append('.*.swp') exc.append('.merkle') exc.append('.svn') ex_re = make_ex_re(exc) self.do_test_for_expected_exclusions(ex_re) self.assertIsNotNone(ex_re.match('foobarf')) self.assertIsNone(ex_re.match(' foobarf')) self.assertIsNotNone(ex_re.match('ohMybar')) self.assertIsNone(ex_re.match('ohMybarf')) self.assertIsNotNone(ex_re.match('junky')) self.assertIsNone(ex_re.match(' junk')) # not at beginning
def create_from_file_system(path_to_dir, hashtype=HashTypes.SHA2, exclusions=None, matches=None): """ Create a MerkleDoc based on the information in the directory at pathToDir. The name of the directory will be the last component of pathToDir. Return the MerkleTree. """ check_hashtype(hashtype) if not path_to_dir: raise RuntimeError("cannot create a MerkleTree, no path set") if not os.path.exists(path_to_dir): raise RuntimeError( "MerkleTree: directory '%s' does not exist" % path_to_dir) path, _, _ = path_to_dir.rpartition('/') if path == '': raise RuntimeError("cannot parse inclusive path " + path_to_dir) path += '/' ex_re = None if exclusions: ex_re = util.make_ex_re(exclusions) match_re = None if matches: match_re = util.make_match_re(matches) tree = MerkleTree.create_from_file_system(path_to_dir, hashtype, ex_re, match_re) # creates the hash doc = MerkleDoc(path, hashtype, False, tree, ex_re, match_re) doc.bound = True return doc