๐Ÿ“ฆ felixonmars / archlinux-futils

๐Ÿ“„ qmlanalyze ยท 68 lines
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#!/usr/bin/python
import sys
from sh import find, pacman

qtdir = {
    "5": "usr/lib/qt",
    "6": "usr/lib/qt6"
}[sys.argv[1] if len(sys.argv) > 1 else "5"]

qmlmap = {}
package = None
for line in pacman("-F", "qmldir"):
    # pacman outputs ANSI escape sequences
    if line.startswith("\x1b[?25l"):
        line = line[6:]
    line = line.strip()
    if line.endswith("/qmldir"):
        if line.startswith(f"{qtdir}/qml/"):
            module = line[len(f"{qtdir}/qml/"):-len("/qmldir")].replace("/", ".")
            qmlmap[module] = package
    else:
        package = line.split()[0]

for m in qmlmap.copy():
    if "." in m and m.split(".")[-1].isnumeric():
        new_m = ".".join(m.split(".")[:-1])
        if new_m not in qmlmap:
            qmlmap[new_m] = qmlmap[m]

packages = set()
for file in find("." if len(sys.argv) < 3 else sys.argv[2], "-name", "*.qml"):
    file = file.strip()
    print(f"Analyzing {file}...")
    with open(file) as f:
        for line in f:
            if line.startswith("import "):
                line = line.split("import ")[1].split()
                if len(line) == 1:
                    module_name = module = line[0]
                    if module_name.startswith(('"', "'")):
                        # skip directory imports
                        continue
                    
                    if module_name in qmlmap:
                        print(f"{module_name} found in {qmlmap[module_name]}")
                        packages.add(qmlmap[module_name])

                else:
                    module = line[0]
                    version = " " + line[1]
                
                    while True:
                        module_name = (module + version).replace(" ", ".")
                        if module_name in qmlmap:
                            print(f"{module_name} found in {qmlmap[module_name]}")
                            packages.add(qmlmap[module_name])
                            break
                        elif "." in version:
                            version = ".".join(version.split(".")[:-1])
                        elif version:
                            version = ""
                        else:
                            print(f"Unknown module: {module}")
                            break

print("\nFound dependencies:")
print("\n".join(packages))