Skip to content

Commit

Permalink
Detect path type rather than OS type when transferring files between …
Browse files Browse the repository at this point in the history
…WSL and Windows filesystems (#8)

* Now checks to see if it is a windows filepath rather that just running on windows when doing path translations.

* Added a simple test to test various path types.
  • Loading branch information
STFleming authored Mar 19, 2024
1 parent e516280 commit f9d364d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 6 additions & 2 deletions npu/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
# SPDX-License-Identifier: MIT

import os
from .utils import is_win
from .utils import is_win, is_win_path, is_wsl_win_path

MODULE_PATH = os.path.dirname(os.path.realpath(__file__))
BUILD_TEMPLATE_PATH = os.path.join(MODULE_PATH,"build_template")

def wslpath(winpath:str)->str:
""" From the windows path create the equivalent WSL path """
if is_win():
if is_win_path(winpath):
drive = winpath[0].lower()
tpath = winpath[3:].replace("\\", "/")
return f"/mnt/{drive}/{tpath}"
elif is_wsl_win_path(winpath):
splitpath = winpath.split("\\")
newpath = '/' + '/'.join(splitpath[4:])
return newpath
else:
return winpath

12 changes: 11 additions & 1 deletion npu/build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
# SPDX-License-Identifier: MIT

import os
import re
import subprocess

def is_win()->bool:
""" Returns true if we are running this on windows."""
return os.name == "nt"

def is_win_path(path:str)->bool:
""" Returns true if the path above is a windows path """
newpath = path.split('\\')
return newpath[0].endswith(':')

def is_wsl_win_path(path:str)->bool:
""" Returns true if this is a windows path into WSL """
return path.startswith("\\\\wsl.localhost")

def wsl_prefix()->str:
""" if we are running this on windows return the appropriate wsl prefix."""
if is_win():
Expand All @@ -21,4 +31,4 @@ def check_wsl_install()->None:
except subprocess.CalledProcessError as e:
print("Failed to detect Riallto WSL instance. Please see https://riallto.ai/install-riallto.html for installation instructions.")
print(f"{e.output.decode()}")
raise e
raise e
10 changes: 10 additions & 0 deletions tests/test_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from npu.build import wslpath, is_win_path, is_wsl_win_path

def test_path_type_checks():
assert(is_win_path("C:\\Users\\dev\\Riallto\\npu\\lib\\kernels\\cpp\\*.h"))
assert(is_win_path("C:\\Users\\dev\\Riallto\\npu\\lib\\kernels\\cpp\\file.h"))
assert(is_win_path("C:\\Users\\dev\\Riallto\\npu\\lib\\kernels\\cpp\\file.h"))
assert(not is_win_path("/mnt/c/Users/dev/test.cpp"))
assert(not is_win_path("/mnt/c/Users/dev/*.h"))
assert(is_wsl_win_path("\\\\wsl.localhost\\Riallto\\dev\\Riallto\\npu\\lib\\kernels\\cpp\\file.h"))
assert(is_wsl_win_path("\\\\wsl.localhost\\Riallto\\dev\\Riallto\\npu\\lib\\kernels\\cpp\\*.h"))

0 comments on commit f9d364d

Please sign in to comment.