summaryrefslogtreecommitdiffstats
path: root/zenmap/install_scripts/utils/get_deps.py
blob: 631bae60b6d2ece617a20565cfd1e88c99bb9b95 (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
#!/usr/bin/env python3

import modulefinder
import configparser
import sys
import os.path
import site
import encodings

site_package_deps = ("gi", "cairo")

# These items are unneeded, large, and on macOS _ssl causes dependency problems.
pyd_remove = ("_decimal", "_ssl", "_testcapi", "_hashlib")

def module_paths(mods):
    for m in mods:
        if m.__name__ in pyd_remove:
            continue
        elif getattr(m, "__file__", None) and m.__file__.startswith(sys.prefix):
            yield m.__file__

def get_deps():
    # Start with pygobject and zenmap itself
    sitedirs = site.getsitepackages()
    files = set(os.path.join(sitedirs[0], name) for name in site_package_deps)

    # These items are missed by modulefinder
    files.add(encodings.__path__[0]) # All encodings just in case
    for path in module_paths((site, site._sitebuiltins)):
        files.add(path)

    # Now use modulefinder to get the rest
    mfind = modulefinder.ModuleFinder()
    mfind.run_script(os.path.normpath(__file__ + '/../../../zenmapGUI/App.py'))
    for path in module_paths(mfind.modules.values()):
        parent = os.path.dirname(path)
        found_parent = False
        # If a parent dir is already included, don't bother listing the file.
        while parent not in sys.path and len(parent) > 2:
            if parent in files:
                found_parent = True
                break
            parent = os.path.dirname(parent)
        if not found_parent:
            files.add(path)
    return files

def read_cfg(filename):
    cfg = configparser.ConfigParser()
    cfg.read(filename)
    return cfg

def write_cfg(cfg, filename):
    with open(filename, "w") as f:
        cfg.write(f)

def update_cfg(cfg, files):
    filestr = "\nmingw*".join((f.removeprefix(sys.prefix) for f in files))
    oldvalue = cfg.get('bundle', 'nodelete')
    cfg.set('bundle', 'nodelete', oldvalue + "\nmingw*" + filestr)

def write_xml(filename, files):
    with open(filename, "w") as f:
        for file in files:
            fname = r"${prefix}" + file.removeprefix(sys.prefix)
            fmt = "<data>{}</data>"
            if file.endswith(".so"):
                fmt = "<binary>{}</binary>"
            print(fmt.format(fname), file=f)

if __name__ == "__main__":
    files = get_deps()
    if sys.platform == "win32":
        cfg = read_cfg(sys.argv[2])
        update_cfg(cfg, files)
        write_cfg(cfg, sys.argv[1])
    elif sys.platform == "darwin":
        write_xml(sys.argv[1], files)
    else:
        raise NotImplementedError