build_script.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import glob
  3. import shutil
  4. import subprocess
  5. import sys
  6. import argparse
  7. import compileall
  8. # Step 1: Compile .py files to .pyc files
  9. def compile_files_to_pyc(root_dir):
  10. compileall.compile_dir(root_dir, force=True, legacy=True)
  11. for py_file in glob.glob(f"{root_dir}/**/*.py", recursive=True):
  12. py_cache_dir = os.path.join(os.path.dirname(py_file), '__pycache__')
  13. for pyc_file in glob.glob(f"{py_cache_dir}/*.pyc"):
  14. target_file = os.path.join(os.path.dirname(py_file), os.path.basename(pyc_file))
  15. shutil.move(pyc_file, target_file)
  16. print(f"Moved {pyc_file} to {target_file}")
  17. if os.path.exists(py_cache_dir):
  18. shutil.rmtree(py_cache_dir)
  19. # Step 2: Remove .py files from the build directory except setup.py
  20. def remove_py_files_from_build(build_dir):
  21. for py_file in glob.glob(f"{build_dir}/**/*.py", recursive=True):
  22. if os.path.basename(py_file) == "setup.py":
  23. continue
  24. os.remove(py_file)
  25. print(f"Removed {py_file} from build directory")
  26. # Step 3: Backup .py files
  27. def backup_py_files(root_dir, backup_dir):
  28. if os.path.exists(backup_dir):
  29. shutil.rmtree(backup_dir)
  30. shutil.copytree(root_dir, backup_dir, ignore=shutil.ignore_patterns('__pycache__', 'build', 'dist', '*.egg-info', '.git'))
  31. # Step 4: Restore .py files from backup
  32. def restore_py_files_from_backup(root_dir, backup_dir):
  33. for item in os.listdir(backup_dir):
  34. s = os.path.join(backup_dir, item)
  35. d = os.path.join(root_dir, item)
  36. if os.path.isdir(s):
  37. shutil.copytree(s, d, dirs_exist_ok=True)
  38. else:
  39. shutil.copy2(s, d)
  40. # Step 5: Setup and compile
  41. def compile_extensions(build_dir):
  42. subprocess.run([sys.executable, "setup.py", "bdist_wheel", "--dist-dir", "../dist"], cwd=build_dir)
  43. def main(include_source):
  44. root_dir = "."
  45. backup_dir = "./backup"
  46. build_dir = "./build"
  47. if not include_source:
  48. backup_py_files(root_dir, backup_dir)
  49. compile_files_to_pyc(root_dir)
  50. # Copy files to build_dir for packaging
  51. if os.path.exists(build_dir):
  52. shutil.rmtree(build_dir)
  53. shutil.copytree(root_dir, build_dir, ignore=shutil.ignore_patterns('__pycache__', 'build', 'dist', '*.egg-info', '.git'))
  54. remove_py_files_from_build(build_dir)
  55. compile_extensions(build_dir)
  56. if not include_source:
  57. restore_py_files_from_backup(root_dir, backup_dir)
  58. shutil.rmtree(backup_dir)
  59. if os.path.exists(build_dir):
  60. shutil.rmtree(build_dir)
  61. if __name__ == "__main__":
  62. parser = argparse.ArgumentParser(description="Build and package the Python project.")
  63. parser.add_argument("--include-source", action="store_true", help="Include source code in the package")
  64. args = parser.parse_args()
  65. main(args.include_source)