Skip to content

Commit b704fa8

Browse files
committed
Make Resource.build_path work on Windows
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 4aab7ae commit b704fa8

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/commoncode/resource.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from os.path import normpath
2424
from posixpath import join as posixpath_join
2525
from posixpath import normpath as posixpath_normpath
26+
from posixpath import dirname as posixpath_parent
2627

2728
import attr
2829

@@ -1169,13 +1170,21 @@ def build_path(cls, root_location, location):
11691170
to ``root_location`. Both locations are absolute native locations.
11701171
The returned path has no leading and trailing slashes. The first segment
11711172
of this path is always the last segment of the ``root_location``.
1173+
For example:
1174+
>>> result = Resource.build_path(r'D:\\foo\\bar', r'D:\\foo\\bar\\baz')
1175+
>>> assert result == 'bar/baz', repr(result)
1176+
>>> result = Resource.build_path('/foo/bar/', '/foo/bar/baz')
1177+
>>> assert result == 'bar/baz', result
1178+
>>> result = Resource.build_path('/foo/bar/', '/foo/bar')
1179+
>>> assert result == 'bar', result
11721180
"""
11731181
root_loc = clean_path(root_location)
11741182
loc = clean_path(location)
11751183
assert loc.startswith(root_loc)
1184+
11761185
# keep the root directory name by default
1177-
root_loc = parent_directory(root_loc, with_trail=True)
1178-
path = loc.replace(root_loc, '', 1)
1186+
root_loc = posixpath_parent(root_loc).strip('/')
1187+
path = loc.replace(root_loc, '', 1).strip('/')
11791188
if TRACE:
11801189
logger_debug('build_path:', root_loc, loc, path)
11811190
return path
@@ -1580,6 +1589,7 @@ def get_codebase_cache_dir(temp_dir):
15801589

15811590
@attr.s(slots=True)
15821591
class _CodebaseAttributes(object):
1592+
15831593
def to_dict(self):
15841594
return attr.asdict(self, dict_factory=dict)
15851595

0 commit comments

Comments
 (0)