util.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. from __future__ import unicode_literals
  17. import os
  18. import shutil
  19. import sys
  20. from io import open
  21. try:
  22. import urllib.request
  23. _urlretrieve = urllib.request.urlretrieve
  24. except ImportError:
  25. # Python 2 fallback
  26. import urllib
  27. _urlretrieve = urllib.urlretrieve
  28. def files_equal(path_1, path_2):
  29. if not os.path.exists(path_1) or not os.path.exists(path_2):
  30. return False
  31. file_1_contents = ''
  32. with open(path_1, 'r', encoding='utf-8') as f_1:
  33. file_1_contents = f_1.read()
  34. file_2_contents = ''
  35. with open(path_2, 'r', encoding='utf-8') as f_2:
  36. file_2_contents = f_2.read()
  37. return file_1_contents == file_2_contents
  38. def copy_file_if_modified(src_file_path, dst_file_path):
  39. if not files_equal(src_file_path, dst_file_path):
  40. dst_dir_name = os.path.dirname(dst_file_path)
  41. if not os.path.isdir(dst_dir_name):
  42. os.makedirs(dst_dir_name)
  43. shutil.copy(src_file_path, dst_file_path)
  44. def copy_if_modified(src_path, dst_path):
  45. if os.path.isfile(src_path):
  46. copy_file_if_modified(src_path, dst_path)
  47. return
  48. src_path_len = len(src_path)
  49. for root, dirs, files in os.walk(src_path):
  50. for src_file_name in files:
  51. src_file_path = os.path.join(root, src_file_name)
  52. dst_file_path = os.path.join(dst_path + root[src_path_len:], src_file_name)
  53. copy_file_if_modified(src_file_path, dst_file_path)
  54. def download_file_if_missing(from_url, to_path):
  55. filename_with_path = to_path + '/' + os.path.basename(from_url)
  56. exists = os.path.isfile(filename_with_path)
  57. if exists:
  58. print("The file '%s' already exists" % (filename_with_path))
  59. else:
  60. tmp_file, header = _urlretrieve(from_url)
  61. with open(filename_with_path, 'wb') as fobj:
  62. with open(tmp_file, 'rb') as tmp:
  63. fobj.write(tmp.read())
  64. def call_with_python(cmd):
  65. # using sys.executable ensures that the scripts are called with the same Python interpreter
  66. if os.system('{} {}'.format(sys.executable, cmd)) != 0:
  67. raise RuntimeError('{} failed'.format(cmd))