3
from flexget.plugin import register_plugin, priority
5
log = logging.getLogger('free_space')
8
def get_free_space(folder):
9
""" Return folder/drive free space (in megabytes)"""
12
free_bytes = ctypes.c_ulonglong(0)
13
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes))
14
return free_bytes.value / (1024 * 1024)
16
stats = os.statvfs(folder)
17
return (stats.f_bavail * stats.f_frsize) / (1024 * 1024)
20
class PluginFreeSpace(object):
21
"""Aborts a feed if an entry is accepted and there is less than a certain amount of space free on a drive."""
24
from flexget import validator
25
root = validator.factory()
26
# Allow just the free space at the root
28
# Also allow a dict with path and free space
29
advanced = root.accept('dict')
30
advanced.accept('number', key='space', required=True)
31
advanced.accept('path', key='path')
34
def get_config(self, feed):
35
config = feed.config.get('free_space', {})
36
if isinstance(config, (float, int)):
37
config = {'space': config}
38
# Use config path if none is specified
39
if not config.get('path'):
40
config['path'] = feed.manager.config_base
44
def on_feed_download(self, feed):
45
config = self.get_config(feed)
46
# Only bother aborting if there were accepted entries this run.
48
if get_free_space(config['path']) < config['space']:
49
log.error('Less than %d MB of free space in %s aborting feed.' % (config['space'], config['path']))
53
register_plugin(PluginFreeSpace, 'free_space')