From 54a47670f1b14b5c0ed8827dfb13bea833e7a3be Mon Sep 17 00:00:00 2001 From: Alvis <alvisisme@163.com> Date: Fri, 14 Aug 2020 11:51:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(apt):=20=E5=8A=A0=E5=85=A5ubuntu=20deb?= =?UTF-8?q?=E5=8C=85=E4=B8=8B=E8=BD=BD=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- download-apt.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 download-apt.py diff --git a/download-apt.py b/download-apt.py new file mode 100644 index 0000000..fb79737 --- /dev/null +++ b/download-apt.py @@ -0,0 +1,64 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- +#================== +# Author: Alvis Zhao +# Date: 2020-08-14 11:45:09 +# LastEditTime: 2020-08-14 11:51:17 +# Description: 下载ubuntu deb软件及其依赖包 +#================== + +import os +import sys + +def showUsage(): + print("Usage:") + print(" python download-apt.py net-tools") + +def getDepends(pkg): + dependList = [] + cmd = 'apt-cache depends ' + pkg + ' | grep Depends |cut -d: -f2 |tr -d "<>" | sort | uniq' + pipeline = os.popen(cmd) + items = pipeline.read().split("\n") + for item in items: + if len(item) > 0: + dependList.append(item) + return dependList + +def main(): + if len(sys.argv) < 2: + print("用法错误!") + showUsage() + return + + pkgAll = [] + pkg = sys.argv[1] + outputDir = pkg + pkgAll.append(pkg) + + os.popen('mkdir -p ' + outputDir) + # dependList = getDepends(pkg) + # pkgAll = pkgAll + dependList + # 递归深度 + depth = 3 + count = 0 + tempList = [] + tempList.append(pkg) + while (count < depth): + count = count + 1 + itemDepends = [] + for item in tempList: + dependList = getDepends(item) + itemDepends = itemDepends + dependList + pkgAll = pkgAll + itemDepends + tempList = itemDepends + + # 先将列表转化为set,再转化为list就可以实现去重操作 + pkgAll = list(set(pkgAll)) + os.chdir(outputDir) + for item in pkgAll: + # download deb package + print(" downloading " + item) + os.popen('apt-get download ' + item) + +if __name__ == '__main__': + main()