|
21 | 21 | import sys
|
22 | 22 |
|
23 | 23 | def review_patches(patches):
|
24 |
| - ret = 0 |
25 |
| - for patch in patches: |
26 |
| - out = "" |
27 |
| - f = open(patch) |
| 24 | + ret = 0 |
| 25 | + for patch in patches: |
| 26 | + out = "" |
| 27 | + f = open(patch) |
28 | 28 |
|
29 |
| - added_files = [] |
30 |
| - files_without_source = [] |
| 29 | + added_files = [] |
| 30 | + files_without_source = [] |
31 | 31 |
|
32 |
| - for line in f.readlines(): |
33 |
| - filename = "" |
34 |
| - if line.startswith("+++"): |
35 |
| - filename = line[4:-1] |
36 |
| - elif line.startswith("diff --git"): |
37 |
| - filename = line[line.rfind(" ") + 1:-1] |
| 32 | + for line in f.readlines(): |
| 33 | + filename = "" |
| 34 | + if line.startswith("+++"): |
| 35 | + filename = line[4:-1] |
| 36 | + elif line.startswith("diff --git"): |
| 37 | + filename = line[line.rfind(" ") + 1:-1] |
38 | 38 |
|
39 |
| - if filename != "" and not filename in added_files: |
40 |
| - added_files.append(filename) |
41 |
| - if not filename.endswith(".source.txt"): |
42 |
| - files_without_source.append(filename) |
43 |
| - f.close() |
| 39 | + if filename != "" and not filename in added_files: |
| 40 | + added_files.append(filename) |
| 41 | + if not filename.endswith(".source.txt"): |
| 42 | + files_without_source.append(filename) |
| 43 | + f.close() |
44 | 44 |
|
45 | 45 |
|
46 |
| - local_error = False |
| 46 | + local_error = False |
47 | 47 |
|
48 |
| - added_files.sort() |
49 |
| - for f in added_files: |
50 |
| - dir_id = f.rfind("/") + 1 |
51 |
| - ext_id = dir_id + f[dir_id:].find(".") |
52 |
| - if ext_id != -1: |
53 |
| - ext = f[ext_id + 1:] |
54 |
| - if ext.endswith("source.txt"): |
55 |
| - real_db_file = f[:-len(".source.txt")] |
56 |
| - try: |
57 |
| - files_without_source.remove(real_db_file) |
58 |
| - except: |
59 |
| - out += "\t" + f + ": .source.txt found for non-existing file, expecting " + real_db_file + " in patch\n" |
60 |
| - ret = 1 |
61 |
| - local_error = True |
62 |
| - else: |
63 |
| - db_dir = os.path.basename(os.path.dirname(f)) |
64 |
| - if db_dir != ext: |
65 |
| - out += "\t" + f + ": File is not in proper directory according to extension\n" |
66 |
| - ret = 1 |
67 |
| - local_error = True |
| 48 | + added_files.sort() |
| 49 | + for f in added_files: |
| 50 | + dir_id = f.rfind("/") + 1 # f[dir_id:] is name.type[.source.txt] |
| 51 | + if dir_id == 0: |
| 52 | + out += "\tMalformed patch: " + f + " should be of the form 'b/db/type/name.type[.source.txt]'\n" |
| 53 | + ret = 1 |
| 54 | + local_error = True |
| 55 | + continue |
| 56 | + if not "/db/" in f: |
| 57 | + continue # file is not in db subdir |
68 | 58 |
|
69 |
| - for f in files_without_source: |
70 |
| - out += "\t" + f + ": Does not have matching .source.txt file with license\n" |
71 |
| - ret = 1 |
72 |
| - local_error = True |
| 59 | + ext_id = dir_id + f[dir_id:].find(".") |
| 60 | + if ext_id == dir_id-1: # check if f is .../db/type/type or .../db/misc/type |
| 61 | + dir_id2 = f.rfind("/", 0, dir_id-1)+1 # f[dir_id2:dir_id] is subdir within db/ |
| 62 | + if f[dir_id2:dir_id-1] == f[dir_id:]: |
| 63 | + pass # ok, is .../db/type/type |
| 64 | + if f[dir_id2] in ('.', '_'): |
| 65 | + out += "\t" + f + ": Subdir names may not start with . or _\n" |
| 66 | + ret = 1 |
| 67 | + local_error = True |
| 68 | + elif f[dir_id2:dir_id-1] == 'misc': |
| 69 | + pass # ok, is .../db/msic/type |
| 70 | + else: |
| 71 | + out += "\t" + f + ": File is not in proper directory according to extension\n" |
| 72 | + ret = 1 |
| 73 | + local_error = True |
| 74 | + else: |
| 75 | + ext = f[ext_id + 1:] # ext is type[.source.txt] |
| 76 | + if ext.endswith("source.txt"): |
| 77 | + real_db_file = f[:-len(".source.txt")] |
| 78 | + try: |
| 79 | + files_without_source.remove(real_db_file) |
| 80 | + except: |
| 81 | + out += "\t" + f + ": .source.txt found for non-existing file, expecting " + real_db_file + " in patch\n" |
| 82 | + ret = 1 |
| 83 | + local_error = True |
| 84 | + else: |
| 85 | + db_dir = os.path.basename(os.path.dirname(f)) |
| 86 | + if db_dir != ext: |
| 87 | + out += "\t" + f + ": File is not in proper directory according to extension\n" |
| 88 | + ret = 1 |
| 89 | + local_error = True |
73 | 90 |
|
74 |
| - if local_error: |
75 |
| - print "Patch", patch + ":" |
76 |
| - print out |
| 91 | + for f in files_without_source: |
| 92 | + out += "\t" + f + ": Does not have matching .source.txt file with license\n" |
| 93 | + ret = 1 |
| 94 | + local_error = True |
77 | 95 |
|
78 |
| - return ret |
| 96 | + if local_error: |
| 97 | + print "Patch", patch + ":" |
| 98 | + print out |
| 99 | + |
| 100 | + return ret |
79 | 101 |
|
80 | 102 | if __name__ == '__main__':
|
81 |
| - if len(sys.argv) == 1: |
82 |
| - print "Reviews patches for file-tests database." |
83 |
| - print """There are following rules currently: |
| 103 | + if len(sys.argv) == 1: |
| 104 | + print "Reviews patches for file-tests database." |
| 105 | + print """There are following rules currently: |
84 | 106 | - Every file 'F' in file-tests database has to have matching 'F.source.txt'
|
85 | 107 | file with the license information. Note that case sensitivity is important
|
86 | 108 | and must be respected.
|
87 | 109 | - Every file with extension '.X' has to be in directory 'db/X' according to
|
88 | 110 | its extension. Note that case sensitivity is important and must be
|
89 | 111 | respected.
|
| 112 | + - If a file has no (typical) extension (e.g. AppleDouble), then it has to go |
| 113 | + to folder db/misc or must be given a descriptive short name and no extension |
| 114 | + and appear in a directory of the same name (e.g. db/AppleDouble/AppleDouble). |
| 115 | + The corresponding .source.txt needs to explain this in a line starting with |
| 116 | + 'NOTE: ' (see e.g. db/AppleDouble/AppleDouble.source.txt). No leading . or _ |
| 117 | + (e.g. .DS_Store --> DS_Store) |
90 | 118 | """
|
91 |
| - print "Usage: " + sys.argv[0] + " [[patch1], [patch2], ...]" |
92 |
| - print "Usage: " + sys.argv[0] + " *.patch" |
93 |
| - sys.exit(0) |
94 |
| - sys.exit(review_patches(sys.argv[1:])) |
| 119 | + print "Usage: " + sys.argv[0] + " [[patch1], [patch2], ...]" |
| 120 | + print "Usage: " + sys.argv[0] + " *.patch" |
| 121 | + sys.exit(0) |
| 122 | + sys.exit(review_patches(sys.argv[1:])) |
0 commit comments