4 Build a fake directory tree for testing rsync management code.
10 def make_random_string(length=8):
11 """Make a random string, length characters long
13 symbols = "abcdefhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
15 for i in xrange(length):
16 name.append(random.choice(symbols))
19 def make_file(pathname):
20 """Make a file with some random stuff in it
22 stream = open(pathname,'w')
23 stream.write(make_random_string(16))
26 def make_tree(root, depth=3, directories=5, files=10):
28 Make a tree of random directories and files
30 depth is how many levels of subdirectories
31 directories is how many directories each subdirectory should have
32 files is how many files to create in each directory
34 if not os.path.exists(root):
39 for i in range(files):
40 name = make_random_string()
42 pathname = os.path.join(root, name)
45 # make subdirectories if we still have some depth to go
47 for i in range(directories):
48 name = make_random_string()
50 pathname = os.path.join(root, name)
51 subpaths = make_tree(pathname, depth-1, directories, files)
52 paths.extend([ os.path.join(name, x) for x in subpaths ])
56 def generate_paths(root):
57 """Make a list of relative paths like generated by make_tree
60 for curdir, subdirs, files in os.walk(root):
61 paths.extend([ os.path.join(curdir, f) for f in files ])
63 # an inefficient way of getting the correct common prefix
64 # (e.g. root might not have a trailing /)
65 common_root = os.path.commonprefix(paths)
66 common_len = len(common_root)
67 return [ p[common_len:] for p in paths ]
69 def compare_tree(root, paths, verbose=False):
70 """Make sure the tree matches our relative list of paths
72 # what we find when we look
73 experimental_set = set(generate_paths(root))
75 theoretical_set = set(paths)
76 # true if the difference of the two sets is the empty set
77 difference = experimental_set - theoretical_set
78 issame = (len(difference) == 0)
79 if verbose and not issame: