hash_source.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import hashlib
  12. import os
  13. import sys
  14. ignore_folder = ["node_modules", "dist", "coverage", "build", "scripts", "test-config"]
  15. ignore_file_name = [
  16. "DS_Store",
  17. "env.local",
  18. "env.development.local",
  19. "env.test.local",
  20. "env.production.local",
  21. ".eslintrc.js",
  22. ".gitignore",
  23. "jest.config.js",
  24. "typedoc-mkdocs.json",
  25. ".env",
  26. "dev.env",
  27. ]
  28. ignore_file_extension = [".md"]
  29. def hash_file(file_path):
  30. hasher = hashlib.sha256()
  31. with open(file_path, "rb") as f:
  32. buf = f.read()
  33. hasher.update(buf)
  34. return hasher.hexdigest()
  35. def hash_files_in_frontend_folder(frontend_folder):
  36. combined_hasher = hashlib.sha256()
  37. file_hashes = {}
  38. lookup_fe_folder = [f"{frontend_folder}{os.sep}taipy", f"{frontend_folder}{os.sep}taipy-gui"]
  39. if len(sys.argv) > 1:
  40. if sys.argv[1] == "--taipy-gui-only":
  41. lookup_fe_folder.pop(0)
  42. elif sys.argv[1] == "--taipy-gui-core-only":
  43. lookup_fe_folder.pop(1)
  44. for root_folder in lookup_fe_folder:
  45. # Sort before looping to ensure consistent cache key
  46. for root, _, files in sorted(os.walk(root_folder)):
  47. if any(ignore in root for ignore in ignore_folder):
  48. continue
  49. # Sort before looping to ensure consistent cache key
  50. for file in sorted(files):
  51. if any(ignore in file for ignore in ignore_file_name):
  52. continue
  53. if any(file.endswith(ignore) for ignore in ignore_file_extension):
  54. continue
  55. file_path = os.path.join(root, file)
  56. file_hash = hash_file(file_path)
  57. file_hashes[file_path] = file_hash
  58. combined_hasher.update(file_hash.encode())
  59. combined_hash = combined_hasher.hexdigest()
  60. return file_hashes, combined_hash
  61. if __name__ == "__main__":
  62. frontend_folder = "frontend"
  63. file_hashes, combined_hash = hash_files_in_frontend_folder(frontend_folder)
  64. # for path, file_hash in sorted(file_hashes.items()):
  65. # print(f"{path}: {file_hash}")
  66. print(f"Taipy Frontend hash {combined_hash}") # noqa: T201
  67. # write combined hash to file
  68. with open("hash.txt", "w") as f:
  69. f.write(combined_hash)