From 5ebf46fbe31a4277a27304b51bfff8f9b1894902 Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Mon, 9 Mar 2015 17:41:05 +0200 Subject: [PATCH] Handle incorrect shutdown process --- youtube_dl_gui/mainframe.py | 10 +++++++++- youtube_dl_gui/utils.py | 35 ++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/youtube_dl_gui/mainframe.py b/youtube_dl_gui/mainframe.py index 2d375c6..259e16d 100644 --- a/youtube_dl_gui/mainframe.py +++ b/youtube_dl_gui/mainframe.py @@ -100,6 +100,9 @@ class MainFrame(wx.Frame): OPEN_DIR_ERR = _("Unable to open directory: '{dir}'. " "The specified path does not exist") + SHUTDOWN_ERR = _("Error while shutting down. " + "Make sure you typed the correct password") + SHUTDOWN_MSG = _("Shutting down system") VIDEO_LABEL = _("Title") SIZE_LABEL = _("Size") @@ -268,7 +271,12 @@ class MainFrame(wx.Frame): """ if self.opt_manager.options['shutdown']: self.opt_manager.save_to_file() - shutdown_sys(self.opt_manager.options['sudo_password']) + success = shutdown_sys(self.opt_manager.options['sudo_password']) + + if success: + self._status_bar_write(self.SHUTDOWN_MSG) + else: + self._status_bar_write(self.SHUTDOWN_ERR) else: self._create_popup(self.DL_COMPLETED_MSG, self.INFO_LABEL, wx.OK | wx.ICON_INFORMATION) if self.opt_manager.options['open_dl_dir']: diff --git a/youtube_dl_gui/utils.py b/youtube_dl_gui/utils.py index e6c8e31..e334a8e 100644 --- a/youtube_dl_gui/utils.py +++ b/youtube_dl_gui/utils.py @@ -82,8 +82,9 @@ def get_config_path(): return path -def shutdown_sys(password=''): +def shutdown_sys(password=None): """Shuts down the system. + Returns True if no errors occur else False. Args: password (string): SUDO password for linux. @@ -93,15 +94,35 @@ def shutdown_sys(password=''): have elevated privileges. """ + _stderr = subprocess.PIPE + _stdin = None + info = None + encoding = get_encoding() + if os.name == 'nt': - subprocess.call(['shutdown', '/s', '/t', '1']) + cmd = ['shutdown', '/s', '/t', '1'] + + # Hide subprocess window + info = subprocess.STARTUPINFO() + info.dwFlags |= subprocess.STARTF_USESHOWWINDOW else: - if not password: - subprocess.call(['/sbin/shutdown', '-h', 'now']) + if password: + _stdin = subprocess.PIPE + password = ('%s\n' % password).encode(encoding) + cmd = ['sudo', '-S', '/sbin/shutdown', '-h', 'now'] else: - password = ('%s\n' % password).encode(get_encoding()) - subprocess.Popen(['sudo', '-S', '/sbin/shutdown', '-h', 'now'], - stdin=subprocess.PIPE).communicate(password) + cmd = ['/sbin/shutdown', '-h', 'now'] + + cmd = [item.encode(encoding, 'ignore') for item in cmd] + + shutdown_proc = subprocess.Popen(cmd, + stderr=_stderr, + stdin=_stdin, + startupinfo=info) + + output = shutdown_proc.communicate(password)[1] + + return not output or output == "Password:" def to_string(data):