mark the example submission rule files as being raw, so the escapes dont get confused
[htsworkflow.git] / test / tree.py
1 #!/usr/bin/env python
2
3 """
4 Build a fake directory tree for testing rsync management code.
5 """
6
7 import os
8 import random
9
10 def make_random_string(length=8):
11   """Make a random string, length characters long
12   """
13   symbols = "abcdefhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
14   name = []
15   for i in xrange(length):
16     name.append(random.choice(symbols))
17   return "".join(name)
18
19 def make_file(pathname):
20   """Make a file with some random stuff in it
21   """
22   stream = open(pathname,'w')
23   stream.write(make_random_string(16))
24   stream.close()
25
26 def make_tree(root, depth=3, directories=5, files=10):
27   """
28   Make a tree of random directories and files
29
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
33   """
34   if not os.path.exists(root):
35     os.mkdir(root)
36
37   paths = []
38   # make files
39   for i in range(files):
40     name = make_random_string()
41     paths.append(name)
42     pathname = os.path.join(root, name)
43     make_file(pathname)
44
45   # make subdirectories if we still have some depth to go
46   if depth > 0:
47     for i in range(directories):
48       name = make_random_string()
49       # paths.append(name)
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 ])
53
54   return paths
55
56 def generate_paths(root):
57   """Make a list of relative paths like generated by make_tree
58   """
59   paths = []
60   for curdir, subdirs, files in os.walk(root):
61     paths.extend([ os.path.join(curdir, f) for f in files ])
62
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 ]
68     
69 def compare_tree(root, paths, verbose=False):
70   """Make sure the tree matches our relative list of paths
71   """
72   # what we find when we look
73   experimental_set = set(generate_paths(root))
74   # what we expect
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:
80     print difference
81   return issame