• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# Disable super() warnings since this file must be Python 2 compatible.
17# pylint: disable=super-with-arguments
18
19# goto label written to the end of Windows batch files for exiting a script.
20_SCRIPT_END_LABEL = '_pw_end'
21
22
23class BatchVisitor(object):  # pylint: disable=useless-object-inheritance
24    """Serializes an Environment into a batch file."""
25
26    def __init__(self, *args, **kwargs):
27        pathsep = kwargs.pop('pathsep', ':')
28        super(BatchVisitor, self).__init__(*args, **kwargs)
29        self._replacements = ()
30        self._outs = None
31        self._pathsep = pathsep
32
33    def serialize(self, env, outs):
34        try:
35            self._replacements = tuple(
36                (key, env.get(key) if value is None else value)
37                for key, value in env.replacements
38            )
39            self._outs = outs
40            self._outs.write('@echo off\n')
41
42            env.accept(self)
43
44            outs.write(':{}\n'.format(_SCRIPT_END_LABEL))
45
46        finally:
47            self._replacements = ()
48            self._outs = None
49
50    def _apply_replacements(self, action):
51        value = action.value
52        for var, replacement in self._replacements:
53            if var != action.name:
54                value = value.replace(replacement, '%{}%'.format(var))
55        return value
56
57    def visit_set(self, set):  # pylint: disable=redefined-builtin
58        value = self._apply_replacements(set)
59        self._outs.write(
60            'set {name}={value}\n'.format(name=set.name, value=value)
61        )
62
63    def visit_clear(self, clear):
64        self._outs.write('set {name}=\n'.format(name=clear.name))
65
66    def visit_remove(self, remove):
67        pass  # Not supported on Windows.
68
69    def _join(self, *args):
70        if len(args) == 1 and isinstance(args[0], (list, tuple)):
71            args = args[0]
72        return self._pathsep.join(args)
73
74    def visit_prepend(self, prepend):
75        value = self._apply_replacements(prepend)
76        value = self._join(value, '%{}%'.format(prepend.name))
77        self._outs.write(
78            'set {name}={value}\n'.format(name=prepend.name, value=value)
79        )
80
81    def visit_append(self, append):
82        value = self._apply_replacements(append)
83        value = self._join('%{}%'.format(append.name), value)
84        self._outs.write(
85            'set {name}={value}\n'.format(name=append.name, value=value)
86        )
87
88    def visit_echo(self, echo):
89        if echo.newline:
90            if not echo.value:
91                self._outs.write('echo.\n')
92            else:
93                self._outs.write('echo {}\n'.format(echo.value))
94        else:
95            self._outs.write('<nul set /p="{}"\n'.format(echo.value))
96
97    def visit_comment(self, comment):
98        for line in comment.value.splitlines():
99            self._outs.write(':: {}\n'.format(line))
100
101    def visit_command(self, command):
102        # TODO(mohrr) use shlex.quote here?
103        self._outs.write('{}\n'.format(' '.join(command.command)))
104        if not command.exit_on_error:
105            return
106
107        # Assume failing command produced relevant output.
108        self._outs.write(
109            'if %ERRORLEVEL% neq 0 goto {}\n'.format(_SCRIPT_END_LABEL)
110        )
111
112    def visit_doctor(self, doctor):
113        self._outs.write('if "%PW_ACTIVATE_SKIP_CHECKS%"=="" (\n')
114        self.visit_command(doctor)
115        self._outs.write(') else (\n')
116        self._outs.write(
117            'echo Skipping environment check because '
118            'PW_ACTIVATE_SKIP_CHECKS is set\n'
119        )
120        self._outs.write(')\n')
121
122    def visit_blank_line(self, blank_line):
123        del blank_line
124        self._outs.write('\n')
125
126    def visit_function(self, function):
127        pass  # Not supported on Windows.
128
129    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
130        pass  # Not relevant on Windows.
131