Skip to content

Commit

Permalink
fix(windows): fix invalid mapPath in libjsonnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Oct 13, 2022
1 parent 5ccfb47 commit 74ef98a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
13 changes: 10 additions & 3 deletions ddb/feature/docker/actions.py
Expand Up @@ -21,6 +21,7 @@
from ...config import config
from ...context import context
from ...event import bus, events
from ...utils.compat import posix_as_path_fast
from ...utils.table_display import get_table_display


Expand Down Expand Up @@ -331,13 +332,19 @@ def _fix_owner(relative_path):
relative_path)

@staticmethod
def _check_file_in_project(target):
target_path = os.path.realpath(target)
def _check_file_in_project(source):
if os.name == 'nt':
source = posix_as_path_fast(source)

target_path = os.path.realpath(source)
cwd = os.path.realpath(".")
return target_path.startswith(cwd)

@staticmethod
def _create_local_volume(source, target):
if os.name == 'nt':
source = posix_as_path_fast(source)

rel_source = os.path.relpath(source, ".")
if os.path.exists(source):
context.log.notice("Local volume source: %s (exists)", rel_source)
Expand Down Expand Up @@ -392,7 +399,7 @@ def _get_volume_mappings(docker_compose_config, external_volumes):
source = PurePosixPath(device).joinpath(volume)
target = external_volumes_dict.get(volume)
if source and target:
volume_mapping = (source, target)
volume_mapping = (str(source), target)
if volume_mapping not in volume_mappings:
volume_mappings.append(volume_mapping)

Expand Down
2 changes: 1 addition & 1 deletion ddb/feature/jsonnet/lib/ddb.docker.libjsonnet
Expand Up @@ -49,7 +49,7 @@ local mapPath(path) =
if (std.length(keys) > 0) then
# Keep longuest directory to use as path mapping
local key = std.foldl(function(x, y) if std.length(x) > std.length(y) then x else y, keys, "");
_docker_path_mapping[key + std.substr(path, std.length(_docker_path_mapping[key]), std.length(path))]
_docker_path_mapping[key] + std.substr(path, std.length(_docker_path_mapping[key]), std.length(path))
else
path;

Expand Down
18 changes: 14 additions & 4 deletions ddb/utils/compat.py
@@ -1,11 +1,21 @@
import re

_posix_drive_letter_regex = re.compile(r"^([a-zA-Z]):")
_windows_drive_letter_regex = re.compile(r"^([a-zA-Z]):")
_windows_posix_drive_letter_regex = re.compile(r"^/([a-z])(/|$)")


def path_as_posix_fast(path: str):
"""
Simpler but faster version of pathlib Path.as_posix method
Convert a local path to a posix path
"""
path = _posix_drive_letter_regex.sub(lambda match: '/' + match.group(1).lower(), path)
return path.replace('\\', '/')
replaced = _windows_drive_letter_regex.sub(lambda match: '/' + match.group(1).lower(), path)
return replaced.replace('\\', '/')


def posix_as_path_fast(posix_path: str):
"""
Convert a posix path to a local path
"""
replaced = _windows_posix_drive_letter_regex.sub(lambda match: match.group(1).upper() + ':' + match.group(2),
posix_path)
return replaced.replace('/', '\\')

0 comments on commit 74ef98a

Please sign in to comment.