local_util.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Utility functions used in conf.py
  2. #
  3. # Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http:#www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import re
  17. import os
  18. import shutil
  19. def run_cmd_get_output(cmd):
  20. return os.popen(cmd).read().strip()
  21. def files_equal(path_1, path_2):
  22. if not os.path.exists(path_1) or not os.path.exists(path_2):
  23. return False
  24. file_1_contents = ''
  25. with open(path_1, "r") as f_1:
  26. file_1_contents = f_1.read()
  27. file_2_contents = ''
  28. with open(path_2, "r") as f_2:
  29. file_2_contents = f_2.read()
  30. return file_1_contents == file_2_contents
  31. def copy_file_if_modified(src_file_path, dst_file_path):
  32. if not files_equal(src_file_path, dst_file_path):
  33. dst_dir_name = os.path.dirname(dst_file_path)
  34. if not os.path.isdir(dst_dir_name):
  35. os.makedirs(dst_dir_name)
  36. shutil.copy(src_file_path, dst_file_path)
  37. def copy_if_modified(src_path, dst_path):
  38. if os.path.isfile(src_path):
  39. copy_file_if_modified(src_path, dst_path)
  40. return
  41. src_path_len = len(src_path)
  42. for root, dirs, files in os.walk(src_path):
  43. for src_file_name in files:
  44. src_file_path = os.path.join(root, src_file_name)
  45. dst_file_path = os.path.join(dst_path + root[src_path_len:], src_file_name)
  46. copy_file_if_modified(src_file_path, dst_file_path)