예제 #1
0
def copy_path(source, target):
    """
    Makes a copy of the given source path to the given destination path
    :param source: str
    :param target: str
    :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 os.path.exists(target):
        raise IOError('Cannot copy over an existing path: {}'.format(target))
    if os.path.isfile(source):
        shutil.copy(source, target)
    else:
        shutil.copytree(source, target)

    return target
예제 #2
0
def rename_path_in_file(path, source, target):
    """
    Renames the given source path to the given target path
    :param path: str
    :param source: str
    :param target: str
    """

    source = path_utils.normalize_path(source)
    target = path_utils.normalize_path(target)

    source1 = '"' + source + '"'
    target1 = '"' + target + '"'

    replace_json(path, source1, target1)

    source2 = '"' + source
    target2 = '"' + target

    if not source2.endswith("/"):
        source2 += "/"

    if not target2.endswith("/"):
        target2 += "/"

    replace_json(path, source2, target2)
예제 #3
0
def absolute_path(data, start):
    """
    Returns an absolute version of all the paths in data using the start path
    :param data: str
    :param start: str
    :return: str
    """

    rel_path1 = path_utils.normalize_path(os.path.dirname(start))
    rel_path2 = path_utils.normalize_path(os.path.dirname(rel_path1))
    rel_path3 = path_utils.normalize_path(os.path.dirname(rel_path2))

    if not rel_path1.endswith("/"):
        rel_path1 += "/"

    if not rel_path2.endswith("/"):
        rel_path2 += "/"

    if not rel_path3.endswith("/"):
        rel_path3 += "/"

    data = data.replace('../../../', rel_path3)
    data = data.replace('../../', rel_path2)
    data = data.replace('../', rel_path1)

    return data
예제 #4
0
    def _get_available_modules(self, paths=None):
        imports = list()
        if not paths:
            paths = sys.path
        if paths:
            paths = python.force_list(paths)

        for path in paths:
            fix_path = path_utils.normalize_path(path)
            if not path.is_dir(fix_path):
                continue
            folders = folder_utils.get_folders(fix_path)
            for folder in folders:
                folder_path = path_utils.join_path(fix_path, folder)
                files = folder_utils.get_files_with_extension('py',
                                                              folder_path,
                                                              full_path=False)
                if '__init__.py' in files:
                    imports.append(str(folder))

            python_files = folder_utils.get_files_with_extension(
                'py', fix_path, full_path=False)
            for python_file in python_files:
                if python_file.startswith('__'):
                    continue
                python_file_name = python_file.split('.')[0]
                imports.append(str(python_file_name))

        if imports:
            imports = list(set(imports))

        return imports
예제 #5
0
    def find_items(self, path, depth=3, **kwargs):
        """
        Find and create items by walking the given path
        :param path: str
        :param depth: int
        :param kwargs: dict
        :return: Iterable(LibraryItem)
        """

        path = path_utils.normalize_path(path)
        max_depth = depth
        start_depth = path.count(os.path.sep)

        for root, dirs, files in walk(path, followlinks=True):
            files.extend(dirs)
            for filename in files:
                remove = False
                path = os.path.join(root, filename)
                item = self.item_from_path(path, **kwargs)
                if item:
                    yield item
                    if not item.EnableNestedItems:
                        remove = True
                if remove and filename in dirs:
                    dirs.remove(filename)

            if depth == 1:
                break

            # Stop walking the directory if the maximum depth has been reached
            current_depth = root.count(os.path.sep)
            if (current_depth - start_depth) >= max_depth:
                del dirs[:]
예제 #6
0
def rename_path(source, target, extension=None, force=False):
    """
    Renames the given source path to the given destination path
    :param source: str
    :param target: str
    :param extension: str
    :param force: bool
    :return: str
    """

    dirname = os.path.dirname(source)

    if '/' not in target:
        target = os.path.join(dirname, target)

    if extension and extension not in target:
        target += extension

    source = path_utils.normalize_path(source)
    target = path_utils.normalize_path(target)
    tpPyUtils.logger.debug('Renaming: {} > {}'.format(source, target))

    if source == target and not force:
        raise exceptions.RenamePathError(
            'The source path and destination path are the same: {}'.format(
                source))
    if os.path.exists(target) and not force:
        raise exceptions.RenamePathError(
            'Cannot save over an existing path: "{}"'.format(target))
    if not os.path.exists(dirname):
        raise exceptions.RenamePathError(
            'The system cannot find the specified path: "{}"'.format(dirname))
    if not os.path.exists(os.path.dirname(target)) and force:
        os.mkdir(os.path.dirname(target))
    if not os.path.exists(source):
        raise exceptions.RenamePathError(
            'The system cannot find the specified path: "{}"'.format(source))

    os.rename(source, target)
    tpPyUtils.logger.debug('Renamed: {} > {}'.format(source, target))

    return target
예제 #7
0
def save_json(path, data):
    """
    Serialize given tdata to a JSON string and write it to the given path
    :param path: str
    :param data: dict
    """

    path = path_utils.normalize_path(path)
    data = OrderedDict(sorted(data.items(), key=lambda t: t[0]))
    data = json.dumps(data, indent=4)
    write(path, data)
예제 #8
0
def read_json(path):
    """
    Reads the given JSON file and deserialize it to a Python object
    :param path: str
    :return: dict
    """

    path = path_utils.normalize_path(path)
    data = read(path) or '{}'
    data = json.loads(data)

    return data
예제 #9
0
    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)
예제 #10
0
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
예제 #11
0
def move_paths(source_paths, target):
    """
    Moves the given paths to the given destination paths
    :param source_paths: list(str)
    :param target: str
    :return: list(str)
    """

    if not os.path.exists(target):
        os.makedirs(target)

    for source in source_paths or list():
        if not source:
            continue
        basename = os.path.basename(source)
        target_ = path_utils.normalize_path(os.path.join(target, basename))
        tpPyUtils.logger.debug('Moving Content: {} > {}'.format(
            source, target_))
        shutil.move(source, target_)
예제 #12
0
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):
        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):
            os.remove(tmp)
        if not os.path.exists(path) and os.path.exists(bak):
            os.rename(bak, path)

        raise