wscript (4833B)
1 #! /usr/bin/env python3.1 2 3 import os, shutil 4 from waflib import Node, Build, Utils, Logs 5 6 def exists(path): 7 try: 8 os.stat(path) 9 except: 10 return 'no' 11 else: 12 return 'yes' 13 14 def remove(path): 15 try: 16 try: 17 os.listdir(path) 18 except OSError: 19 os.remove(path) 20 else: 21 shutil.rmtree(path) 22 except: 23 pass 24 25 def create(path): 26 try: 27 os.makedirs(path) 28 except: 29 os.listdir(path) 30 31 def configure(ctx): 32 pass 33 34 def test(ctx): 35 bld = Build.BuildContext() 36 37 errors = [] 38 def tt(msg, result, expected): 39 color = 'RED' 40 if result == expected: 41 color = 'GREEN' 42 else: 43 errors.append(result) 44 Logs.pprint(color, msg.ljust(20) + " %r" % result) 45 46 47 # 1. absdir is wrong, keep the drive letter 48 # 2. split should use os.sep 49 # 3. replace / in d1 from d2 50 # 4. use os.sep in find_node 51 absdir = os.getcwd().split(os.sep) 52 53 dd = bld.root.make_node(absdir) 54 pp = dd.parent 55 56 57 tt('dir printed', repr(dd), os.getcwd()) 58 tt('parent', repr(pp), os.path.split(os.getcwd())[0]) 59 tt('path_from', dd.path_from(pp), os.path.split(os.getcwd())[1]) 60 tt('path_from (reverse)', pp.path_from(dd), '..') 61 tt('same path', pp.path_from(pp), '.') 62 tt('path from root is abspath()', pp.path_from(bld.root), pp.abspath()) 63 tt('root from root', bld.root.path_from(bld.root), bld.root.abspath()) 64 65 tt('root height', bld.root.height(), 0) 66 tt('self height', dd.height(), len(absdir)) 67 68 d1 = dd.make_node(['a', 'b']) 69 d2 = dd.make_node(['c', 'd']) 70 tt('compare height', d1.height() - pp.height(), 3) 71 72 tt('d1 from d2', d1.path_from(d2), '../../a/b'.replace('/', os.sep)) 73 tt('d2 from d1', d2.path_from(d1), '../../c/d'.replace('/', os.sep)) 74 75 76 d1.parent.delete() 77 78 tt('d1.parent exists', exists(d1.parent.abspath()), 'no') 79 tt('d1 exists', exists(d1.abspath()), 'no') 80 81 d1.parent.mkdir() 82 d1.parent.mkdir() 83 84 tt('d1.parent exists', exists(d1.parent.abspath()), 'yes') 85 tt('d1 exists', exists(d1.abspath()), 'no') 86 87 88 d1.mkdir() 89 kp = d1.make_node(['ah-ha']) 90 ini = "this is a test" 91 kp.write(ini) 92 kp.chmod(493) 93 fin = kp.read() 94 95 tt('read and write text', fin, ini) 96 97 rama = ['1234', '5', '6', '7'] 98 remove('1234') 99 create('/'.join(rama)) 100 rr = dd.find_node(rama) 101 tt('find a node', repr(rr), os.sep.join([os.getcwd()]+rama)) 102 103 remove('src/build') 104 create('src/build') 105 106 ss = dd.find_node(['src']) 107 bb = dd.find_node(['src', 'build']) 108 bld.top_dir = ss.abspath() 109 bld.out_dir = bb.abspath() 110 bld.init_dirs() 111 112 #remove(dd.abspath() + '/' +"xyz") 113 tt('find ["xyz"]', dd.find_node(['xyz']), None) 114 115 tt('bld.srcnode is src', bld.srcnode.is_src(), True) 116 tt('bld.srcnode is bld', bld.srcnode.is_bld(), False) 117 tt('bld.bldnode is src', bld.bldnode.is_src(), False) 118 tt('bld.bldnode is bld', bld.bldnode.is_bld(), True) 119 tt('bld.root is bld', bld.root.is_bld(), False) 120 tt('bld.root is src', bld.root.is_src(), False) 121 nf = bld.srcnode.make_node('abc') 122 nf.write("aha") 123 nf.get_bld_sig() 124 tt('find_resource src/abc', bld.srcnode.find_resource(['abc']), nf) 125 tt('find_or_declare src/abc', bld.srcnode.find_or_declare(['abc']), bld.bldnode.make_node(['abc'])) 126 tt('src.get_bld()', bld.srcnode.get_bld(), bld.bldnode) 127 tt('bld.get_src()', bld.bldnode.get_src(), bld.srcnode) 128 129 stupid_build = bld.bldnode.make_node(['abc']) 130 stupid_build.write("heheh") 131 tt('find_or_declare src/abc', bld.srcnode.find_or_declare(['abc']), stupid_build) 132 tt('find_resource src/abc', bld.srcnode.find_resource(['abc']), stupid_build) 133 134 bld = Build.BuildContext() 135 bld.top_dir = ss.abspath() 136 bld.out_dir = bb.abspath() 137 bld.init_dirs() 138 139 create('src/a.txt') 140 create('src/b.txt') 141 nd = bld.srcnode.make_node('c.txt') 142 nd.write("test") 143 create('d.TXT') 144 nd2 = bld.srcnode.make_node('d.TXT') 145 nd2.write("test") 146 nd3 = bld.srcnode.make_node('e.e+(e).txt') 147 nd3.write("test") 148 149 tt("ant_glob ->", len(bld.srcnode.ant_glob('*.txt', flat=False)), 2) 150 tt("ant_glob (icase) ->", len(bld.srcnode.ant_glob('*.txt', flat=False, ignorecase=True)), 3) 151 tt("ant_glob (parentheses) ->", len(bld.srcnode.ant_glob('e.e+[(]e[)].txt', flat=False)), 1) 152 #print("ant_glob src ->", bld.srcnode.ant_glob('*.txt')) 153 154 def abspath(self): 155 try: 156 return self.cache_abspath 157 except AttributeError: 158 pass 159 if not self.parent: 160 val = '' 161 elif not self.parent.name: 162 val = self.name + '\\' 163 else: 164 val = self.parent.abspath().rstrip('\\') + '\\' + self.name 165 self.cache_abspath = val 166 return val 167 168 # the local class will be unused soon enough 169 old_abspath = bld.node_class.abspath 170 bld.node_class.abspath = abspath 171 172 unc1 = '\\\\computer\\share\\file' 173 lst = Utils.split_path_win32(unc1) 174 node = bld.root.make_node(lst) 175 tt('UNC head node', lst[0], '\\\\computer') 176 tt('UNC share path', node.abspath(), unc1) 177 178 unc2 = '\\\\?\\C:\\foo' 179 lst = Utils.split_path_win32(unc2) 180 node = bld.root.make_node(lst) 181 tt('UNC long path', node.abspath(), 'C:\\foo') 182 183 if errors: 184 bld.fatal('There are test failures ^^') 185