Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """Install all the required Python packages, with the minimum Python version. |
| 3 | """ |
| 4 | |
| 5 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 7 | |
| 8 | import argparse |
| 9 | import os |
| 10 | import re |
Gilles Peskine | c31780f | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 11 | import subprocess |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 12 | import sys |
Gilles Peskine | c31780f | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 13 | import tempfile |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 14 | import typing |
| 15 | |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 16 | from typing import List, Optional |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 17 | from mbedtls_dev import typing_util |
| 18 | |
| 19 | def pylint_doesn_t_notice_that_certain_types_are_used_in_annotations( |
| 20 | _list: List[typing.Any], |
| 21 | ) -> None: |
| 22 | pass |
| 23 | |
| 24 | |
| 25 | class Requirements: |
| 26 | """Collect and massage Python requirements.""" |
| 27 | |
| 28 | def __init__(self) -> None: |
| 29 | self.requirements = [] #type: List[str] |
| 30 | |
| 31 | def adjust_requirement(self, req: str) -> str: |
| 32 | """Adjust a requirement to the minimum specified version.""" |
| 33 | # allow inheritance #pylint: disable=no-self-use |
| 34 | # If a requirement specifies a minimum version, impose that version. |
| 35 | req = re.sub(r'>=|~=', r'==', req) |
| 36 | return req |
| 37 | |
| 38 | def add_file(self, filename: str) -> None: |
| 39 | """Add requirements from the specified file. |
| 40 | |
| 41 | This method supports a subset of pip's requirement file syntax: |
| 42 | * One requirement specifier per line, which is passed to |
| 43 | `adjust_requirement`. |
| 44 | * Comments (``#`` at the beginning of the line or after whitespace). |
| 45 | * ``-r FILENAME`` to include another file. |
| 46 | """ |
Gilles Peskine | aeb8d66 | 2022-03-04 20:02:00 +0100 | [diff] [blame] | 47 | with open(filename) as fd: |
| 48 | for line in fd: |
| 49 | line = line.strip() |
| 50 | line = re.sub(r'(\A|\s+)#.*', r'', line) |
| 51 | if not line: |
| 52 | continue |
| 53 | m = re.match(r'-r\s+', line) |
| 54 | if m: |
| 55 | nested_file = os.path.join(os.path.dirname(filename), |
| 56 | line[m.end(0):]) |
| 57 | self.add_file(nested_file) |
| 58 | continue |
| 59 | self.requirements.append(self.adjust_requirement(line)) |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 60 | |
| 61 | def write(self, out: typing_util.Writable) -> None: |
| 62 | """List the gathered requirements.""" |
| 63 | for req in self.requirements: |
| 64 | out.write(req + '\n') |
| 65 | |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 66 | def install( |
| 67 | self, |
| 68 | pip_general_options: Optional[List[str]] = None, |
| 69 | pip_install_options: Optional[List[str]] = None, |
| 70 | ) -> None: |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 71 | """Call pip to install the requirements.""" |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 72 | if pip_general_options is None: |
| 73 | pip_general_options = [] |
| 74 | if pip_install_options is None: |
| 75 | pip_install_options = [] |
Gilles Peskine | c31780f | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 76 | with tempfile.TemporaryDirectory() as temp_dir: |
| 77 | # This is more complicated than it needs to be for the sake |
| 78 | # of Windows. Use a temporary file rather than the command line |
| 79 | # to avoid quoting issues. Use a temporary directory rather |
| 80 | # than NamedTemporaryFile because with a NamedTemporaryFile on |
| 81 | # Windows, the subprocess can't open the file because this process |
| 82 | # has an exclusive lock on it. |
| 83 | req_file_name = os.path.join(temp_dir, 'requirements.txt') |
| 84 | with open(req_file_name, 'w') as req_file: |
| 85 | self.write(req_file) |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 86 | subprocess.check_call([sys.executable, '-m', 'pip'] + |
| 87 | pip_general_options + |
| 88 | ['install'] + pip_install_options + |
| 89 | ['-r', req_file_name]) |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 90 | |
Gilles Peskine | 4b71e9b | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 91 | DEFAULT_REQUIREMENTS_FILE = 'ci.requirements.txt' |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 92 | |
| 93 | def main() -> None: |
| 94 | """Command line entry point.""" |
| 95 | parser = argparse.ArgumentParser(description=__doc__) |
| 96 | parser.add_argument('--no-act', '-n', |
| 97 | action='store_true', |
| 98 | help="Don't act, just print what will be done") |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 99 | parser.add_argument('--pip-install-option', |
| 100 | action='append', dest='pip_install_options', |
| 101 | help="Pass this option to pip install") |
| 102 | parser.add_argument('--pip-option', |
| 103 | action='append', dest='pip_general_options', |
| 104 | help="Pass this general option to pip") |
| 105 | parser.add_argument('--user', |
| 106 | action='append_const', dest='pip_install_options', |
| 107 | const='--user', |
| 108 | help="Install to the Python user install directory" |
| 109 | " (short for --pip-install-option --user)") |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 110 | parser.add_argument('files', nargs='*', metavar='FILE', |
| 111 | help="Requirement files" |
Gilles Peskine | 4b71e9b | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 112 | " (default: {} in the script's directory)" \ |
| 113 | .format(DEFAULT_REQUIREMENTS_FILE)) |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 114 | options = parser.parse_args() |
| 115 | if not options.files: |
| 116 | options.files = [os.path.join(os.path.dirname(__file__), |
Gilles Peskine | 4b71e9b | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 117 | DEFAULT_REQUIREMENTS_FILE)] |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 118 | reqs = Requirements() |
| 119 | for filename in options.files: |
| 120 | reqs.add_file(filename) |
| 121 | reqs.write(sys.stdout) |
| 122 | if not options.no_act: |
Gilles Peskine | ca07ea0 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 123 | reqs.install(pip_general_options=options.pip_general_options, |
| 124 | pip_install_options=options.pip_install_options) |
Gilles Peskine | e4d142f | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 125 | |
| 126 | if __name__ == '__main__': |
| 127 | main() |