from dionysus.util import run_command
from dionysus.archive import Archive
import os

archive = Archive("http://192.168.1.50/debian")


def patch(archive, source, dsc):
    print(" -> %s" % (dsc['Source']))

    os.mkdir("uscan")
    with source.unpack():
        version = dsc['Version']
        if '-' not in version:
            return

        upstream, debian = version.rsplit("-", 1)

        print("Uscan'ing")
        out, err, ret = run_command([
            "uscan",
                "--force-download",
                "--download-version", upstream,
                "--destdir", "../uscan/"
        ])
        print("Uscan'd")
        if ret != 0:
            print("Error: %s/%s - %s" % (
                dsc['Source'],
                dsc['Version'],
                ret,
from debversion import Block
from dionysus.archive import Archive
import os

archive = Archive("http://192.168.1.50/debian")


def deps(archive, source, dsc):
    deps = dsc['Build-Depends']
    data = {
        "Build-Depends": Block(dsc['Build-Depends']).to_dict()
    }
    return data


def cayley(el, data, component, package, version):
    if el is None:
        el = ""
    data = data['Build-Depends']
    relations = data.get("relations", [])
    for relation in relations:
        targets = relation.get("targets", [])
        for target in targets:
            package_bd = target.get("package")
            el += "<{package}> <{relation}> <{target}> .\r\n".format(
                package=package,
                relation="build_depends",
                target=package_bd,
            )
    return el
from dionysus.archive import Archive
import os

archive = Archive("http://192.168.1.50/debian")



def canidate(archive, source, dsc):
    with source.unpack():
        trove_python3 = False
        if os.path.exists("setup.py"):
            buf = open("setup.py", "r").read()
            if "Programming Language :: Python :: 3" in buf:
                trove_python3 = True
    has_python2_module = False
    has_python3_module = False

    for (name, *foo) in filter(lambda x: x != [], (x.strip().split() for x in
            source.source.get('Package-List', "").split("\n"))):
        if name.startswith("python-"): has_python2_module = True
        if name.startswith("python3-"): has_python3_module = True

    return {
        "has_python2_module": has_python2_module,
        "has_python3_module": has_python3_module,
        "trove_python3": trove_python3,
        "canidate": trove_python3 is True and has_python3_module is False
    }

def is_python(source):
    return source.source['Section'] == "python"
from dionysus.archive import Archive
from dep3.parse import get_headers
import os

archive = Archive("http://192.168.1.50/debian")


def flatten(path):
    for root, dirs, files in os.walk(path):
        for dpath in dirs:
            yield from flatten(os.path.join(root, dpath))
        yield from (os.path.join(root, x) for x in files)


def report(path):
    with open(path, 'rb') as fd:
        if path.endswith(".patch") or path.endswith(".diff"):
            return get_headers(fd)


def patch(archive, source, dsc):
    print(source.source)
    with source.unpack():
        return {
            "dep3": list(map(report, flatten("debian/patches")))
        }


archive.amap(10, "unstable", "main", patch)