Added file_exists_in_mounts method to look for projects on connected network mounts

This commit is contained in:
Brett Williams
2023-06-07 19:03:44 -05:00
parent ff4ef41a39
commit f54d665714
2 changed files with 70 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
import logging
import os
import subprocess
logger = logging.getLogger()
@@ -12,4 +13,45 @@ def launch_url(url):
elif subprocess.run(['which', 'start'], capture_output=True).returncode == 0:
subprocess.run(['start', url]) # windows - need to validate this works
else:
logger.error(f"No valid launchers found to launch url: {url}")
logger.error(f"No valid launchers found to launch url: {url}")
def file_exists_in_mounts(filepath):
"""
Check if a file exists in any mounted directory.
It searches for the file in common mount points like '/Volumes', '/mnt', and '/media'.
Returns the path to the file in the mount if found, otherwise returns None.
Example:
Before: filepath = '/path/to/file.txt'
After: '/Volumes/ExternalDrive/path/to/file.txt'
"""
def get_path_components(path):
path = os.path.normpath(path)
components = []
while True:
path, component = os.path.split(path)
if component:
components.append(component)
else:
if path:
components.append(path)
break
components.reverse()
return components
# Get path components of the directory of the file
path_components = get_path_components(os.path.dirname(filepath).strip('/'))
# Iterate over possible root paths - this may need to be rethought for Windows support
for root in ['/Volumes', '/mnt', '/media']:
if os.path.exists(root):
# Iterate over mounts in the root path
for mount in os.listdir(root):
# Since we don't know the home directory, we iterate until we find matching parts of the path
matching_components = [s for s in path_components if s in mount]
for component in matching_components:
possible_mount_path = os.path.join(root, mount, filepath.split(component)[-1].lstrip('/'))
if os.path.exists(possible_mount_path):
return possible_mount_path