Files
Zordon/lib/utilities/misc_helper.py
Brett Williams 34fbdaa4d9 Refactor: DistributedJobManager with pub/sub status change notifications (#25)
* Add pubsub to render_queue and base_worker

* Refactor: Convert ZeroconfServer to Singleton with Class Methods

* New API for subjob servers to notify parent job servers of status changes

* Refactor: Move all subjob related methods to distributed_job_manager.py

* Rewrite for wait_for_subjobs

* Fix: DistributedJobManager.find_available_servers() takes 1 positional argument but 3 were given

* DistributedJobManager should now notify / be notified abotu background job changes

* Fix the make_ready api. Change children keyname to be id@hostname so it can be unique

* Fixes

* Image sequence to movie needs to find the actual start frame

* Fix: subjob_status_change did not return a valid response

* Fix client renderer selection

* Small fix for subjob status checking

* Fix issue with divide_frames_equally

* Fix issue where downloads were not occurring

* Fix issue where old status was being reported

* Add docstrings and code cleanup
2023-06-30 19:49:57 -05:00

106 lines
3.6 KiB
Python

import logging
import os
import subprocess
from datetime import datetime
logger = logging.getLogger()
def launch_url(url):
if subprocess.run(['which', 'xdg-open'], capture_output=True).returncode == 0:
subprocess.run(['xdg-open', url]) # linux
elif subprocess.run(['which', 'open'], capture_output=True).returncode == 0:
subprocess.run(['open', url]) # macos
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}")
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
def get_time_elapsed(start_time=None, end_time=None):
from string import Template
class DeltaTemplate(Template):
delimiter = "%"
def strfdelta(tdelta, fmt='%H:%M:%S'):
d = {"D": tdelta.days}
hours, rem = divmod(tdelta.seconds, 3600)
minutes, seconds = divmod(rem, 60)
d["H"] = '{:02d}'.format(hours)
d["M"] = '{:02d}'.format(minutes)
d["S"] = '{:02d}'.format(seconds)
t = DeltaTemplate(fmt)
return t.substitute(**d)
# calculate elapsed time
elapsed_time = None
if start_time:
if end_time:
elapsed_time = end_time - start_time
else:
elapsed_time = datetime.now() - start_time
elapsed_time_string = strfdelta(elapsed_time) if elapsed_time else None
return elapsed_time_string
def get_file_size_human(file_path):
size_in_bytes = os.path.getsize(file_path)
# Convert size to a human readable format
if size_in_bytes < 1024:
return f"{size_in_bytes} B"
elif size_in_bytes < 1024 ** 2:
return f"{size_in_bytes / 1024:.2f} KB"
elif size_in_bytes < 1024 ** 3:
return f"{size_in_bytes / 1024 ** 2:.2f} MB"
elif size_in_bytes < 1024 ** 4:
return f"{size_in_bytes / 1024 ** 3:.2f} GB"
else:
return f"{size_in_bytes / 1024 ** 4:.2f} TB"