main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. # 将 package_name 添加到数组开头
  41. packages.insert(0, package_name)
  42. # 对packages去重
  43. packages = list(set(packages))
  44. return packages
  45. def download(args: argparse.Namespace):
  46. '''
  47. Download deb and its dependencies
  48. '''
  49. package_name = args.name
  50. print(f"package_name: {package_name}")
  51. # resolve all packages
  52. packages = resolve_packages(package_name)
  53. print(f"packages: {packages}")
  54. # update
  55. subprocess.run(['apt', 'update'], check=True)
  56. # Download the packages using apt-
  57. packages_str = ' '.join(packages)
  58. try:
  59. subprocess.run('apt install --reinstall --download-only -y ' + packages_str, check=True, shell=True)
  60. print(f"Downloaded {packages_str}")
  61. except subprocess.CalledProcessError as e:
  62. print(f"Failed to download {packages_str}: {e}")
  63. # Move the downloaded packages to the DEB_DIR
  64. subprocess.run(['mkdir', '-p', DEB_DIR], check=True)
  65. subprocess.run('mv /var/cache/apt/archives/*.deb ' + DEB_DIR, check=True, shell=True)
  66. print(f"Downloaded {len(packages)} packages to {DEB_DIR}")
  67. def unpack(args: argparse.Namespace):
  68. print("Executing unpack command")
  69. # 获取 DEB_DIR 下的所有 deb 文件列表
  70. deb_files = [f for f in os.listdir(DEB_DIR) if f.endswith('.deb')]
  71. for deb_file in deb_files:
  72. deb_path = os.path.join(DEB_DIR, deb_file)
  73. subprocess.run(['dpkg', '-x', deb_path, SYSROOT_DIR], check=True)
  74. print(f"Unpacked {len(deb_files)} packages to {SYSROOT_DIR}")
  75. def clean(args: argparse.Namespace):
  76. print("Executing clean command")
  77. subprocess.run(['rm', '-rf', OUT_DIR], check=True)
  78. print(f"Cleaned up {OUT_DIR}")
  79. def help_command(args: argparse.Namespace):
  80. print("""
  81. Usage: main.py [options]
  82. Options:
  83. --download Download deb and its dependencies
  84. --unpack Unpack downloaded files
  85. --clean Clean up temporary files
  86. --help Show this help message and exit
  87. """)
  88. def main():
  89. parser = argparse.ArgumentParser(description="deb downloader")
  90. subparsers = parser.add_subparsers(dest="command")
  91. # Subcommand: download
  92. download_parser = subparsers.add_parser("download", help="Download package")
  93. download_parser.add_argument("name", help="Name of the deb package")
  94. download_parser.set_defaults(func=download)
  95. # Subcommand: unpack
  96. unpack_parser = subparsers.add_parser("unpack", help="Unpack downloaded files")
  97. unpack_parser.set_defaults(func=unpack)
  98. # Subcommand: clean
  99. clean_parser = subparsers.add_parser("clean", help="Clean up temporary files")
  100. clean_parser.set_defaults(func=clean)
  101. # Subcommand: help
  102. help_parser = subparsers.add_parser("help", help="Show help message")
  103. help_parser.set_defaults(func=help_command)
  104. args = parser.parse_args()
  105. if args.command:
  106. args.func(args)
  107. else:
  108. parser.print_help()
  109. if __name__ == "__main__":
  110. main()