aboutsummaryrefslogtreecommitdiffstats
path: root/filter_plugins/filters.py
blob: 168a4f7c0308f3509c3520345ceae639d26de890 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/python

import requests

class FilterModule(object):

    def filters(self):
        return {
            "repology": self.repology_filter
        }

    def repology_filter(self, package, distribution):
        """
        Use repology.org API for getting generic package names accrossed different Unix systems.
        This allows us to use standard package names, and execute install tasks with a system-agnostic way.
        """
        api_endpoint = "https://repology.org/api/v1/project/"
        possible_keys = ["name", "binname", "srcname"]
        package_key = ""

        # Retrieve all packages
        responses = requests.get(f"{api_endpoint}/{package}").json()
        valid_packages = []

        # Sort packages corresponding for the given
        # distribution and name
        for response in responses:
            print(response)

            for possible_key in possible_keys:
                try:
                    response[possible_key]
                except KeyError:
                    continue
                package_key = possible_key

            if package_key == "":
                continue

            is_not_documentation = "doc" not in package and "doc" not in response[package_key]
            is_valid_package = package in response[package_key]
            is_valid_repo = distribution in response["repo"]

            if is_not_documentation and is_valid_package and is_valid_repo:
                valid_packages.append(response[package_key])

        # Try to the guess best package
        if len(valid_packages) > 1:
            guessed_packages = []
            for valid in valid_packages:
                # package name matches exactly
                if valid == package:
                    return [package]


        return valid_packages
remember that computers suck.