update-flet-wheel-deps.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. import re
  3. import shutil
  4. import subprocess
  5. import sys
  6. import tempfile
  7. from pathlib import Path
  8. import wheel.cli.pack
  9. import wheel.cli.unpack
  10. # Get all wheel file paths
  11. def find_wheel_files(directory):
  12. wheel_files = list(Path(directory).glob("flet-*.whl"))
  13. if not wheel_files:
  14. print("No files found matching the pattern flet-*.whl")
  15. return []
  16. return [str(wheel_file.resolve()) for wheel_file in wheel_files]
  17. # Extract version from the wheel file name
  18. def extract_version(wheel_file):
  19. match = re.search(r".*-([0-9]+[^-]+)-.*", wheel_file)
  20. if match:
  21. return match.group(1)
  22. return None
  23. # Process the METADATA file
  24. def update_metadata(metadata_file, version):
  25. with open(metadata_file, "r") as file:
  26. lines = file.readlines()
  27. i = 0
  28. while i < len(lines):
  29. # insert Requires-Dist: flet-desktop-light ...
  30. if lines[i].startswith("Requires-Dist: flet-desktop "):
  31. lines.insert(
  32. i + 1,
  33. f"Requires-Dist: flet-desktop-light (=={version}) ; platform_system == 'Linux' and 'embedded' not in platform_version\n",
  34. )
  35. lines[i] = re.sub(
  36. r'platform_system != "desktop-light"',
  37. "(platform_system == 'Darwin' or platform_system == 'Windows') and 'embedded' not in platform_version",
  38. lines[i],
  39. )
  40. lines[i] = re.sub(
  41. r'platform_system != "embedded"',
  42. "(platform_system == 'Darwin' or platform_system == 'Linux' or platform_system == 'Windows') and 'embedded' not in platform_version",
  43. lines[i],
  44. )
  45. i += 1
  46. with open(metadata_file, "w") as file:
  47. file.writelines(lines)
  48. # Main logic
  49. def process_wheels(directory):
  50. # Find all wheel files
  51. wheel_files = find_wheel_files(directory)
  52. if not wheel_files:
  53. return
  54. for wheel_file in wheel_files:
  55. print(f"Found file: {wheel_file}")
  56. # Extract version from the wheel file name
  57. version = extract_version(wheel_file)
  58. if not version:
  59. print(f"Unable to extract version from wheel file: {wheel_file}")
  60. continue
  61. print(f"Version: {version}")
  62. # Create a temporary directory
  63. with tempfile.TemporaryDirectory() as tmp_dir:
  64. # Unpack the wheel file
  65. wheel.cli.unpack.unpack(wheel_file, tmp_dir)
  66. # Get the unpacked wheel directory
  67. unpacked_wheel_dir = next(Path(tmp_dir).glob("flet-*")).resolve()
  68. print(f"Wheel temp dir: {unpacked_wheel_dir}")
  69. # Process the METADATA file
  70. metadata_files = list(unpacked_wheel_dir.glob("*.dist-info/METADATA"))
  71. for metadata_file in metadata_files:
  72. update_metadata(metadata_file, version)
  73. print(f"Updated METADATA file: {metadata_file}")
  74. # Remove the old wheel file
  75. os.remove(wheel_file)
  76. # Repack the wheel
  77. wheel.cli.pack.pack(unpacked_wheel_dir, os.path.dirname(wheel_file), None)
  78. print("Successfully updated flet-*.whl with platform-specific dependencies.")
  79. if len(sys.argv) < 2:
  80. print("Specify path to dist")
  81. sys.exit(1)
  82. process_wheels(sys.argv[1])