| 24 | | def process_directory(path): |
| 25 | | for file in os.listdir(path): |
| 26 | | fpath = os.path.join(path, file) |
| 27 | | if os.path.isfile(fpath): |
| 28 | | process_file(fpath) |
| 29 | | elif os.path.isdir(fpath): |
| 30 | | process_directory(fpath) |
| | 25 | def process_directory(path, delete_unlicensed, undo): |
| | 26 | for file in os.listdir(path): |
| | 27 | fpath = os.path.join(path, file) |
| | 28 | if os.path.isfile(fpath): |
| | 29 | if undo: |
| | 30 | undo_file(fpath) |
| | 31 | else: |
| | 32 | process_file(fpath, delete_unlicensed) |
| | 33 | elif os.path.isdir(fpath): |
| | 34 | process_directory(fpath, delete_unlicensed, undo) |
| 32 | | def process_file(fpath): |
| 33 | | if (fpath.endswith(".cp") or |
| 34 | | fpath.endswith(".cc") or |
| 35 | | fpath.endswith(".cpp") or |
| 36 | | fpath.endswith(".c") or |
| 37 | | fpath.endswith(".h")): |
| 38 | | replace_license(fpath) |
| | 36 | def undo_file(fpath): |
| | 37 | if (fpath.endswith(".cp.unlicensed") or |
| | 38 | fpath.endswith(".cc.unlicensed") or |
| | 39 | fpath.endswith(".cpp.unlicensed") or |
| | 40 | fpath.endswith(".c.unlicensed") or |
| | 41 | fpath.endswith(".h.unlicensed")): |
| | 42 | fpath = fpath[:-11] |
| | 43 | print fpath |
| | 44 | os.rename(fpath, "%s.licensed" % fpath) |
| | 45 | os.rename("%s.unlicensed" % fpath, fpath) |
| | 46 | os.remove("%s.licensed" % fpath) |
| | 47 | |
| | 48 | def process_file(fpath, delete_unlicensed): |
| | 49 | if delete_unlicensed and \ |
| | 50 | (fpath.endswith(".cp.unlicensed") or |
| | 51 | fpath.endswith(".cc.unlicensed") or |
| | 52 | fpath.endswith(".cpp.unlicensed") or |
| | 53 | fpath.endswith(".c.unlicensed") or |
| | 54 | fpath.endswith(".h.unlicensed")): |
| | 55 | os.remove(fpath) |
| | 56 | elif not delete_unlicensed and \ |
| | 57 | (fpath.endswith(".cp") or |
| | 58 | fpath.endswith(".cc") or |
| | 59 | fpath.endswith(".cpp") or |
| | 60 | fpath.endswith(".c") or |
| | 61 | fpath.endswith(".h")): |
| | 62 | replace_license(fpath) |
| 45 | | first = True |
| 46 | | ignore = False |
| 47 | | reallyignore = False |
| 48 | | ignored = [] |
| 49 | | for line in fin: |
| 50 | | if first: |
| 51 | | fout.write(newlicense) |
| 52 | | if line.startswith("/*"): |
| 53 | | ignore = True |
| 54 | | first = False |
| 55 | | if not ignore: |
| 56 | | fout.write(line) |
| 57 | | else: |
| 58 | | ignored.append(line) |
| 59 | | if ignore: |
| 60 | | if line.find("Copyright") != -1: |
| 61 | | reallyignore = True |
| 62 | | if line.find("*/") != -1: |
| 63 | | ignore = False |
| 64 | | if not reallyignore: |
| 65 | | fout.write("".join(ignored)) |
| 66 | | |
| 67 | | fin.close() |
| 68 | | fout.close() |
| 69 | | os.rename(fpath, "%s.unlicensed" % fpath) |
| 70 | | os.rename("%s.licensed" % fpath, fpath) |
| | 69 | first = True |
| | 70 | ignore = False |
| | 71 | reallyignore = False |
| | 72 | ignored = [] |
| | 73 | for line in fin: |
| | 74 | if first: |
| | 75 | fout.write(newlicense) |
| | 76 | if line.startswith("/*"): |
| | 77 | ignore = True |
| | 78 | first = False |
| | 79 | if not ignore: |
| | 80 | fout.write(line) |
| | 81 | else: |
| | 82 | ignored.append(line) |
| | 83 | if ignore: |
| | 84 | if line.find("Copyright") != -1: |
| | 85 | reallyignore = True |
| | 86 | if line.find("*/") != -1: |
| | 87 | ignore = False |
| | 88 | if not reallyignore: |
| | 89 | fout.write("".join(ignored)) |
| | 90 | |
| | 91 | fin.close() |
| | 92 | fout.close() |
| | 93 | os.rename(fpath, "%s.unlicensed" % fpath) |
| | 94 | os.rename("%s.licensed" % fpath, fpath) |