xref: /aosp_15_r20/external/pigweed/pw_env_setup/py/pw_env_setup/windows_env_start.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# -*- coding: utf-8 -*-
2
3# Copyright 2020 The Pigweed Authors
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9#     https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""Prints the env_setup banner for cmd.exe.
17
18This is done from Python as activating colors and printing ASCII art are not
19easy to do in cmd.exe. Activated colors also don't persist in the parent
20process.
21"""
22
23import argparse
24import os
25import sys
26
27try:
28    from pw_env_setup.colors import Color, enable_colors
29except ImportError:
30    # Load from this directory if pw_env_setup is not available.
31    from colors import Color, enable_colors  # type: ignore
32
33
34_PIGWEED_BANNER = u'''
35 ▒█████▄   █▓  ▄███▒  ▒█    ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
36  ▒█░  █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█  ▒█   ▀  ▒█   ▀  ▒█  ▀█▌
37  ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█  ▒███    ▒███    ░█   █▌
38  ▒█▀     ░█░ ▓█   █▓ ░█░ █ ▒█  ▒█   ▄  ▒█   ▄  ░█  ▄█▌
39  ▒█      ░█░ ░▓███▀   ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
40'''
41
42
43def print_banner(bootstrap, no_shell_file):
44    """Print the Pigweed or project-specific banner"""
45    enable_colors()
46
47    print(Color.green('\n  WELCOME TO...'))
48
49    banner_file = os.environ.get('PW_BRANDING_BANNER', None)
50    banner_str = None
51    if banner_file:
52        try:
53            banner_str = open(
54                banner_file, 'r', encoding='utf-8', errors='replace'
55            ).read()
56        except FileNotFoundError:
57            pass
58    if banner_str:
59        print()
60        print(banner_str, end='')
61    else:
62        print(Color.magenta(_PIGWEED_BANNER), end='')
63
64    if bootstrap:
65        print(
66            Color.green(
67                '\n  BOOTSTRAP! Bootstrap may take a few minutes; '
68                'please be patient'
69            )
70        )
71        print(
72            Color.green(
73                '  On Windows, this stage is extremely slow (~10 minutes).\n'
74            )
75        )
76    else:
77        print(
78            Color.green(
79                '\n  ACTIVATOR! This sets your console environment variables.\n'
80            )
81        )
82
83        if no_shell_file:
84            print(Color.bold_red('Error!\n'))
85            print(
86                Color.red(
87                    '  Your Pigweed environment does not seem to be'
88                    ' configured.'
89                )
90            )
91            print(Color.red('  Run bootstrap.bat to perform initial setup.'))
92
93    return 0
94
95
96def parse():
97    """Parse command-line arguments."""
98    parser = argparse.ArgumentParser(
99        prog="python -m pw_env_setup.windows_env_start"
100    )
101    parser.add_argument('--bootstrap', action='store_true')
102    parser.add_argument('--no-shell-file', action='store_true')
103    return parser.parse_args()
104
105
106def main():
107    """Script entry point."""
108    if os.name != 'nt':
109        return 1
110    return print_banner(**vars(parse()))
111
112
113if __name__ == '__main__':
114    sys.exit(main())
115