# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms of the AGPLv3 License"
import collections
import logging
import sarge
import threading
from flask import request, make_response, jsonify, url_for
from flask_babel import gettext
import psutil
from octoprint.settings import settings as s
from octoprint.server import NO_CONTENT
from octoprint.server.api import api
from octoprint.server.util.flask import no_firstrun_access, get_remote_address
from octoprint.access.permissions import Permissions
from octoprint.logging import prefix_multilines
from octoprint.util.platform import CLOSE_FDS
@api.route("/system/usage", methods=["GET"])
@no_firstrun_access
@Permissions.SYSTEM.require(403)
def readUsageForFolders():
return jsonify(usage=_usageForFolders())
def _usageForFolders():
data = {}
for folder_name in s().get(['folder']).keys():
path = s().getBaseFolder(folder_name, check_writable=False)
if path is not None:
usage = psutil.disk_usage(path)
data[folder_name] = { 'free': usage.free, 'total': usage.total }
return data
@api.route("/system", methods=["POST"])
@no_firstrun_access
@Permissions.SYSTEM.require(403)
def performSystemAction():
logging.getLogger(__name__).warning("Deprecated API call to /api/system made by {}, should be migrated to use /system/commands/custom/
This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), reboot=dict( command=s().get(["server", "commands", "systemRestartCommand"]), name=gettext("Reboot system"), confirm=gettext(u"You are about to reboot the system.
This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), restart=dict( command=s().get(["server", "commands", "serverRestartCommand"]), name=gettext("Restart OctoPrint"), confirm=gettext(u"You are about to restart the OctoPrint server.
This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), restart_safe=dict( command=s().get(["server", "commands", "serverRestartCommand"]), name=gettext("Restart OctoPrint in safe mode"), confirm=gettext(u"You are about to restart the OctoPrint server in safe mode.
This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage)."), before=enable_safe_mode) ) available_commands = collections.OrderedDict() for action, spec in commands.items(): if not spec["command"]: continue spec.update({'action': action, 'source': 'core', 'async': True, 'debug': True}) available_commands[action] = spec return available_commands def _get_core_command_spec(action): available_actions = _get_core_command_specs() if not action in available_actions: logging.getLogger(__name__).warning(u"Command for core action {} is not configured, you need to configure the command before it can be used".format(action)) return None return available_actions[action] def _get_custom_command_specs(): specs = collections.OrderedDict() dividers = 0 for spec in s().get(["system", "actions"]): if not "action" in spec: continue copied = dict(spec) copied["source"] = "custom" action = spec["action"] if action == "divider": dividers += 1 action = "divider_{}".format(dividers) specs[action] = copied return specs def _get_custom_command_spec(action): available_actions = _get_custom_command_specs() if not action in available_actions: return None return available_actions[action] class CommandFailed(Exception): def __init__(self, error): self.error = error