(directory, find, replace)
| 33 | |
| 34 | # Main function which will iterate and replace the copyright year |
| 35 | def find_replace(directory, find, replace): |
| 36 | total = 0 |
| 37 | COPYRIGHT_PATTERN = re.compile( |
| 38 | r'(Copyright \(C\) \d{{4}} - ){0},'.format(find) |
| 39 | ) |
| 40 | failed = [] |
| 41 | |
| 42 | for path, dirs, files in os.walk(os.path.abspath(directory), topdown=True): |
| 43 | # Exclude the specified directory |
| 44 | dirs[:] = [d for d in dirs if d not in EXCLUDE_DIR] |
| 45 | |
| 46 | for filename in filter(is_code_file, files): |
| 47 | current_file = os.path.join(path, filename) |
| 48 | print( |
| 49 | "Updating copyright in {0}... ".format( |
| 50 | current_file |
| 51 | ), end='' |
| 52 | ) |
| 53 | |
| 54 | try: |
| 55 | # Read the file |
| 56 | with codecs.open(current_file, "r", "latin-1") as fp: |
| 57 | content = fp.read() |
| 58 | |
| 59 | is_update_required = False |
| 60 | new_content = COPYRIGHT_PATTERN.sub( |
| 61 | r'\g<1>{},'.format(replace), content |
| 62 | ) |
| 63 | if new_content != content: |
| 64 | is_update_required = True |
| 65 | |
| 66 | if is_update_required: |
| 67 | with codecs.open(current_file, "w", "latin-1") as fp: |
| 68 | fp.write(new_content) |
| 69 | |
| 70 | total += 1 |
| 71 | print("Done") |
| 72 | else: |
| 73 | print("N/A") |
| 74 | except Exception: |
| 75 | failed.append(current_file) |
| 76 | print("FAILED") |
| 77 | |
| 78 | return failed, total |
| 79 | |
| 80 | |
| 81 | def help(): |
no test coverage detected