|
| 1 | +# Copyright © 2021 United States Government as represented by the Administrator of the |
| 2 | +# National Aeronautics and Space Administration. All Rights Reserved. |
| 3 | + |
| 4 | +import os |
| 5 | +COPYRIGHT_TAG = "Copyright © 2021 United States Government as represented by the Administrator" # String to check file lines for |
| 6 | + |
| 7 | +def check_copyright(directory: str, invalid_files: list) -> bool: |
| 8 | + result = True |
| 9 | + |
| 10 | + for filename in os.listdir(directory): |
| 11 | + path = os.path.join(directory, filename) |
| 12 | + |
| 13 | + # If path is subdirectory, recursively check files/subdirectories within |
| 14 | + if os.path.isdir(path): |
| 15 | + result = result and check_copyright(path, invalid_files) |
| 16 | + # If path is a file, ensure it is of type py and check for copyright |
| 17 | + elif os.path.isfile(path) and path.split(".")[-1] in ("py", "pyw", "py3"): |
| 18 | + file = open(path, 'r') |
| 19 | + copyright_met = False |
| 20 | + # Iterate over lines in file, check each line against COPYRIGHT_TAG |
| 21 | + for line in file: |
| 22 | + if COPYRIGHT_TAG in line: |
| 23 | + # File contains copyright, skip rest of lines |
| 24 | + file.close() |
| 25 | + copyright_met = True |
| 26 | + if copyright_met: |
| 27 | + break |
| 28 | + if not copyright_met: |
| 29 | + result = False |
| 30 | + invalid_files.append(path) |
| 31 | + file.close() |
| 32 | + |
| 33 | + return result |
| 34 | + |
| 35 | +def main(): |
| 36 | + print("\n\nTesting Files for Copyright Information") |
| 37 | + |
| 38 | + root = '../prog_models' |
| 39 | + invalid_files = [] |
| 40 | + copyright_confirmed = check_copyright(root, invalid_files) |
| 41 | + |
| 42 | + if not copyright_confirmed: |
| 43 | + raise Exception(f"Failed test\nFiles missing copyright information: {invalid_files}") |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + main() |
0 commit comments