detached
     !ˆÌËÏ466ÊÃÍ Ò    !‹      µ“]oÓ0†ï÷+¾à®=m÷E7Çh$•¨ÔµU„B•c;‰EGŽKÙ~=NÓ±‚Vñ!¸É‡Ï›÷¼ÏqL_D‹0þ°œÀÛøvËwofÓHñýiˆÅQW8ë†ˆ“9’;W_!n·Ûþö´ol†ñ
sWgØ8«…ëK'	;¡í;š+.ý¨Ó®PlZIõL
h6®ÑRyçz“Z<.|/Œð�—%—èÊ¹Z§ºPëÆ«Öµ5r#œ6Ž/#Š�½oˆûŽ41ò¾2üM½÷�'»Þí£eÔåð…:«âLMÕe�A-LÕ`Rðês?Ó)^¸€|œ†‹O^‡.o¿f”CnU�×a0¿^as^*ŠüyÍ­×Ü6ã�ƒÒH�j%�Šï:ñ�~8nušH5Âêºå}’¢G<D¦hjîYÏ=AnU�Dþj\ü0ŒåÍj2�£éª‰l-žÒýÃ-$lÉ­ªDÚ*á«÷{Ê®åË*iêëÇWØ3X�åŽ0€<«üðÔRÙCôcÐãWŸ¶½&ü)Òh0ºè‡½Ñ%.®ÎÇ •ý7·÷?=ÅÝ9£Øøoä:)öS    7   #ˆl–äY>”Ên-ji@¿pB¸ÈÅ£�þ\n,à„ãñŒBy÷�üÿÑ„N¶÷ÐÄÎ ?÷     ##!/usr/bin/python3
#
# Copyright 2021 Johannes Schauer Marin Rodrigues <josch@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# This tool is similar to debootstrap but is able to recreate a chroot
# containing precisely the given package and version selection. The package
# list is expected on standard input and may be of the format produced by:
#
#     dpkg-query --showformat '${binary:Package}=${Version}\n' --show

# The name was suggested by Adrian Bunk as a portmanteau of debootstrap and
# snapshot.debian.org.

# TODO: Adress invalid names
# pylint: disable=invalid-name

import argparse
import dataclasses
import http.server
import os
import pathlib
import re
import shutil
import socketserver
import subprocess
import sys
import tempfile
import threading
import time
from collections import defaultdict
from contextlib import contextmanager
from functools import partial
from http import HTTPStatus
from operator import itemgetter

import pycurl
import requests
from debian.deb822 import BuildInfo


class MyHTTPException(Exception):
    pass


class MyHTTP404Exception(Exception):
    pass


class MyHTTPTimeoutException(Exception):
    pass


class RetryCountExceeded(Exception):
    pass


# pylint: disable=c-extension-no-member
class Proxy(http.server.SimpleHTTPRequestHandler):
    last_request = None
    maxretries = 10

    def do_GET(self):  # pylint: disable=too-many-branches,too-many-statements
        # check validity and extract the timestamp
        url = "http://snapshot.debian.org/" + self.path
        start = None
        state = ""
        written = 0
        for retrynum in range(self.maxretries):
            try:
                c = pycurl.Curl()
                c.setopt(c.URL, url)
                # even 100 kB/s is too much sometimes
                c.setopt(c.MAX_RECV_SPEED_LARGE, 1000 * 1024)  # bytes per second
                c.setopt(c.CONNECTTIMEOUT, 30)  # the default is 300
                # sometimes, curl stalls forever and even ctrl+c doesn't work
                start = time.time()

                def progress(*_):
                    # a download must not last more than 10 minutes
                    # with 100 kB/s this means files cannot be larger than 62MB
                    if time.time() - start > 10 * 60:
                        print("transfer took too long")
                        # the code will not see this exception but instead get a
                        # pycurl.error
                        raise MyHTTPTimeoutException(url)

                c.setopt(pycurl.NOPROGRESS, 0)
                c.setopt(pycurl.XFERINFOFUNCTION, progress)
                # $ host snapshot.debian.org
                # snapshot.debian.org has address 185.17.185.185
                # snapshot.debian.org has address 193.62.202.27
                # c.setopt(c.RESOLVE, ["snapshot.debian.org:80:185.17.185.185"])
                if written > 0:
                    c.setopt(pycurl.RESUME_FROM, written)

     