Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 1 | # |
| 2 | # Interactively decide what to distribute |
| 3 | # |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 4 | # |
| 5 | # The exclude file signals files to always exclude, |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 6 | # The pattern file lines are of the form |
| 7 | # *.c |
| 8 | # This excludes all files ending in .c. |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 9 | # |
| 10 | # The include file signals files and directories to include. |
| 11 | # Records are of the form |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 12 | # ('Tools:bgen:AE:AppleEvents.py', 'Lib:MacToolbox:AppleEvents.py') |
| 13 | # This includes the specified file, putting it in the given place, or |
| 14 | # ('Tools:bgen:AE:AppleEvents.py', None) |
| 15 | # This excludes the specified file. |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 16 | # |
| 17 | from MkDistr_ui import * |
| 18 | import fnmatch |
| 19 | import regex |
| 20 | import os |
| 21 | import sys |
| 22 | import macfs |
| 23 | import macostools |
| 24 | |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 25 | DEBUG=0 |
| 26 | |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 27 | SyntaxError='Include/exclude file syntax error' |
| 28 | |
| 29 | class Matcher: |
| 30 | """Include/exclude database, common code""" |
| 31 | |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 32 | def __init__(self, filename): |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 33 | self.filename = filename |
| 34 | self.rawdata = [] |
| 35 | self.parse(filename) |
| 36 | self.rawdata.sort() |
| 37 | self.rebuild() |
| 38 | self.modified = 0 |
| 39 | |
| 40 | def parse(self, dbfile): |
| 41 | try: |
| 42 | fp = open(dbfile) |
| 43 | except IOError: |
| 44 | return |
| 45 | data = fp.readlines() |
| 46 | fp.close() |
| 47 | for d in data: |
| 48 | d = d[:-1] |
| 49 | if not d or d[0] == '#': continue |
| 50 | pat = self.parseline(d) |
| 51 | self.rawdata.append(pat) |
| 52 | |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 53 | def save(self): |
| 54 | fp = open(self.filename, 'w') |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 55 | self.savedata(fp, self.rawdata) |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 56 | self.modified = 0 |
| 57 | |
| 58 | def add(self, value): |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 59 | if len(value) == 1: |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 60 | value = value + ('',) |
| 61 | self.rawdata.append(value) |
| 62 | self.rebuild1(value) |
| 63 | self.modified = 1 |
| 64 | |
| 65 | def delete(self, value): |
| 66 | key = value |
| 67 | for i in range(len(self.rawdata)): |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 68 | if self.rawdata[i][0] == key: |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 69 | del self.rawdata[i] |
| 70 | self.unrebuild1(i, key) |
| 71 | self.modified = 1 |
| 72 | return |
| 73 | print 'Not found!', key |
| 74 | |
| 75 | def getall(self): |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 76 | return map(lambda x: x[0], self.rawdata) |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 77 | |
| 78 | def get(self, value): |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 79 | for src, dst in self.rawdata: |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 80 | if src == value: |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 81 | return src, dst |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 82 | print 'Not found!', value |
| 83 | |
| 84 | def is_modified(self): |
| 85 | return self.modified |
| 86 | |
| 87 | class IncMatcher(Matcher): |
| 88 | """Include filename database and matching engine""" |
| 89 | |
| 90 | def rebuild(self): |
| 91 | self.idict = {} |
| 92 | self.edict = {} |
| 93 | for v in self.rawdata: |
| 94 | self.rebuild1(v) |
| 95 | |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 96 | def parseline(self, line): |
| 97 | try: |
| 98 | data = eval(line) |
| 99 | except: |
| 100 | raise SyntaxError, line |
| 101 | if type(data) <> type(()) or len(data) not in (1,2): |
| 102 | raise SyntaxError, line |
| 103 | if len(data) == 1: |
| 104 | data = data + ('',) |
| 105 | return data |
| 106 | |
| 107 | def savedata(self, fp, data): |
| 108 | for d in self.rawdata: |
| 109 | fp.write(`d`+'\n') |
| 110 | |
| 111 | def rebuild1(self, (src, dst)): |
| 112 | if dst == '': |
| 113 | dst = src |
| 114 | if dst == None: |
| 115 | self.edict[src] = None |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 116 | else: |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 117 | self.idict[src] = dst |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 118 | |
| 119 | def unrebuild1(self, num, src): |
| 120 | if self.idict.has_key(src): |
| 121 | del self.idict[src] |
| 122 | else: |
| 123 | del self.edict[src] |
| 124 | |
| 125 | def match(self, patharg): |
| 126 | removed = [] |
| 127 | # First check the include directory |
| 128 | path = patharg |
| 129 | while 1: |
| 130 | if self.idict.has_key(path): |
| 131 | # We know of this path (or initial piece of path) |
| 132 | dstpath = self.idict[path] |
| 133 | # We do want it distributed. Tack on the tail. |
| 134 | while removed: |
| 135 | dstpath = os.path.join(dstpath, removed[0]) |
| 136 | removed = removed[1:] |
| 137 | # Finally, if the resultant string ends in a separator |
| 138 | # tack on our input filename |
| 139 | if dstpath[-1] == os.sep: |
| 140 | dir, file = os.path.split(path) |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 141 | dstpath = os.path.join(dstpath, file) |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 142 | if DEBUG: |
| 143 | print 'include', patharg, dstpath |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 144 | return dstpath |
| 145 | path, lastcomp = os.path.split(path) |
| 146 | if not path: |
| 147 | break |
| 148 | removed[0:0] = [lastcomp] |
| 149 | # Next check the exclude directory |
| 150 | path = patharg |
| 151 | while 1: |
| 152 | if self.edict.has_key(path): |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 153 | if DEBUG: |
| 154 | print 'exclude', patharg, path |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 155 | return '' |
| 156 | path, lastcomp = os.path.split(path) |
| 157 | if not path: |
| 158 | break |
| 159 | removed[0:0] = [lastcomp] |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 160 | if DEBUG: |
| 161 | print 'nomatch', patharg |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 162 | return None |
| 163 | |
| 164 | def checksourcetree(self): |
| 165 | rv = [] |
| 166 | for name in self.idict.keys(): |
| 167 | if not os.path.exists(name): |
| 168 | rv.append(name) |
| 169 | return rv |
| 170 | |
| 171 | class ExcMatcher(Matcher): |
| 172 | """Exclude pattern database and matching engine""" |
| 173 | |
| 174 | def rebuild(self): |
| 175 | self.relist = [] |
| 176 | for v in self.rawdata: |
| 177 | self.rebuild1(v) |
| 178 | |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 179 | def parseline(self, data): |
| 180 | return (data, None) |
| 181 | |
| 182 | def savedata(self, fp, data): |
| 183 | for d in self.rawdata: |
| 184 | fp.write(d[0]+'\n') |
| 185 | |
| 186 | def rebuild1(self, (src, dst)): |
| 187 | pat = fnmatch.translate(src) |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 188 | if DEBUG: |
| 189 | print 'PATTERN', `src`, 'REGEX', `pat` |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 190 | self.relist.append(regex.compile(pat)) |
| 191 | |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 192 | def unrebuild1(self, num, src): |
| 193 | del self.relist[num] |
| 194 | |
| 195 | def match(self, path): |
| 196 | comps = os.path.split(path) |
| 197 | file = comps[-1] |
| 198 | for pat in self.relist: |
| 199 | if pat and pat.match(file) == len(file): |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 200 | if DEBUG: |
| 201 | print 'excmatch', file, pat |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 202 | return 1 |
| 203 | return 0 |
| 204 | |
| 205 | |
| 206 | class Main: |
| 207 | """The main program glueing it all together""" |
| 208 | |
| 209 | def __init__(self): |
| 210 | InitUI() |
| 211 | fss, ok = macfs.GetDirectory('Source directory:') |
| 212 | if not ok: |
| 213 | sys.exit(0) |
| 214 | os.chdir(fss.as_pathname()) |
Jack Jansen | 230ad2c | 1996-10-23 15:52:56 +0000 | [diff] [blame] | 215 | if not os.path.isdir(':Mac:Distributions'): |
| 216 | os.mkdir(':Mac:Distributions') |
Jack Jansen | 8628897 | 1996-08-28 14:18:58 +0000 | [diff] [blame] | 217 | typedist = GetType() |
Jack Jansen | 230ad2c | 1996-10-23 15:52:56 +0000 | [diff] [blame] | 218 | self.inc = IncMatcher(':Mac:Distributions:%s.include'%typedist) |
| 219 | self.exc = ExcMatcher(':Mac:Distributions:%s.exclude'%typedist) |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 220 | self.ui = MkDistrUI(self) |
| 221 | self.ui.mainloop() |
| 222 | |
| 223 | def check(self): |
| 224 | return self.checkdir(':', 1) |
| 225 | |
| 226 | def checkdir(self, path, istop): |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 227 | if DEBUG: |
| 228 | print 'checkdir', path |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 229 | files = os.listdir(path) |
| 230 | rv = [] |
| 231 | todo = [] |
| 232 | for f in files: |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 233 | if DEBUG: |
| 234 | print 'checkfile', f |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 235 | if self.exc.match(f): |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 236 | if DEBUG: |
| 237 | print 'exclude match', f |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 238 | continue |
| 239 | fullname = os.path.join(path, f) |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 240 | if DEBUG: |
| 241 | print 'checkpath', fullname |
| 242 | matchvalue = self.inc.match(fullname) |
| 243 | if matchvalue == None: |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 244 | if os.path.isdir(fullname): |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 245 | if DEBUG: |
| 246 | print 'do dir', fullname |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 247 | todo.append(fullname) |
| 248 | else: |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 249 | if DEBUG: |
| 250 | print 'include', fullname |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 251 | rv.append(fullname) |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 252 | elif DEBUG: |
| 253 | print 'badmatch', matchvalue |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 254 | for d in todo: |
Jack Jansen | 6f69c50 | 1997-05-23 15:41:15 +0000 | [diff] [blame] | 255 | if len(rv) > 500: |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 256 | if istop: |
| 257 | rv.append('... and more ...') |
| 258 | return rv |
| 259 | rv = rv + self.checkdir(d, 0) |
| 260 | return rv |
| 261 | |
| 262 | def run(self, destprefix): |
| 263 | missing = self.inc.checksourcetree() |
| 264 | if missing: |
| 265 | print '==== Missing source files ====' |
| 266 | for i in missing: |
| 267 | print i |
| 268 | print '==== Fix and retry ====' |
| 269 | return |
| 270 | if not self.rundir(':', destprefix, 0): |
| 271 | return |
| 272 | self.rundir(':', destprefix, 1) |
| 273 | |
| 274 | def rundir(self, path, destprefix, doit): |
| 275 | files = os.listdir(path) |
| 276 | todo = [] |
| 277 | rv = 1 |
| 278 | for f in files: |
| 279 | if self.exc.match(f): |
| 280 | continue |
| 281 | fullname = os.path.join(path, f) |
| 282 | if os.path.isdir(fullname): |
| 283 | todo.append(fullname) |
| 284 | else: |
| 285 | dest = self.inc.match(fullname) |
| 286 | if dest == None: |
| 287 | print 'Not yet resolved:', fullname |
| 288 | rv = 0 |
| 289 | if dest: |
| 290 | if doit: |
| 291 | print 'COPY ', fullname |
| 292 | print ' -> ', os.path.join(destprefix, dest) |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 293 | try: |
| 294 | macostools.copy(fullname, os.path.join(destprefix, dest), 1) |
| 295 | except: #DBG |
Jack Jansen | 804fdc8 | 1998-04-27 15:12:13 +0000 | [diff] [blame] | 296 | print 'cwd', os.getcwd() #DBG |
Jack Jansen | 73c804a | 1998-02-20 16:08:19 +0000 | [diff] [blame] | 297 | print 'fsspec', macfs.FSSpec(fullname) #DBG |
Jack Jansen | 804fdc8 | 1998-04-27 15:12:13 +0000 | [diff] [blame] | 298 | raise |
Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 299 | for d in todo: |
| 300 | if not self.rundir(d, destprefix, doit): |
| 301 | rv = 0 |
| 302 | return rv |
| 303 | |
| 304 | def save(self): |
| 305 | self.inc.save() |
| 306 | self.exc.save() |
| 307 | |
| 308 | def is_modified(self): |
| 309 | return self.inc.is_modified() or self.exc.is_modified() |
| 310 | |
| 311 | if __name__ == '__main__': |
| 312 | Main() |
| 313 | |