xref: /aosp_15_r20/external/pigweed/pw_env_setup/py/pw_env_setup/batch_visitor.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Serializes an Environment into a batch file."""
15
16# goto label written to the end of Windows batch files for exiting a script.
17_SCRIPT_END_LABEL = '_pw_end'
18
19
20class BatchVisitor:
21    """Serializes an Environment into a batch file."""
22
23    def __init__(self, *args, **kwargs):
24        pathsep = kwargs.pop('pathsep', ':')
25        super().__init__(*args, **kwargs)
26        self._replacements = ()
27        self._outs = None
28        self._pathsep = pathsep
29
30    def serialize(self, env, outs):
31        """Write a batch file based on the given environment.
32
33        Args:
34            env (environment.Environment): Environment variables to use.
35            outs (file): Batch file to write.
36        """
37        try:
38            self._replacements = tuple(
39                (key, env.get(key) if value is None else value)
40                for key, value in env.replacements
41            )
42            self._outs = outs
43            self._outs.write('@echo off\n')
44
45            env.accept(self)
46
47            outs.write(':{}\n'.format(_SCRIPT_END_LABEL))
48
49        finally:
50            self._replacements = ()
51            self._outs = None
52
53    def _apply_replacements(self, action):
54        value = action.value
55        for var, replacement in self._replacements:
56            if var != action.name:
57                value = value.replace(replacement, '%{}%'.format(var))
58        return value
59
60    def visit_set(self, set):  # pylint: disable=redefined-builtin
61        value = self._apply_replacements(set)
62        self._outs.write(
63            'set {name}={value}\n'.format(name=set.name, value=value)
64        )
65
66    def visit_clear(self, clear):
67        self._outs.write('set {name}=\n'.format(name=clear.name))
68
69    def visit_remove(self, remove):
70        pass  # Not supported on Windows.
71
72    def _join(self, *args):
73        if len(args) == 1 and isinstance(args[0], (list, tuple)):
74            args = args[0]
75        return self._pathsep.join(args)
76
77    def visit_prepend(self, prepend):
78        value = self._apply_replacements(prepend)
79        value = self._join(value, '%{}%'.format(prepend.name))
80        self._outs.write(
81            'set {name}={value}\n'.format(name=prepend.name, value=value)
82        )
83
84    def visit_append(self, append):
85        value = self._apply_replacements(append)
86        value = self._join('%{}%'.format(append.name), value)
87        self._outs.write(
88            'set {name}={value}\n'.format(name=append.name, value=value)
89        )
90
91    def visit_echo(self, echo):
92        if echo.newline:
93            if not echo.value:
94                self._outs.write('echo.\n')
95            else:
96                self._outs.write('echo {}\n'.format(echo.value))
97        else:
98            self._outs.write('<nul set /p="{}"\n'.format(echo.value))
99
100    def visit_comment(self, comment):
101        for line in comment.value.splitlines():
102            self._outs.write(':: {}\n'.format(line))
103
104    def visit_command(self, command):
105        # TODO(mohrr) use shlex.quote here?
106        self._outs.write('{}\n'.format(' '.join(command.command)))
107        if not command.exit_on_error:
108            return
109
110        # Assume failing command produced relevant output.
111        self._outs.write(
112            'if %ERRORLEVEL% neq 0 goto {}\n'.format(_SCRIPT_END_LABEL)
113        )
114
115    def visit_doctor(self, doctor):
116        self._outs.write('if "%PW_ACTIVATE_SKIP_CHECKS%"=="" (\n')
117        self.visit_command(doctor)
118        self._outs.write(') else (\n')
119        self._outs.write(
120            'echo Skipping environment check because '
121            'PW_ACTIVATE_SKIP_CHECKS is set\n'
122        )
123        self._outs.write(')\n')
124
125    def visit_blank_line(self, blank_line):
126        del blank_line
127        self._outs.write('\n')
128
129    def visit_function(self, function):
130        pass  # Not supported on Windows.
131
132    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
133        pass  # Not relevant on Windows.
134