Guido van Rossum | d69a84b | 1997-04-02 06:11:55 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 950a126 | 1996-07-30 17:38:17 +0000 | [diff] [blame] | 2 | |
| 3 | # This program reads all *.py and test/*.py in "libDir", and |
Guido van Rossum | 8731e5c | 1997-10-07 14:55:11 +0000 | [diff] [blame^] | 4 | # copies those files with illegal DOS names to libDir/dos-8x3. |
Guido van Rossum | 950a126 | 1996-07-30 17:38:17 +0000 | [diff] [blame] | 5 | # Names are illegal if they are longer than 8x3 chars or if they |
| 6 | # contain uppercase chars. It also tests for name collisions. |
Guido van Rossum | 8731e5c | 1997-10-07 14:55:11 +0000 | [diff] [blame^] | 7 | # You must first create the directory libDir/dos-8x3 yourself. |
| 8 | # You should remove all files in dos-8x3 if you run it again. |
Guido van Rossum | 950a126 | 1996-07-30 17:38:17 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 8731e5c | 1997-10-07 14:55:11 +0000 | [diff] [blame^] | 10 | # CHANGE libDir TO THE CORRECT DIRECTORY. RM dos-8x3/* FIRST. |
Guido van Rossum | 950a126 | 1996-07-30 17:38:17 +0000 | [diff] [blame] | 11 | |
| 12 | import sys, os, regex, string |
| 13 | |
| 14 | libDir = "./Lib" # Location of Python Lib |
| 15 | |
| 16 | def make8x3(): |
| 17 | reg_uppercase = regex.compile("[A-Z]") |
| 18 | collisions = {} # See if all names are unique in first 8 chars. |
Guido van Rossum | 8731e5c | 1997-10-07 14:55:11 +0000 | [diff] [blame^] | 19 | destDir = os.path.join(libDir, "dos-8x3") |
Guido van Rossum | 950a126 | 1996-07-30 17:38:17 +0000 | [diff] [blame] | 20 | if not os.path.isdir(destDir): |
| 21 | print "Please create the directory", destDir, "first." |
| 22 | err() |
| 23 | while 1: |
| 24 | ans = raw_input("Ok to copy to " + destDir + " [yn]? ") |
| 25 | if not ans: |
| 26 | continue |
| 27 | elif ans[0] == "n": |
| 28 | err() |
| 29 | elif ans[0] == "y": |
| 30 | break |
| 31 | for dirname in libDir, os.path.join(libDir, "test"): |
| 32 | for filename in os.listdir(dirname): |
| 33 | if filename[-3:] == ".py": |
| 34 | name = filename[0:-3] |
| 35 | if len(name) > 8 or reg_uppercase.search(name) >= 0: |
| 36 | shortName = string.lower(name[0:8]) |
| 37 | if collisions.has_key(shortName): |
| 38 | print "Name not unique in first 8 chars:", collisions[shortName], name |
| 39 | else: |
| 40 | collisions[shortName] = name |
| 41 | fin = open(os.path.join(dirname, filename), "r") |
| 42 | dest = os.path.join(destDir, shortName + ".py") |
| 43 | fout = open(dest, "w") |
| 44 | fout.write(fin.read()) |
| 45 | fin.close() |
| 46 | fout.close() |
| 47 | os.chmod(dest, 0644) |
| 48 | elif filename == "." or filename == "..": |
| 49 | continue |
| 50 | elif filename[-4:] == ".pyc": |
| 51 | continue |
| 52 | elif filename == "Makefile": |
| 53 | continue |
| 54 | else: |
| 55 | parts = string.splitfields(filename, ".") |
| 56 | if len(parts) > 2 or \ |
| 57 | len(parts[0]) > 8 or \ |
| 58 | reg_uppercase.search(filename) >= 0 or \ |
| 59 | (len(parts) > 1 and len(parts[1]) > 3): |
| 60 | print "Illegal DOS name", os.path.join(dirname, filename) |
| 61 | sys.exit(0) |
| 62 | def err(): |
| 63 | print "No files copied." |
| 64 | sys.exit(1) |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | make8x3() |