build_tree.py 1.4 KB

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