Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 1 | """Mbed TLS build tree information and manipulation. |
| 2 | """ |
| 3 | |
| 4 | # Copyright The Mbed TLS Contributors |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | |
| 19 | import os |
| 20 | |
| 21 | def looks_like_mbedtls_root(path: str) -> bool: |
| 22 | """Whether the given directory looks like the root of the Mbed TLS source tree.""" |
| 23 | return all(os.path.isdir(os.path.join(path, subdir)) |
| 24 | for subdir in ['include', 'library', 'programs', 'tests']) |
| 25 | |
| 26 | def chdir_to_root() -> None: |
| 27 | """Detect the root of the Mbed TLS source tree and change to it. |
| 28 | |
| 29 | The current directory must be up to two levels deep inside an Mbed TLS |
| 30 | source tree. |
| 31 | """ |
| 32 | for d in [os.path.curdir, |
| 33 | os.path.pardir, |
| 34 | os.path.join(os.path.pardir, os.path.pardir)]: |
| 35 | if looks_like_mbedtls_root(d): |
| 36 | os.chdir(d) |
| 37 | return |
| 38 | raise Exception('Mbed TLS source tree not found') |