Untitled

mail@pastecode.io avatar
unknown
python
a month ago
3.8 kB
1
Indexable
Never
from re import findall, search


class Dependency:
    def __init__(self, dependency):
        dependency = dependency.split()
        self.package_name = dependency[0]
        self.package_version = [int(v) for v in dependency[-1].split(".")]
        self.operator = dependency[1]

    def compare(self, version):
        match self.operator:
            case ">>":
                return version > self.package_version
            case ">=":
                return version >= self.package_version
            case "==":
                return version == self.package_version
            case "~=":
                return (version[:2] == self.package_version[:2]) and (version[-1] >= self.package_version[-1])
            case "^=":
                return (version[0] == self.package_version[0]) and (version[1:] >= self.package_version[1:])

    def __repr__(self):
        return f"{self.package_name} {self.operator} {self.package_version}"

    def __lt__(self, other):
        return self.package_version < other.package_version


class Version:
    def __init__(self, version, dependencies: list[Dependency], size):
        self.version = [int(v) for v in version.split('.')]
        self.dependencies = dependencies
        self.size = size

    def __repr__(self):
        return f"{self.version}, size: {self.size}\n {self.dependencies}"


class Package:
    def __init__(self, name, versions: list[Version]):
        self.name = name
        self.versions = versions

    def __repr__(self):
        return f"{self.name} {self.versions}"


def get_packages(filename):
    package_pattern = r"Package: .*"
    full_version_pattern = "Version: .*\nDepends:.*\nSize: .*"
    version_pattern = "Version: .*"
    depends_pattern = "Depends: .*"
    size_pattern = "Size: .*"

    with open(filename) as file:
        text = file.read()

    packages = []
    for package_text in text.split("\n\n"):
        package_name = search(package_pattern, package_text).group().split()[-1]

        versions = []
        for ver in findall(full_version_pattern, package_text):
            version = search(version_pattern, ver).group().split()[-1]
            depends = search(depends_pattern, ver)
            size = int(search(size_pattern, ver).group().split()[-1])

            if not depends:
                versions.append(Version(version, None, size))
            else:
                depends = depends.group().split(": ")[-1]
                dependencies = [Dependency(dependency) for dependency in depends.split(", ")]
                versions.append(Version(version, dependencies, size))

        packages.append(Package(package_name, versions))

    return packages


packages = get_packages("task6.txt")

needed_packages = [
    "gdisk == 1.0.8",
    "libc6 == 2.34.5",
    "libgcc-s1 == 12.2.8",
    "gcc-12-base == 12.2.8",
    "libcrypt1 == 4.4.27",
    "libncursesw6 == 6.3.2",
    "libtinfo6 == 6.3.2",
    "libpopt0 == 1.18.3",
    "libstdc++6 == 12.2.8",
    "libuuid1 == 2.37.2"
    ]


needed_packages = [Dependency(package) for package in needed_packages]
result = 0
found = set()
for package in packages:
    flag = False

    for version in package.versions:
        if flag:
            break
        for n_package in needed_packages:
            if n_package.package_name == package.name and n_package.compare(version.version):
                result += version.size
                flag = True
                found.add(package.name)
                break


needed_packages = [p.package_name for p in needed_packages]
if found == set(needed_packages):
    print("All packages found successfully!")
    print(len(needed_packages), result)
else:
    print("Something went wrong :(")
    print(f"Packages found: {sorted(found)}")
    print(f"Packages needed: {sorted(needed_packages)}")
    print(len(needed_packages), result)
Leave a Comment