Browse Source

Handle incorrect shutdown process

doc-issue-template
MrS0m30n3 9 years ago
parent
commit
5ebf46fbe3
2 changed files with 37 additions and 8 deletions
  1. 10
      youtube_dl_gui/mainframe.py
  2. 35
      youtube_dl_gui/utils.py

10
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']:

35
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):

Loading…
Cancel
Save