Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding copyright header to python files #2596

Closed
wants to merge 12 commits into from
Closed

Conversation

germa89
Copy link
Collaborator

@germa89 germa89 commented Jan 2, 2024

As the title.

Probably overkill a bit....

Python script

Python script used to inject the header

import os

header = """Copyright (C) 2016 - 2024 ANSYS, Inc. and/or its affiliates.
SPDX-License-Identifier: MIT

MIT License

Copyright (c) 2024 ANSYS, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

def comment(text, comment_symbol="#"):
    return  "\n".join([f"{comment_symbol} {each}" for each in text.splitlines()])

def process_file(file_path, type_):
    with open(file_path, 'r') as file:
        content = file.read()

    if not content or header.splitlines()[0] in content:
        return
    
    # Replace the old header with the new header
    if type_ == "example":
        out_content = comment(header) + "\n\n" + content
    
    elif type_ == "apdl":
        out_content = comment(header, comment_symbol="!") + "\n\n" + content
    
    elif '"' in content[0]:
        # Start by "
        index = content.find('"""') + 3
        out_content = content[:index] + "\n" + header + "\n\n" + content[index:]

    elif "#" in content[0]:
        # Start by #
        index = content.find('#')
        out_content = content[:index] + comment(header) + "\n\n" + content[index:]

    else:
        # Adding a triple quoted comment
        out_content = '"""\n' + header + '"""\n' + content

    with open(file_path, 'w') as file:
        file.write(out_content)


def change_header(directory, is_example):
    for root, _, files in os.walk(directory):
        for file_name in files:
            if file_name.split(".")[1] not in ["py", "cdb", "CDB", "dat"]:
                continue

            print(file_name)
            file_path = os.path.join(root, file_name)

            try:
                if is_example:
                    type_ = "example"

                elif file_name.endswith("py"):
                    type_ = "python"

                else:
                    type_ = "apdl"

                process_file(file_path, type_)

            except Exception as e:
                print(file_path)
                print(e)

if __name__ == "__main__":
    # Replace these values with your actual directory and headers
    directory_path = [r"C:\Users\gayuso\pymapdl\src", r"C:\Users\gayuso\pymapdl\tests", r"C:\Users\gayuso\pymapdl\examples"]

    for each in directory_path:
        is_example= True if "examples" in each else False
        change_header(each, is_example)

@ansys-reviewer-bot
Copy link
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6b3d6b1) 81.80% compared to head (5ca4e97) 80.11%.
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2596      +/-   ##
==========================================
- Coverage   81.80%   80.11%   -1.70%     
==========================================
  Files          46       46              
  Lines        9090     9090              
==========================================
- Hits         7436     7282     -154     
- Misses       1654     1808     +154     

@germa89 germa89 self-assigned this Jan 2, 2024
@germa89 germa89 requested a review from clatapie January 2, 2024 10:15
@germa89 germa89 enabled auto-merge (squash) January 2, 2024 10:16
@germa89 germa89 requested a review from a team January 2, 2024 10:45
@RobPasMue
Copy link
Member

@germa89 - please use the pre-commit hook for this action. Have a look at PyMechanical's pre-commit hook

@RobPasMue
Copy link
Member

Do not implement the script on your own

@RobPasMue
Copy link
Member

@RobPasMue
Copy link
Member

Pinging @klmcadams to see feedback (whenever it is given by @germa89)

Copy link
Member

@RobPasMue RobPasMue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@germa89
Copy link
Collaborator Author

germa89 commented Jan 2, 2024

Feedback:

  • The file LICENSE at the root directory of each repo is not updated.
  • The files modified cannot be customized in an extendable way. We can specify which files to omit from the default, but we cannot add files to the default.
  • Same with extensions, only python and proto files are allowed. I would like to add the Verification manual files (.dat, .cdb, and .CDB).
  • Because of the above point, I need to specify a custom comment symbol other than #.
  • Bonus: Ideally, it will be nice to reuse (somehow) the license file for the template, I think we can specify a template, but it will be nice to not have 2 files around, specially at the root of the repo.

Pinging @klmcadams

@germa89
Copy link
Collaborator Author

germa89 commented Jan 2, 2024

Closed in favor of #2597

@germa89 germa89 closed this Jan 2, 2024
auto-merge was automatically disabled January 2, 2024 12:29

Pull request was closed

@germa89 germa89 deleted the feat/adding-copyright branch January 2, 2024 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants