Just van Rossum | d0f0637 | 1999-01-24 23:24:21 +0000 | [diff] [blame] | 1 | # fixgusidir - Modify filenames in the CWGUSI source tree, so |
| 2 | # that it can be put under CVS. Needed because there are files with slashes |
| 3 | # in their name, which CVS does not approve of. |
| 4 | # |
| 5 | # Usage: |
| 6 | # - On importing gusi in cvs: |
| 7 | # - Run the script after unpacking the gusi distribution. This creates |
| 8 | # _s_ files for all / files. |
| 9 | # - Remove all / files with "find file" or some such. |
| 10 | # - import the tree |
| 11 | # - On checking out gusi: |
| 12 | # - After checkout, run the script to create / files for all _s_ files. |
| 13 | # - After modifying stuff, or later checkouts: |
| 14 | # - Run the script. Conflicts between / and _s_ files will be reported. |
| 15 | # - Fix the problems by removing the outdated / or _s_ file. |
| 16 | # - Run the script again. Possibly do a cvs checkin. |
| 17 | import macfs |
| 18 | import os |
| 19 | import sys |
| 20 | import re |
| 21 | |
| 22 | # Substitution for slashes in filenames |
| 23 | SUBST = '_s_' |
| 24 | # Program to find those substitutions |
| 25 | SUBSTPROG = re.compile(SUBST) |
| 26 | |
| 27 | def main(): |
| 28 | if len(sys.argv) > 1: |
| 29 | gusidir = sys.argv[1] |
| 30 | else: |
| 31 | fss, ok = macfs.GetDirectory("CWGUSI source folder?") |
| 32 | if not ok: sys.exit(0) |
| 33 | gusidir = fss.as_pathname() |
| 34 | fixgusifolder(gusidir) |
| 35 | sys.exit(1) |
| 36 | |
| 37 | def fixgusifolder(gusidir): |
| 38 | """Synchronize files with / in their name with their _s_ counterparts""" |
| 39 | os.path.walk(gusidir, gusiwalk, None) |
| 40 | |
| 41 | def gusiwalk(dummy, top, names): |
| 42 | """Helper for fixgusifolder: convert a single directory full of files""" |
| 43 | # First remember names with slashes and with our slash-substitution |
| 44 | macnames = [] |
| 45 | codenames = [] |
| 46 | for name in names: |
| 47 | if '/' in name: |
| 48 | macnames.append(name) |
| 49 | if SUBSTPROG.search(name): |
| 50 | codenames.append(name) |
| 51 | # Next, check whether we need to copy any slash-files to subst-files |
| 52 | for name in macnames: |
| 53 | if os.path.isdir(name): |
| 54 | print '** Folder with slash in name cannot be handled!' |
| 55 | sys.exit(1) |
| 56 | othername = mac2codename(name) |
| 57 | if len(othername) > 31: |
| 58 | print '** Converted filename too long:', othername |
| 59 | sys.exit(1) |
| 60 | if othername in codenames: |
| 61 | codenames.remove(othername) |
| 62 | sync(os.path.join(top, name), os.path.join(top, othername)) |
| 63 | # Now codenames contains only files that have no / equivalent |
| 64 | for name in codenames: |
| 65 | othername = code2macname(name) |
| 66 | sync(os.path.join(top, name), os.path.join(top, othername)) |
| 67 | |
| 68 | def mac2codename(name): |
| 69 | return re.sub('/', SUBST, name) |
| 70 | |
| 71 | def code2macname(name): |
| 72 | return re.sub(SUBST, '/', name) |
| 73 | |
| 74 | def sync(old, new): |
| 75 | if os.path.exists(new): |
| 76 | # Both exist. Check that they are indentical |
| 77 | d1 = open(old, 'rb').read() |
| 78 | d2 = open(new, 'rb').read() |
| 79 | if d1 == d2: |
| 80 | print '-- OK ', old |
| 81 | return |
| 82 | print '** OUT-OF-SYNC', old |
| 83 | fp = open(new, 'wb') |
| 84 | fp.write(open(old, 'rb').read()) |
| 85 | print '-- COPIED ', old |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |
| 89 | |