Last active
November 10, 2019 02:26
-
-
Save sblmnl/a2dbc34b4381e9f912e12cd9f893693e to your computer and use it in GitHub Desktop.
Recursive package downloader for Debian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A Debian package downloader that downloads all package | |
dependencies and sub-dependencies from packages.debian.org. | |
Author: https://github.com/sblmnl | |
License: MIT | |
Date Created: 2019-02-26 (YYYY-MM-DD) | |
Sample Usage: | |
python debby.py stretch amd64 gnome,gcc,python3 | |
""" | |
import os | |
import sys | |
import threading | |
import urllib.request | |
location = os.path.dirname(os.path.realpath(__file__)) | |
target = location | |
def get_contents(url): | |
response = urllib.request.urlopen(url) | |
contents = response.read() | |
return contents | |
def get_package_dependencies(url): | |
dependencies = [] | |
html = get_contents(url).decode("utf-8").split(">") | |
for i in range(len(html)): | |
if "dep:" in html[i]: | |
href = html[i + 1] | |
href = href.split("\"") | |
url = href[1] | |
url = "https://packages.debian.org/%s" % url | |
dependencies.append(url) | |
return dependencies | |
def download_package(package): | |
html = get_contents("%s/download" % package).decode("utf-8").split("\n") | |
url = "" | |
for line in html: | |
if "ftp.us.debian.org" in line: | |
tmp = line.split("\"") | |
url = tmp[1] | |
break | |
try: | |
data = get_contents(url) | |
filename = url.split("/") | |
filename = filename[len(filename) - 1] | |
print("downloading: %s" % url) | |
f = open("%s/%s" % (target, filename), "wb+") | |
f.write(data) | |
f.close() | |
except: | |
try: | |
data = get_contents(url) | |
filename = url.split("/") | |
filename = filename[len(filename) - 1] | |
print("downloading: %s" % url) | |
f = open("%s/%s" % (target, filename), "wb+") | |
f.write(data) | |
f.close() | |
except: | |
print("failed to download: %s" % url) | |
pass | |
dist = sys.argv[1] | |
arch = sys.argv[2] | |
pkgs = sys.argv[3] | |
for pkg in pkgs.split(","): | |
if not os.path.exists("%s/packages" % location): | |
os.mkdir("%s/packages" % location) | |
if not os.path.exists("%s/packages/%s" % (location, pkg)): | |
os.mkdir("%s/packages/%s" % (location, pkg)) | |
target = "%s/packages/%s" % (location, pkg) | |
print("getting packages...") | |
packages = [] | |
url = "https://packages.debian.org/%s/%s/%s" % (dist, arch, pkg) | |
packages.append(url) | |
working = True | |
current = 0 | |
while True: | |
print("getting dependencies for: %s" % packages[current]) | |
dependencies = get_package_dependencies(packages[current]) | |
for dependency in dependencies: | |
if dependency not in packages: | |
print("adding dependency: %s" % dependency) | |
packages.append(dependency) | |
current += 1 | |
if (current >= len(packages)): | |
break | |
for package in packages: | |
download_package(package) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment