ayon-core/openpype/plugins/publish/cleanup_farm.py
Petr Kalis 1531708236
Global: persistent staging directory for renders (#4583)
* OP-4258 - Settings for transient template

* OP-4258 - added collector for transient staging dir

Allows setting profiles to create persistent stagingDir.

* OP-4258 - implemented persistent stagingDir in cleanup

* OP-4258 - updated logging

* OP-4258 - updated settings

* OP-4258 - Hound

* OP-4258 - renamed class to better name

* OP-4258 - changed location of Settings

Should be used in create and collecting phase also.

* OP-4258 - remove version placeholder from transient template

It was discussed that it shouldn't be used for now.

* OP-4258 - extracted transient dir query logic

This should be used in collection and creation phase for DCCs which are storing staging dir path directly into nodes.

* OP-4258 - added use of scene_name placeholder in collector

DCC dependent, way how to implement versioning, might not be used.

* OP-4258 - fix scene_name

* OP-4258 - remove wrong defaults

* OP-4258 - added possibility of different template name

Studio might want to put renders to different place from caches.

* OP-4258 - renamed according to GH comments

* OP-4258 - use is active filter

* OP-4258 - use is active filter

* OP-4793 - added project_settings to signature

* OP-4793 - updated logging message

* OP-4793 - added documentation

* OP-4258 - fix function arguments

* OP-4258 - updates to documentation


* OP-4258 - added known issues to documentation

---------

Co-authored-by: Roy Nieterau <roy_nieterau@hotmail.com>
2023-03-24 16:30:46 +01:00

72 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
"""Cleanup leftover files from publish."""
import os
import shutil
import pyblish.api
class CleanUpFarm(pyblish.api.ContextPlugin):
"""Cleans up the staging directory after a successful publish.
This will also clean published renders and delete their parent directories.
"""
order = pyblish.api.IntegratorOrder + 11
label = "Clean Up Farm"
enabled = True
# Keep "filesequence" for backwards compatibility of older jobs
targets = ["filesequence", "farm"]
allowed_hosts = ("maya", )
def process(self, context):
# Get source host from which farm publishing was started
src_host_name = context.data["hostName"]
self.log.debug("Host name from context is {}".format(src_host_name))
# Skip process if is not in list of source hosts in which this
# plugin should run
if src_host_name not in self.allowed_hosts:
self.log.info((
"Source host \"{}\" is not in list of enabled hosts {}."
" Skipping"
).format(str(src_host_name), str(self.allowed_hosts)))
return
self.log.debug("Preparing filepaths to remove")
# Collect directories to remove
dirpaths_to_remove = set()
for instance in context:
staging_dir = instance.data.get("stagingDir")
if staging_dir and not instance.data.get("stagingDir_persistent"):
dirpaths_to_remove.add(os.path.normpath(staging_dir))
if "representations" in instance.data:
for repre in instance.data["representations"]:
staging_dir = repre.get("stagingDir")
if staging_dir:
dirpaths_to_remove.add(os.path.normpath(staging_dir))
if not dirpaths_to_remove:
self.log.info("Nothing to remove. Skipping")
return
self.log.debug("Filepaths to remove are:\n{}".format(
"\n".join(["- {}".format(path) for path in dirpaths_to_remove])
))
# clean dirs which are empty
for dirpath in dirpaths_to_remove:
if not os.path.exists(dirpath):
self.log.debug("Skipping not existing directory \"{}\"".format(
dirpath
))
continue
self.log.debug("Removing directory \"{}\"".format(dirpath))
try:
shutil.rmtree(dirpath)
except OSError:
self.log.warning(
"Failed to remove directory \"{}\"".format(dirpath),
exc_info=True
)