icode2github.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import re
  16. NEW_COPYRIGHT = '# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.\n\
  17. # \n\
  18. # Licensed under the Apache License, Version 2.0 (the "License");\n\
  19. # you may not use this file except in compliance with the License.\n\
  20. # You may obtain a copy of the License at\n\
  21. #\n\
  22. # http://www.apache.org/licenses/LICENSE-2.0\n\
  23. #\n\
  24. # Unless required by applicable law or agreed to in writing, software\n\
  25. # distributed under the License is distributed on an "AS IS" BASIS,\n\
  26. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\
  27. # See the License for the specific language governing permissions and\n\
  28. # limitations under the License.\n'
  29. def replace_copyright_in_file(file_path, new_copyright):
  30. """
  31. Replace copyright information in single Python file.
  32. Args:
  33. file_path (str): The path of the file to be processed.
  34. new_copyright (str): The new copyright information.
  35. Returns:
  36. None
  37. """
  38. print(f"Processing file: {file_path}")
  39. try:
  40. with open(file_path, 'r+', encoding='utf-8') as file:
  41. content = file.read()
  42. pattern = re.compile(r'(# !/usr/bin/env python3[\s\S]*?Authors\s*\n""")', re.MULTILINE)
  43. new_content = pattern.sub(new_copyright + '\n', content)
  44. if new_content != content:
  45. print(f"Copyright information replaced in {file_path}")
  46. file.seek(0) # Reset the file pointer to the beginning of the file.
  47. file.write(new_content)
  48. file.truncate()
  49. except Exception as e:
  50. print(f"Error processing file {file_path}: {e}")
  51. def replace_copyright_in_directory(directory, new_copyright):
  52. """
  53. Replace copyright information in Python files under the specified directory.
  54. Args:
  55. directory (str): The directory path where Python files are located.
  56. new_copyright (str): The new copyright information to be replaced.
  57. Returns:
  58. None.
  59. """
  60. for root, dirs, files in os.walk(directory):
  61. for file in files:
  62. if file.endswith('.py'):
  63. file_path = os.path.join(root, file)
  64. replace_copyright_in_file(file_path, new_copyright)
  65. if __name__ == '__main__':
  66. replace_copyright_in_directory('./', NEW_COPYRIGHT)