Merge pull request #162 from sklirg/fix/network-mounts-windows

feat: Support non-mapped network drives on Windows.
This commit is contained in:
Alan Hamlett 2018-12-28 18:10:49 -06:00 committed by GitHub
commit 58a8ddcc8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View file

@ -27,6 +27,18 @@ class UtilsTestCase(TestCase):
result = format_file_path(path) result = format_file_path(path)
self.assertPathsEqual(expected, result) self.assertPathsEqual(expected, result)
def test_format_file_path_windows_network_mount(self):
path = '\\\\some\\path////to\\\\\\a\\file.txt'
expected = '//some/path/to/a/file.txt'
with mock.patch('os.path.realpath') as mock_realpath:
mock_realpath.return_value = path
with mock.patch('os.path.abspath') as mock_abspath:
mock_abspath.return_value = path
result = format_file_path(path)
self.assertPathsEqual(expected, result)
def test_format_file_path_handles_exceptions(self): def test_format_file_path_handles_exceptions(self):
path = 'c:\\some\\path////to\\\\\\a\\file.txt' path = 'c:\\some\\path////to\\\\\\a\\file.txt'
expected = path expected = path

View file

@ -255,6 +255,9 @@ class Heartbeat(object):
if self._file_exists(): if self._file_exists():
return return
if not self.entity:
return
self.args.local_file = self._to_unc_path(self.entity) self.args.local_file = self._to_unc_path(self.entity)
def _to_unc_path(self, filepath): def _to_unc_path(self, filepath):

View file

@ -26,6 +26,7 @@ log = logging.getLogger('WakaTime')
BACKSLASH_REPLACE_PATTERN = re.compile(r'[\\/]+') BACKSLASH_REPLACE_PATTERN = re.compile(r'[\\/]+')
WINDOWS_DRIVE_PATTERN = re.compile(r'^[a-z]:/') WINDOWS_DRIVE_PATTERN = re.compile(r'^[a-z]:/')
WINDOWS_NETWORK_MOUNT_PATTERN = re.compile(r'^\\{2}[a-z]+', re.IGNORECASE)
def should_exclude(entity, include, exclude): def should_exclude(entity, include, exclude):
@ -77,10 +78,15 @@ def format_file_path(filepath):
"""Formats a path as absolute and with the correct platform separator.""" """Formats a path as absolute and with the correct platform separator."""
try: try:
is_windows_network_mount = WINDOWS_NETWORK_MOUNT_PATTERN.match(filepath)
filepath = os.path.realpath(os.path.abspath(filepath)) filepath = os.path.realpath(os.path.abspath(filepath))
filepath = re.sub(BACKSLASH_REPLACE_PATTERN, '/', filepath) filepath = re.sub(BACKSLASH_REPLACE_PATTERN, '/', filepath)
if WINDOWS_DRIVE_PATTERN.match(filepath): if WINDOWS_DRIVE_PATTERN.match(filepath):
filepath = filepath.capitalize() filepath = filepath.capitalize()
if is_windows_network_mount:
# Add back a / to the front, since the previous modifications
# will have replaced any double slashes with single
filepath = '/' + filepath
except: except:
pass pass
return filepath return filepath