Ejemplo n.º 1
0
    def delete_read_only_error(action, name, exc):
        """
        Helper to delete read only files
        """

        osplatform.get_permission(name)
        action(name)
Ejemplo n.º 2
0
def delete_file(name, directory=None, show_warning=True):
    """
    Delete the file by name in the directory
    :param name: str, name of the file to delete
    :param directory: str, the directory where the file is stored
    :param show_warning: bool
    :return: str, file path that was deleted
    """

    from tpDcc.libs.python import path, osplatform

    if not directory:
        full_path = name
    else:
        full_path = path.join_path(directory, name)
    if not path.is_file(full_path):
        if show_warning:
            print('File "{}" was not deleted.'.format(full_path))
        return full_path

    try:
        osplatform.get_permission(full_path)
    except Exception:
        pass
    try:
        os.remove(full_path)
    except Exception:
        pass

    return full_path
Ejemplo n.º 3
0
    def export_data(self, comment='-', create_version=True, *args, **kwargs):
        if not dcc.is_maya():
            LOGGER.warning('Maya data must be saved from within Maya!')
            return

        file_path = self.get_file()
        osplatform.get_permission(file_path)
        self._handle_unknowns()
        self._clean_scene()

        if not file_path.endswith('.mb') and not file_path.endswith('.ma'):
            file_path = maya.cmds.workspace(query=True, rd=True)
            if self.maya_file_type == self.maya_ascii:
                file_path = maya.cmds.fileDialog(ds=1, fileFilter='Maya Ascii (*.ma)', dir=file_path)
            elif self.maya_file_type == self.maya_binary:
                file_path = maya.cmds.fileDialog(ds=1, fileFilter='Maya Binary (*.mb)', dir=file_path)
            if file_path:
                file_path = file_path[0]

        saved = scene.save_as(file_path)
        if saved:
            if create_version:
                version_file = version.VersionFile(file_path)
                # if scene.is_batch() or not version_file.has_versions():
                version_file.save(comment)

            helpers.display_info('Saved {} data'.format(self.name))
            return True

        return False
Ejemplo n.º 4
0
def create_file(filename, directory=None, make_unique=False):
    """
    Creates a file
    :param filename: str, name of the new file
    :param directory: str, directory of the new file
    :param make_unique: bool, whether to make the name unique or not
    :return: variant, str || bool, filename with path or False if create file failed
    """

    from tpDcc.libs.python import name, path, osplatform

    if directory is None:
        directory = path.get_dirname(filename)
        filename = path.get_basename(filename)

    filename = name.clean_file_string(filename)
    full_path = path.join_path(directory, filename)

    if make_unique:
        full_path = path.unique_path_name(full_path)

    open_file = None
    try:
        open_file = open(full_path, 'a')
        open_file.close()
    except Exception:
        if open_file:
            open_file.close()
        return False

    osplatform.get_permission(full_path)

    return full_path
Ejemplo n.º 5
0
    def __init__(self, file_path):
        super(FileWriter, self).__init__(file_path=file_path)

        from tpDcc.libs.python import osplatform

        osplatform.get_permission(file_path)
        self.append = False
Ejemplo n.º 6
0
def source_python_module(module_file):
    """
    Sources the python module located in the given directory
    :param module_file: str
    :return: variant, sourced module || None
    """

    osplatform.get_permission(module_file)

    try:
        try:
            delete_source_code_from_sys_modules(module_file)
            module_in = open(module_file, 'r')
            if is_python2():
                import md5
                import imp
                return imp.load_source(
                    md5.new(module_file).hexdigest(), module_file, module_in)
            else:
                import hashlib
                from importlib.machinery import SourceFileLoader
                return SourceFileLoader(
                    hashlib.md5(module_file.encode()).hexdigest(),
                    module_file).load_module()
        except Exception:
            return traceback.format_exc()
        finally:
            try:
                module_in.close()
            except Exception:
                pass
    except ImportError:
        traceback.print_exc(file=sys.stderr)
        return None
Ejemplo n.º 7
0
def copy_file(file_path, file_path_destination):
    """
    Copies the given file to a new given directory
    :param file_path: str, file to copy with full path
    :param file_path_destination: str, destination directory where we want to copy the file into
    :return: str, the new copied path
    """

    from tpDcc.libs.python import path, osplatform

    osplatform.get_permission(file_path)

    if path.is_file(file_path):
        if path.is_dir(file_path_destination):
            file_name = path.get_basename(file_path)
            file_path_destination = path.join_path(file_path_destination,
                                                   file_name)
        shutil.copy2(file_path, file_path_destination)

    return file_path_destination
Ejemplo n.º 8
0
def create_folder(name, directory=None, make_unique=False):
    """
    Creates a new folder on the given path and with the given name
    :param name: str, name of the new directory
    :param directory: str, path to the new directory
    :param make_unique: bool, Whether to pad the name with a number to make it unique if the folder is not unique
    :return: variant, str || bool, folder name with path or False if the folder creation failed
    """

    from tpDcc.libs.python import path, osplatform

    full_path = False

    if directory is None:
        full_path = name

    if not name:
        full_path = directory

    if name and directory:
        full_path = path.join_path(directory, name)

    if make_unique:
        full_path = path.unique_path_name(directory=full_path)

    if not full_path:
        return False

    if path.is_dir(full_path):
        return full_path

    try:
        os.makedirs(full_path)
    except Exception:
        return False

    osplatform.get_permission(full_path)

    return full_path
Ejemplo n.º 9
0
def rename_folder(directory, name, make_unique=False):
    """
    Renames given with a new name
    :param directory: str, full path to the directory we want to rename
    :param name: str, new name of the folder we want to rename
    :param make_unique: bool, Whether to add a number to the folder name to make it unique
    :return: str, path of the renamed folder
    """

    from tpDcc.libs.python import path, osplatform

    base_name = path.get_basename(directory=directory)
    if base_name == name:
        return

    parent_path = path.get_dirname(directory=directory)
    rename_path = path.join_path(parent_path, name)

    if make_unique:
        rename_path = path.unique_path_name(directory=rename_path)

    if path.exists(rename_path):
        return False

    try:
        osplatform.get_permission(directory)
        message = 'rename: {0} >> {1}'.format(directory, rename_path)
        logger.info(message)
        os.rename(directory, rename_path)
    except Exception:
        time.sleep(0.1)
        try:
            os.rename(directory, rename_path)
        except Exception:
            logger.error('{}'.format(traceback.format_exc()))
            return False

    return rename_path
Ejemplo n.º 10
0
def write_lines(file_path, lines, append=False):
    """
    Writes a list of text lines to a file. Every entry in the list is a new line
    :param file_path: str, filename and path
    :param lines: list<str>, list of text lines in which each entry is a new line
    :param append: bool, Whether to append the text or replace it
    """

    from tpDcc.libs.python import python, osplatform

    permission = osplatform.get_permission(file_path)
    if not permission:
        return

    write_string = 'a' if append else 'w'

    lines = python.force_list(lines)
    text = '\n'.join(map(str, lines))
    if append:
        text = '\n' + text

    with open(file_path, write_string) as open_file:
        open_file.write(text)