utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # File : utils.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import sys
  25. import os
  26. def splitall(loc):
  27. """
  28. Return a list of the path components in loc. (Used by relpath_).
  29. The first item in the list will be either ``os.curdir``, ``os.pardir``, empty,
  30. or the root directory of loc (for example, ``/`` or ``C:\\).
  31. The other items in the list will be strings.
  32. Adapted from *path.py* by Jason Orendorff.
  33. """
  34. parts = []
  35. while loc != os.curdir and loc != os.pardir:
  36. prev = loc
  37. loc, child = os.path.split(prev)
  38. if loc == prev:
  39. break
  40. parts.append(child)
  41. parts.append(loc)
  42. parts.reverse()
  43. return parts
  44. def _make_path_relative(origin, dest):
  45. """
  46. Return the relative path between origin and dest.
  47. If it's not possible return dest.
  48. If they are identical return ``os.curdir``
  49. Adapted from `path.py <http://www.jorendorff.com/articles/python/path/>`_ by Jason Orendorff.
  50. """
  51. origin = os.path.abspath(origin).replace('\\', '/')
  52. dest = os.path.abspath(dest).replace('\\', '/')
  53. #
  54. orig_list = splitall(os.path.normcase(origin))
  55. # Don't normcase dest! We want to preserve the case.
  56. dest_list = splitall(dest)
  57. #
  58. if orig_list[0] != os.path.normcase(dest_list[0]):
  59. # Can't get here from there.
  60. return dest
  61. #
  62. # Find the location where the two paths start to differ.
  63. i = 0
  64. for start_seg, dest_seg in zip(orig_list, dest_list):
  65. if start_seg != os.path.normcase(dest_seg):
  66. break
  67. i += 1
  68. #
  69. # Now i is the point where the two paths diverge.
  70. # Need a certain number of "os.pardir"s to work up
  71. # from the origin to the point of divergence.
  72. segments = [os.pardir] * (len(orig_list) - i)
  73. # Need to add the diverging part of dest_list.
  74. segments += dest_list[i:]
  75. if len(segments) == 0:
  76. # If they happen to be identical, use os.curdir.
  77. return os.curdir
  78. else:
  79. # return os.path.join(*segments).replace('\\', '/')
  80. return os.path.join(*segments)
  81. def xml_indent(elem, level=0):
  82. i = "\n" + level*" "
  83. if len(elem):
  84. if not elem.text or not elem.text.strip():
  85. elem.text = i + " "
  86. if not elem.tail or not elem.tail.strip():
  87. elem.tail = i
  88. for elem in elem:
  89. xml_indent(elem, level+1)
  90. if not elem.tail or not elem.tail.strip():
  91. elem.tail = i
  92. else:
  93. if level and (not elem.tail or not elem.tail.strip()):
  94. elem.tail = i