summarylogtreecommitdiffstats
path: root/hook.py
blob: 0037a3da929b7bf8b8ac6f9a8c5c95501e45aadc (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3

import json
import re
import sys

from subprocess import run, PIPE
from glob import glob
from os import path

from HardcodeTray.const import DB_FOLDER, USERHOME

HARDCORE_TRAY_PACKAGES = ['hardcode-tray-git', 'hardcode-tray']
BLACKLIST = ['mullvad'] # Added cause got /usr/bin in it paths

CONVERSION_TOOL = 'RSVGConvert' # None if not needed
THEME = 'Papirus-Dark' # None if not needed


class Hook:
    userhome_re = r"{userhome}"

    def __init__(self, packages):
        self.__packages = packages
        self.__db = self.read_db_files()


    def read_db_files(self):
        """ Reading all package paths """

        files = glob("{0}*.json".format(path.join(DB_FOLDER, "")))
        db = {}

        for db_file in files:
            script_name = path.splitext(path.basename(db_file))[0]

            if script_name in BLACKLIST:
                continue

            with open(db_file, 'r') as db_file:
                app_path = json.load(db_file)['app_path']
                db[script_name] = [re.sub(Hook.userhome_re, USERHOME, sctipt_path) for sctipt_path in app_path]

        return db

    def get_package_paths(self, package):
        """ Reading all package paths from pacman """

        regex = r"^%s\s+" % re.escape(package)
        proc = run(
            [
                "pacman", "-Ql",
                package
            ],
            stdout=PIPE,
            stderr=PIPE,
        )

        if proc.returncode == 0:
            output = str(proc.stdout, "utf-8").split('\n')
            return [re.sub(regex, '',line) for line in output if re.match(regex, line)]
        else:
            return []

    def get_supported_scripts(self):
        """ Finding supported scripts """

        supported_scripts = [];

        for package in self.__packages:
            package_paths = self.get_package_paths(package)
            for path in package_paths:
                for script_name, script_paths in self.__db.items():
                    if path in script_paths:
                        supported_scripts.append(script_name)

        return set(supported_scripts)

    def run_hadcode_tray(self, packages = None):
        """ Hardcode tray bit execution """

        args = ["hardcode-tray"]

        if CONVERSION_TOOL != None:
            args += ["--conversion-tool", CONVERSION_TOOL]

        if THEME != None:
            args += ["--theme", THEME]

        if packages != None:
            args += ["--only", ','.join(packages)]

        args.append("--apply")

        proc = run(
            args,
            stdout=PIPE,
            stderr=PIPE,
        )

        return (proc.returncode == 0)

    def execute(self):
        """ Execution method running hardcode-tray for supported apps"""

        for package in self.__packages:
            if package in HARDCORE_TRAY_PACKAGES:
                if self.run_hadcode_tray():
                    print("All packages are updated.")
                    return True
                else:
                    print("Packages update failed.")
                    return False

        packages_for_update = self.get_supported_scripts()

        if len(packages_for_update) == 0:
            print("No packages for update has been found.")
            return True
        else:
            if self.run_hadcode_tray(packages_for_update):
                print("Packages: %s - updated." % ', '.join(packages_for_update))
                return True
            else:
                print("Packages: %s - failed." % ', '.join(packages_for_update))
                return False

        return True

input_str = sys.stdin.read()
packages = [x for x in input_str.split('\n') if x]
hook = Hook(packages)

if not hook.execute():
    sys.exit('Hook failed')