diff options
| author | [email protected] <[email protected]> | 2012-02-19 09:11:35 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2012-02-19 09:11:35 +0000 |
| commit | 3fd6fd1f050e05369f9406ab6a94721a4ea8eb94 (patch) | |
| tree | 2b6a719d5bc2db83957abeac41be91ecb2ed3b0d /tools/utils.py | |
| parent | 68384c37c4d20b8c8d24f0cc187dd7533b51b1a3 (diff) | |
cleanup the building script in tools.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1970 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'tools/utils.py')
| -rw-r--r-- | tools/utils.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tools/utils.py b/tools/utils.py new file mode 100644 index 0000000000..d1b0944939 --- /dev/null +++ b/tools/utils.py @@ -0,0 +1,81 @@ +import sys +import os + +def splitall(loc): + """ + Return a list of the path components in loc. (Used by relpath_). + + The first item in the list will be either ``os.curdir``, ``os.pardir``, empty, + or the root directory of loc (for example, ``/`` or ``C:\\). + + The other items in the list will be strings. + + Adapted from *path.py* by Jason Orendorff. + """ + parts = [] + while loc != os.curdir and loc != os.pardir: + prev = loc + loc, child = os.path.split(prev) + if loc == prev: + break + parts.append(child) + parts.append(loc) + parts.reverse() + return parts + +def _make_path_relative(origin, dest): + """ + Return the relative path between origin and dest. + + If it's not possible return dest. + + + If they are identical return ``os.curdir`` + + Adapted from `path.py <http://www.jorendorff.com/articles/python/path/>`_ by Jason Orendorff. + """ + origin = os.path.abspath(origin).replace('\\', '/') + dest = os.path.abspath(dest).replace('\\', '/') + # + orig_list = splitall(os.path.normcase(origin)) + # Don't normcase dest! We want to preserve the case. + dest_list = splitall(dest) + # + if orig_list[0] != os.path.normcase(dest_list[0]): + # Can't get here from there. + return dest + # + # Find the location where the two paths start to differ. + i = 0 + for start_seg, dest_seg in zip(orig_list, dest_list): + if start_seg != os.path.normcase(dest_seg): + break + i += 1 + # + # Now i is the point where the two paths diverge. + # Need a certain number of "os.pardir"s to work up + # from the origin to the point of divergence. + segments = [os.pardir] * (len(orig_list) - i) + # Need to add the diverging part of dest_list. + segments += dest_list[i:] + if len(segments) == 0: + # If they happen to be identical, use os.curdir. + return os.curdir + else: + # return os.path.join(*segments).replace('\\', '/') + return os.path.join(*segments) + +def xml_indent(elem, level=0): + i = "\n" + level*" " + if len(elem): + if not elem.text or not elem.text.strip(): + elem.text = i + " " + if not elem.tail or not elem.tail.strip(): + elem.tail = i + for elem in elem: + xml_indent(elem, level+1) + if not elem.tail or not elem.tail.strip(): + elem.tail = i + else: + if level and (not elem.tail or not elem.tail.strip()): + elem.tail = i |
