def copy_path(source, target, force=False): """ Makes a copy of the given source path to the given destination path :param source: str :param target: str :param force: bool :return: str """ dirname = os.path.dirname(source) if '/' not in target: target = os.path.join(dirname, target) source = path_utils.normalize_path(source) target = path_utils.normalize_path(target) if source == target: raise IOError( 'The source path and destination path are the same: {}'.format( source)) if not force and os.path.exists(target): raise IOError('Cannot copy over an existing path: {}'.format(target)) if force and os.path.exists(target): if os.path.isdir(target): shutil.rmtree(target) else: os.remove(target) # Ensure target directory exists target_directory = os.path.dirname(target) if not os.path.exists(target_directory): os.makedirs(target_directory) if os.path.isfile(source): shutil.copy(source, target) else: shutil.copytree(source, target) return target
def format_path(format_string, path='', **kwargs): """ Resolves given path by replacing necessary info with proper data :param format_string: str :param path: str :param kwargs: :return: str """ dirname, name, extension = path_utils.split_path(path) encoding = locale.getpreferredencoding() temp = tempfile.gettempdir() if temp: try: temp = temp.decode(encoding) except Exception: pass username = osplatform.get_user(lower=True) if username: try: username = username.decode(encoding) except Exception: pass local = os.getenv('APPDATA') or os.getenv('HOME') if local: try: local = local.decode(encoding) except Exception: pass kwargs.update(os.environ) format_dict = { 'name': name, 'path': path, 'root': path, 'user': username, 'temp': temp, 'home': local, 'local': local, 'dirname': dirname, 'extension': extension } kwargs.update(format_dict) resolve_string = path_utils.normalize_path(format_string).format(**kwargs) return path_utils.clean_path(resolve_string)
def item_from_path(self, path, extension=None, **kwargs): """ Return a new item instance for the given path :param path: str :param extension: str :param kwargs: dict :return: LibraryItem or None """ if extension: full_path = path_utils.normalize_path(path + extension) else: full_path = path_utils.normalize_path(path) path = path_utils.normalize_path(path) for ignore in self.get_ignore_paths(): if ignore in full_path: return None for cls in self.registered_items(): if cls.match(full_path): lib_win = kwargs.get('library_window', self.library_window()) kwargs['library_window'] = lib_win return cls(path, **kwargs)
def write(path, data): """ Writes the given data to the given file on disk :param path: str :param data: str """ path = path_utils.normalize_path(path) data = path_utils.get_relative_path(data, path) tmp = path + '.tmp' bak = path + '.bak' dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) if os.path.exists(tmp): try: os.remove(tmp) except Exception: pass if os.path.exists(tmp): msg = 'The path is locked for writing and cannot be accessed {}'.format( tmp) raise IOError(msg) try: with open(tmp, 'w') as f: f.write(data) f.flush() if os.path.exists(bak): os.remove(bak) if os.path.exists(path): os.rename(path, bak) if os.path.exists(tmp): os.rename(tmp, path) if os.path.exists(path) and os.path.exists(bak): os.remove(bak) except Exception: if os.path.exists(tmp): try: os.remove(tmp) except Exception: pass if not os.path.exists(path) and os.path.exists(bak): os.rename(bak, path) raise
def read(path): """ Returns the contents of the given file :param path: str :return: str """ data = '' path = path_utils.normalize_path(path) if os.path.isfile(path): with open(path) as f: data = f.read() or data data = absolute_path(data, path) return data
def temp_path(*args): """ Returns the temporal directory based on library settings :param args: """ datalib_config = configs.get_library_config('tpDcc-libs-datalibrary') temp_path = datalib_config.get('temp_path') if not temp_path: temp_path = tempfile.mkdtemp() temp_path = path_utils.normalize_path( os.path.join(format_path(temp_path), *args)) return temp_path
def __init__(self, msg): msg = path.normalize_path(msg) super(PathError, self).__init__(msg) self._msg = msg