Ejemplo n.º 1
0
    def delete_files(self, paths_to_delete):
        """
        This method handles deleting an item's path(s) from a designated location.

        If the item has "sequence_paths" set, it will attempt to delete all paths
        assuming they meet the required criteria.
        """

        publisher = self.parent

        logger = publisher.logger

        # ---- delete the work files from the publish location
        processed_paths = []
        for deletion_path in paths_to_delete:

            # delete the path
            if os.path.isdir(deletion_path):
                filesystem.safe_delete_folder(deletion_path)
                logger.debug("Deleted folder '%s'." % deletion_path)
            else:
                filesystem.safe_delete_file(deletion_path)
                logger.debug("Deleted file '%s'." % deletion_path)

            processed_paths.append(deletion_path)

        return processed_paths
Ejemplo n.º 2
0
    def _remove_old_cached_data(self, grace_period, *cache_locations):
        """
        Remove old data files cached by this bundle in the given cache locations.

        :param int grace_period: Time period, in days, a file without
                                     modification should be kept around.
        :param cache_locations: A list of cache locations to clean up.
        :raises: ValueError if the grace period is smaller than one day.
        """
        if grace_period < 1:
            raise ValueError(
                "Invalid grace period value %d, it must be a least 1" % grace_period
            )
        delta = datetime.timedelta(days=grace_period)
        # Datetime total_seconds was introduced in Python 2.7, so compute the
        # value ourself.
        grace_in_seconds = (
            delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10 ** 6
        ) / 10 ** 6

        # Please note that we can't log any message from this background thread
        # without the risk of causing deadlocks.
        now_timestamp = time.time()
        # Check if we should stop and bail out immediately if so.
        if self._stop_cleanup:
            return
        for cache_location in cache_locations:
            # Go bottom up in the hierarchy and delete old files
            for folder, dirs, files in os.walk(cache_location, topdown=False):
                for name in files:
                    # Check if we should stop and bail out immediately if so.
                    if self._stop_cleanup:
                        return
                    file_path = os.path.join(folder, name)
                    try:
                        file_stats = os.stat(file_path)
                        # Is it old enough to be removed?
                        if now_timestamp - file_stats.st_mtime > grace_in_seconds:
                            filesystem.safe_delete_file(file_path)
                    except Exception as e:
                        # Silently ignore the error
                        pass

                for name in dirs:
                    # Check if we should stop and bail out immediately if so.
                    if self._stop_cleanup:
                        return
                    # Try to remove empty directories
                    dir_path = os.path.join(folder, name)
                    try:
                        if not os.listdir(dir_path):
                            filesystem.safe_delete_folder(dir_path)
                    except Exception as e:
                        # Silently ignore the error
                        pass
    def _remove_old_cached_data(self, grace_period, *cache_locations):
        """
        Remove old data files cached by this bundle in the given cache locations.

        :param int grace_period: Time period, in days, a file without
                                     modification should be kept around.
        :param cache_locations: A list of cache locations to clean up.
        :raises: ValueError if the grace period is smaller than one day.
        """
        if grace_period < 1:
            raise ValueError(
                "Invalid grace period value %d, it must be a least 1" % grace_period
            )
        delta = datetime.timedelta(days=grace_period)
        # Datetime total_seconds was introduced in Python 2.7, so compute the
        # value ourself.
        grace_in_seconds = (
            delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10**6
        ) / 10**6

        # Please note that we can't log any message from this background thread
        # without the risk of causing deadlocks.
        now_timestamp = time.time()
        # Check if we should stop and bail out immediately if so.
        if self._stop_cleanup:
            return
        for cache_location in cache_locations:
            # Go bottom up in the hierarchy and delete old files
            for folder, dirs, files in os.walk(cache_location, topdown=False):
                for name in files:
                    # Check if we should stop and bail out immediately if so.
                    if self._stop_cleanup:
                        return
                    file_path = os.path.join(folder, name)
                    try:
                        file_stats = os.stat(file_path)
                        # Is it old enough to be removed?
                        if now_timestamp - file_stats.st_mtime > grace_in_seconds:
                            filesystem.safe_delete_file(file_path)
                    except Exception as e:
                        # Silently ignore the error
                        pass

                for name in dirs:
                    # Check if we should stop and bail out immediately if so.
                    if self._stop_cleanup:
                        return
                    # Try to remove empty directories
                    dir_path = os.path.join(folder, name)
                    try:
                        if not os.listdir(dir_path):
                            filesystem.safe_delete_folder(dir_path)
                    except Exception as e:
                        # Silently ignore the error
                        pass
def _sign_plugin(args):
    """
    Sign the built plugin (creates .zxp) and cleanup the build directory.
    """

    # extension_path is same as the plugin build dir with the .zxp extension
    extension_path = "%s.zxp" % (args["plugin_build_dir"], )

    # remove the existing build file
    if os.path.exists(extension_path):
        from sgtk.util.filesystem import safe_delete_file

        safe_delete_file(extension_path)

    (sign_command, certificate_path, certificate_pwd) = args["sign"]

    command = [
        sign_command,
        "-sign",
        args["plugin_build_dir"],
        extension_path,
        certificate_path,
        certificate_pwd,
        # The timestamp is not working at the moment and is not required so it's commented out for now
        # "-tsa",
        # TIMESTAMP_URL,
    ]

    # execute the build script
    logger.info("Signing extension command: %s" % (command, ))
    logger.info("Signing the extension...")
    status = subprocess.call(command)

    # check the return status
    if status:
        logger.error("Error signing the extension.")
        raise Exception("There was a problem signing the extension.")

    # clean up the plugin build directory
    try:
        shutil.rmtree(args["plugin_build_dir"])
    except Exception:
        logger.warning("Failed to remove plugin build dir.")

    # add the signed extension path to the args
    args["extension_path"] = extension_path
    logger.info("Signed extension: %s" % (args["extension_path"], ))
Ejemplo n.º 5
0
def _clean_plugin_dir(args):
    """
    Ensure the plugin dir is in a clean state before bundling.

    NOTE: Once signed, nothing in the plugin directory can be modified, so
    do any and all work to ensure it is in it's ready state.
    """

    # remove all .pyc files recursively
    logger.info("Cleaning built plugin directory...")
    from sgtk.util.filesystem import safe_delete_file
    for (root, dir_names, file_names) in os.walk(args["plugin_build_dir"]):
        for file_name in file_names:
            if file_name.endswith(".pyc"):
                full_path = os.path.join(root, file_name)
                logger.info("Removing pyc file: %s" % (full_path, ))
                safe_delete_file(full_path)
Ejemplo n.º 6
0
    def _delete_files(self, deletion_path, item):
        """
        This method handles deleting an item's path(s) from a designated location.

        If the item has "sequence_paths" set, it will attempt to delete all paths
        assuming they meet the required criteria.
        """

        publisher = self.parent

        # ---- get a list of files to be deleted
        if item.properties["is_sequence"]:
            work_files = item.properties.get("sequence_paths", [])
        else:
            work_files = [item.properties["path"]]

        # ---- delete the work files from the publish location
        processed_files = []
        for work_file in work_files:

            if item.properties["is_sequence"]:
                frame_num = publisher.util.get_frame_number(work_file)
                deletion_file = publisher.util.get_path_for_frame(deletion_path, frame_num)
            else:
                deletion_file = deletion_path

            # If the file paths are the same, skip...
            if work_file == deletion_file:
                continue

            # delete the file
            safe_delete_file(deletion_file)

            self.logger.debug(
                "Deleted publish file '%s'." % deletion_file
            )
            processed_files.append(deletion_file)

        return processed_files