aboutsummaryrefslogtreecommitdiff
path: root/tools/python/maps_generator/generator/osmtools.py
blob: 0132077fc0987d014f1802520ba749be784d6345 (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
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
import os
import subprocess

from maps_generator.generator import settings
from maps_generator.generator.exceptions import BadExitStatusError
from maps_generator.generator.exceptions import wait_and_raise_if_fail


def build_osmtools(path, output=subprocess.DEVNULL, error=subprocess.DEVNULL):
    src = {
        settings.OSM_TOOL_UPDATE: "osmupdate.c",
        settings.OSM_TOOL_FILTER: "osmfilter.c",
        settings.OSM_TOOL_CONVERT: "osmconvert.c",
    }
    ld_flags = ("-lz",)
    cc = []
    result = {}
    for executable, src in src.items():
        out = os.path.join(settings.OSM_TOOLS_PATH, executable)
        op = [
            settings.OSM_TOOLS_CC,
            *settings.OSM_TOOLS_CC_FLAGS,
            "-o",
            out,
            os.path.join(path, src),
            *ld_flags,
        ]
        s = subprocess.Popen(op, stdout=output, stderr=error)
        cc.append(s)
        result[executable] = out

    messages = []
    for c in cc:
        if c.wait() != os.EX_OK:
            messages.append(f"The launch of {' '.join(c.args)} failed.")
    if messages:
        raise BadExitStatusError("\n".split(messages))

    return result


def osmconvert(
    name_executable,
    in_file,
    out_file,
    output=subprocess.DEVNULL,
    error=subprocess.DEVNULL,
    run_async=False,
    **kwargs,
):
    env = os.environ.copy()
    env["PATH"] = f"{settings.OSM_TOOLS_PATH}:{env['PATH']}"
    p = subprocess.Popen(
        [
            name_executable,
            in_file,
            "--drop-author",
            "--drop-version",
            "--out-o5m",
            f"-o={out_file}",
        ],
        env=env,
        stdout=output,
        stderr=error,
    )
    if run_async:
        return p
    else:
        wait_and_raise_if_fail(p)


def osmupdate(
    name_executable,
    in_file,
    out_file,
    output=subprocess.DEVNULL,
    error=subprocess.DEVNULL,
    run_async=False,
    **kwargs,
):
    env = os.environ.copy()
    env["PATH"] = f"{settings.OSM_TOOLS_PATH}:{env['PATH']}"
    p = subprocess.Popen(
        [
            name_executable,
            "--drop-author",
            "--drop-version",
            "--out-o5m",
            "-v",
            in_file,
            out_file,
        ],
        env=env,
        stdout=output,
        stderr=error,
    )
    if run_async:
        return p
    else:
        wait_and_raise_if_fail(p)


def osmfilter(
    name_executable,
    in_file,
    out_file,
    output=subprocess.DEVNULL,
    error=subprocess.DEVNULL,
    run_async=False,
    **kwargs,
):
    env = os.environ.copy()
    env["PATH"] = f"{settings.OSM_TOOLS_PATH}:{env['PATH']}"
    args = [name_executable, in_file, f"-o={out_file}"] + [
        f"--{k.replace('_', '-')}={v}" for k, v in kwargs.items()
    ]
    p = subprocess.Popen(args, env=env, stdout=output, stderr=error)
    if run_async:
        return p
    else:
        wait_and_raise_if_fail(p)