| Gilles Peskine | 9172c9c | 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 | 
|  | 6 | # SPDX-License-Identifier: Apache-2.0 | 
|  | 7 | # | 
|  | 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 9 | # not use this file except in compliance with the License. | 
|  | 10 | # You may obtain a copy of the License at | 
|  | 11 | # | 
|  | 12 | # http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 13 | # | 
|  | 14 | # Unless required by applicable law or agreed to in writing, software | 
|  | 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 17 | # See the License for the specific language governing permissions and | 
|  | 18 | # limitations under the License. | 
|  | 19 |  | 
|  | 20 | import argparse | 
|  | 21 | import os | 
|  | 22 | import re | 
| Gilles Peskine | 8f63f6d | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 23 | import subprocess | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 24 | import sys | 
| Gilles Peskine | 8f63f6d | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 25 | import tempfile | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 26 | import typing | 
|  | 27 |  | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 28 | from typing import List, Optional | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 29 | from mbedtls_dev import typing_util | 
|  | 30 |  | 
|  | 31 | def pylint_doesn_t_notice_that_certain_types_are_used_in_annotations( | 
|  | 32 | _list: List[typing.Any], | 
|  | 33 | ) -> None: | 
|  | 34 | pass | 
|  | 35 |  | 
|  | 36 |  | 
|  | 37 | class Requirements: | 
|  | 38 | """Collect and massage Python requirements.""" | 
|  | 39 |  | 
|  | 40 | def __init__(self) -> None: | 
|  | 41 | self.requirements = [] #type: List[str] | 
|  | 42 |  | 
|  | 43 | def adjust_requirement(self, req: str) -> str: | 
|  | 44 | """Adjust a requirement to the minimum specified version.""" | 
|  | 45 | # allow inheritance #pylint: disable=no-self-use | 
|  | 46 | # If a requirement specifies a minimum version, impose that version. | 
|  | 47 | req = re.sub(r'>=|~=', r'==', req) | 
|  | 48 | return req | 
|  | 49 |  | 
|  | 50 | def add_file(self, filename: str) -> None: | 
|  | 51 | """Add requirements from the specified file. | 
|  | 52 |  | 
|  | 53 | This method supports a subset of pip's requirement file syntax: | 
|  | 54 | * One requirement specifier per line, which is passed to | 
|  | 55 | `adjust_requirement`. | 
|  | 56 | * Comments (``#`` at the beginning of the line or after whitespace). | 
|  | 57 | * ``-r FILENAME`` to include another file. | 
|  | 58 | """ | 
|  | 59 | for line in open(filename): | 
|  | 60 | line = line.strip() | 
|  | 61 | line = re.sub(r'(\A|\s+)#.*', r'', line) | 
|  | 62 | if not line: | 
|  | 63 | continue | 
|  | 64 | m = re.match(r'-r\s+', line) | 
|  | 65 | if m: | 
|  | 66 | nested_file = os.path.join(os.path.dirname(filename), | 
|  | 67 | line[m.end(0):]) | 
|  | 68 | self.add_file(nested_file) | 
|  | 69 | continue | 
|  | 70 | self.requirements.append(self.adjust_requirement(line)) | 
|  | 71 |  | 
|  | 72 | def write(self, out: typing_util.Writable) -> None: | 
|  | 73 | """List the gathered requirements.""" | 
|  | 74 | for req in self.requirements: | 
|  | 75 | out.write(req + '\n') | 
|  | 76 |  | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 77 | def install( | 
|  | 78 | self, | 
|  | 79 | pip_general_options: Optional[List[str]] = None, | 
|  | 80 | pip_install_options: Optional[List[str]] = None, | 
|  | 81 | ) -> None: | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 82 | """Call pip to install the requirements.""" | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 83 | if pip_general_options is None: | 
|  | 84 | pip_general_options = [] | 
|  | 85 | if pip_install_options is None: | 
|  | 86 | pip_install_options = [] | 
| Gilles Peskine | 8f63f6d | 2021-11-18 18:18:35 +0100 | [diff] [blame] | 87 | with tempfile.TemporaryDirectory() as temp_dir: | 
|  | 88 | # This is more complicated than it needs to be for the sake | 
|  | 89 | # of Windows. Use a temporary file rather than the command line | 
|  | 90 | # to avoid quoting issues. Use a temporary directory rather | 
|  | 91 | # than NamedTemporaryFile because with a NamedTemporaryFile on | 
|  | 92 | # Windows, the subprocess can't open the file because this process | 
|  | 93 | # has an exclusive lock on it. | 
|  | 94 | req_file_name = os.path.join(temp_dir, 'requirements.txt') | 
|  | 95 | with open(req_file_name, 'w') as req_file: | 
|  | 96 | self.write(req_file) | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 97 | subprocess.check_call([sys.executable, '-m', 'pip'] + | 
|  | 98 | pip_general_options + | 
|  | 99 | ['install'] + pip_install_options + | 
|  | 100 | ['-r', req_file_name]) | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 101 |  | 
| Gilles Peskine | 3d57afe | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 102 | DEFAULT_REQUIREMENTS_FILE = 'ci.requirements.txt' | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 103 |  | 
|  | 104 | def main() -> None: | 
|  | 105 | """Command line entry point.""" | 
|  | 106 | parser = argparse.ArgumentParser(description=__doc__) | 
|  | 107 | parser.add_argument('--no-act', '-n', | 
|  | 108 | action='store_true', | 
|  | 109 | help="Don't act, just print what will be done") | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 110 | parser.add_argument('--pip-install-option', | 
|  | 111 | action='append', dest='pip_install_options', | 
|  | 112 | help="Pass this option to pip install") | 
|  | 113 | parser.add_argument('--pip-option', | 
|  | 114 | action='append', dest='pip_general_options', | 
|  | 115 | help="Pass this general option to pip") | 
|  | 116 | parser.add_argument('--user', | 
|  | 117 | action='append_const', dest='pip_install_options', | 
|  | 118 | const='--user', | 
|  | 119 | help="Install to the Python user install directory" | 
|  | 120 | " (short for --pip-install-option --user)") | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 121 | parser.add_argument('files', nargs='*', metavar='FILE', | 
|  | 122 | help="Requirement files" | 
| Gilles Peskine | 3d57afe | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 123 | " (default: {} in the script's directory)" \ | 
|  | 124 | .format(DEFAULT_REQUIREMENTS_FILE)) | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 125 | options = parser.parse_args() | 
|  | 126 | if not options.files: | 
|  | 127 | options.files = [os.path.join(os.path.dirname(__file__), | 
| Gilles Peskine | 3d57afe | 2021-12-03 13:32:10 +0100 | [diff] [blame] | 128 | DEFAULT_REQUIREMENTS_FILE)] | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 129 | reqs = Requirements() | 
|  | 130 | for filename in options.files: | 
|  | 131 | reqs.add_file(filename) | 
|  | 132 | reqs.write(sys.stdout) | 
|  | 133 | if not options.no_act: | 
| Gilles Peskine | 7f29ea6 | 2021-11-22 12:52:24 +0000 | [diff] [blame] | 134 | reqs.install(pip_general_options=options.pip_general_options, | 
|  | 135 | pip_install_options=options.pip_install_options) | 
| Gilles Peskine | 9172c9c | 2021-11-17 19:25:43 +0100 | [diff] [blame] | 136 |  | 
|  | 137 | if __name__ == '__main__': | 
|  | 138 | main() |