idf_ci_utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # internal use only for CI
  2. # some CI related util functions
  3. #
  4. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may 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,
  14. # WITHOUT 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. #
  18. import logging
  19. import os
  20. import subprocess
  21. import sys
  22. IDF_PATH = os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..'))
  23. def get_submodule_dirs(full_path=False): # type: (bool) -> list
  24. """
  25. To avoid issue could be introduced by multi-os or additional dependency,
  26. we use python and git to get this output
  27. :return: List of submodule dirs
  28. """
  29. dirs = []
  30. try:
  31. lines = subprocess.check_output(
  32. ['git', 'config', '--file', os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')),
  33. '--get-regexp', 'path']).decode('utf8').strip().split('\n')
  34. for line in lines:
  35. _, path = line.split(' ')
  36. if full_path:
  37. dirs.append(os.path.join(IDF_PATH, path))
  38. else:
  39. dirs.append(path)
  40. except Exception as e:
  41. logging.warning(str(e))
  42. return dirs
  43. def _check_git_filemode(full_path): # type: (str) -> bool
  44. try:
  45. stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8')
  46. except subprocess.CalledProcessError:
  47. return True
  48. mode = stdout.split(' ', 1)[0] # e.g. 100644 for a rw-r--r--
  49. if any([int(i, 8) & 1 for i in mode[-3:]]):
  50. return False
  51. return True
  52. def is_executable(full_path): # type: (str) -> bool
  53. """
  54. os.X_OK will always return true on windows. Use git to check file mode.
  55. :param full_path: file full path
  56. :return: True is it's an executable file
  57. """
  58. if sys.platform == 'win32':
  59. return _check_git_filemode(full_path)
  60. else:
  61. return os.access(full_path, os.X_OK)