main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import argparse
  2. import re
  3. import subprocess
  4. import time
  5. import os
  6. OUT_DIR = "output"
  7. DEB_DIR = f"{OUT_DIR}/debs"
  8. SYSROOT_DIR = f"{OUT_DIR}/sysroot"
  9. def resolve_packages(package_name: str):
  10. print("Resolving all packages...")
  11. # Run the apt-rdepends command and capture its output
  12. result = subprocess.run(['apt-rdepends', '-d', package_name], capture_output=True, text=True)
  13. # Check if the command was successful
  14. if result.returncode != 0:
  15. print(f"Error resolving dependencies for {package_name}")
  16. return {}
  17. apt_rdepends_output = result.stdout
  18. # apt_rdepends_output = f"""
  19. # Reading package lists... Done
  20. # Building dependency tree... Done
  21. # Reading state information... Done
  22. # digraph packages {{
  23. # concentrate=true;
  24. # size="30,40";
  25. # "libc6" [shape=box];
  26. # "libc6" -> "libcrypt1";
  27. # "libc6" -> "libgcc-s1";
  28. # "libcrypt1" [shape=box];
  29. # "libcrypt1" -> "libc6";
  30. # "libgcc-s1" [shape=box];
  31. # "libgcc-s1" -> "gcc-12-base";
  32. # "libgcc-s1" -> "libc6";
  33. # "gcc-12-base" [shape=box];
  34. # }}
  35. # """
  36. # Regular expression to match package names
  37. package_pattern = re.compile(r'"([a-zA-Z0-9_-]+)"')
  38. # Find all package names in the output
  39. packages = package_pattern.findall(apt_rdepends_output)
  40. # 对packages去重
  41. packages = list(set(packages))
  42. return packages
  43. def download(args: argparse.Namespace):
  44. '''
  45. Download deb and its dependencies
  46. '''
  47. package_name = args.name
  48. print(f"package_name: {package_name}")
  49. # resolve all packages
  50. packages = resolve_packages(package_name)
  51. print(f"packages: {packages}")
  52. # update
  53. subprocess.run(['apt', 'update'], check=True)
  54. # Download the packages using apt-
  55. packages_str = ' '.join(packages)
  56. try:
  57. subprocess.run('apt install --reinstall --download-only -y ' + packages_str, check=True, shell=True)
  58. print(f"Downloaded {packages_str}")
  59. except subprocess.CalledProcessError as e:
  60. print(f"Failed to download {packages_str}: {e}")
  61. # Move the downloaded packages to the DEB_DIR
  62. subprocess.run(['mkdir', '-p', DEB_DIR], check=True)
  63. subprocess.run('mv /var/cache/apt/archives/*.deb ' + DEB_DIR, check=True, shell=True)
  64. print(f"Downloaded {len(packages)} packages to {DEB_DIR}")
  65. def unpack(args: argparse.Namespace):
  66. print("Executing unpack command")
  67. # 获取 DEB_DIR 下的所有 deb 文件列表
  68. deb_files = [f for f in os.listdir(DEB_DIR) if f.endswith('.deb')]
  69. for deb_file in deb_files:
  70. deb_path = os.path.join(DEB_DIR, deb_file)
  71. subprocess.run(['dpkg', '-x', deb_path, SYSROOT_DIR], check=True)
  72. print(f"Unpacked {len(deb_files)} packages to {SYSROOT_DIR}")
  73. def clean(args: argparse.Namespace):
  74. print("Executing clean command")
  75. subprocess.run(['rm', '-rf', OUT_DIR], check=True)
  76. print(f"Cleaned up {OUT_DIR}")
  77. def help_command(args: argparse.Namespace):
  78. print("""
  79. Usage: main.py [options]
  80. Options:
  81. --download Download deb and its dependencies
  82. --unpack Unpack downloaded files
  83. --clean Clean up temporary files
  84. --help Show this help message and exit
  85. """)
  86. def main():
  87. parser = argparse.ArgumentParser(description="deb downloader")
  88. subparsers = parser.add_subparsers(dest="command")
  89. # Subcommand: download
  90. download_parser = subparsers.add_parser("download", help="Download package")
  91. download_parser.add_argument("name", help="Name of the deb package")
  92. download_parser.set_defaults(func=download)
  93. # Subcommand: unpack
  94. unpack_parser = subparsers.add_parser("unpack", help="Unpack downloaded files")
  95. unpack_parser.set_defaults(func=unpack)
  96. # Subcommand: clean
  97. clean_parser = subparsers.add_parser("clean", help="Clean up temporary files")
  98. clean_parser.set_defaults(func=clean)
  99. # Subcommand: help
  100. help_parser = subparsers.add_parser("help", help="Show help message")
  101. help_parser.set_defaults(func=help_command)
  102. args = parser.parse_args()
  103. if args.command:
  104. args.func(args)
  105. else:
  106. parser.print_help()
  107. if __name__ == "__main__":
  108. main()