mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
932 B
39 lines
932 B
import os
|
|
import shutil
|
|
from functools import wraps
|
|
|
|
from settings.api import settings
|
|
|
|
__all__ = ["WorkPostRunCleaner", "cleanup_post_run"]
|
|
|
|
|
|
class WorkPostRunCleaner:
|
|
@property
|
|
def clean_dir(self):
|
|
raise NotImplemented
|
|
|
|
def clean_post_run(self):
|
|
if settings.DEBUG_DEV:
|
|
return
|
|
if self.clean_dir and os.path.exists(self.clean_dir):
|
|
shutil.rmtree(self.clean_dir)
|
|
|
|
|
|
def cleanup_post_run(func):
|
|
def get_instance(*args):
|
|
if not len(args) > 0:
|
|
return
|
|
return args[0]
|
|
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
instance = get_instance(*args)
|
|
if not instance or not issubclass(type(instance), WorkPostRunCleaner):
|
|
raise NotImplementedError("you should extend 'WorkPostRunCleaner'")
|
|
try:
|
|
return func(*args, **kwargs)
|
|
finally:
|
|
instance.clean_post_run()
|
|
|
|
return wrapper
|