Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Apply black formatting to all Python files
Browse files Browse the repository at this point in the history
  • Loading branch information
eloquence committed Jun 11, 2020
1 parent 4ddbe08 commit 99c931e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 187 deletions.
12 changes: 3 additions & 9 deletions launcher/sdw_notify/Notify.py
Expand Up @@ -60,9 +60,7 @@ def is_update_check_necessary():
sdlog.error(
"Data in {} not in the expected format. "
"Expecting a timestamp in format '{}'. "
"Showing security warning.".format(
LAST_UPDATED_FILE, LAST_UPDATED_FORMAT
)
"Showing security warning.".format(LAST_UPDATED_FILE, LAST_UPDATED_FORMAT)
)
return True

Expand All @@ -76,9 +74,7 @@ def is_update_check_necessary():
if not last_updated_file_exists:
sdlog.warning(
"Timestamp file '{}' does not exist. "
"Updater may never have run. Showing security warning.".format(
LAST_UPDATED_FILE
)
"Updater may never have run. Showing security warning.".format(LAST_UPDATED_FILE)
)
return True
else:
Expand Down Expand Up @@ -113,9 +109,7 @@ def is_update_check_necessary():
sdlog.info(
"Last successful update ({0:.1f} hours ago) "
"is below the warning threshold ({1:.1f} hours). "
"Exiting without warning.".format(
updated_hours_ago, warning_threshold_hours
)
"Exiting without warning.".format(updated_hours_ago, warning_threshold_hours)
)
return False

Expand Down
40 changes: 9 additions & 31 deletions launcher/sdw_updater_gui/Updater.py
Expand Up @@ -54,9 +54,7 @@ def check_all_updates():

for progress_current, vm in enumerate(current_templates.keys()):
# yield the progress percentage for UI updates
progress_percentage = int(
((progress_current + 1) / len(current_templates.keys())) * 100
)
progress_percentage = int(((progress_current + 1) / len(current_templates.keys())) * 100)
update_results = check_updates(vm)
yield vm, progress_percentage, update_results

Expand Down Expand Up @@ -129,17 +127,11 @@ def _check_updates_debian(vm):
sdlog.info("Checking for updates {}:{}".format(vm, current_templates[vm]))
subprocess.check_call(["qvm-run", current_templates[vm], "sudo apt update"])
subprocess.check_call(
[
"qvm-run",
current_templates[vm],
"[[ $(apt list --upgradable | wc -l) -eq 1 ]]",
]
["qvm-run", current_templates[vm], "[[ $(apt list --upgradable | wc -l) -eq 1 ]]",]
)
except subprocess.CalledProcessError as e:
sdlog.error(
"Updates required for {} or cannot check for updates".format(
current_templates[vm]
)
"Updates required for {} or cannot check for updates".format(current_templates[vm])
)
sdlog.error(str(e))
updates_required = True
Expand All @@ -164,9 +156,7 @@ def _apply_updates_dom0():
try:
subprocess.check_call(["sudo", "qubes-dom0-update", "-y"])
except subprocess.CalledProcessError as e:
sdlog.error(
"An error has occurred updating dom0. Please contact your administrator."
)
sdlog.error("An error has occurred updating dom0. Please contact your administrator.")
sdlog.error(str(e))
return UpdateStatus.UPDATES_FAILED
sdlog.info("dom0 update successful")
Expand Down Expand Up @@ -247,11 +237,7 @@ def _write_updates_status_flag_to_disk(status):
try:
sdlog.info("Setting update flag to {} in sd-app".format(status.value))
subprocess.check_call(
[
"qvm-run",
"sd-app",
"echo '{}' > {}".format(status.value, flag_file_path_sd_app),
]
["qvm-run", "sd-app", "echo '{}' > {}".format(status.value, flag_file_path_sd_app),]
)
except subprocess.CalledProcessError as e:
sdlog.error("Error writing update status flag to sd-app")
Expand Down Expand Up @@ -286,9 +272,7 @@ def last_required_reboot_performed():
return True

if int(flag_contents["status"]) == int(UpdateStatus.REBOOT_REQUIRED.value):
reboot_time = datetime.strptime(
flag_contents["last_status_update"], DATE_FORMAT
)
reboot_time = datetime.strptime(flag_contents["last_status_update"], DATE_FORMAT)
boot_time = datetime.now() - _get_uptime()

# The session was started *before* the reboot was requested by
Expand All @@ -315,9 +299,7 @@ def _get_uptime():
uptime_minutes = (uptime % 3600) // 60
uptime_seconds = uptime % 60

delta = timedelta(
hours=uptime_hours, minutes=uptime_minutes, seconds=uptime_seconds
)
delta = timedelta(hours=uptime_hours, minutes=uptime_minutes, seconds=uptime_seconds)

return delta

Expand Down Expand Up @@ -471,9 +453,7 @@ def _safely_start_vm(vm):
["qvm-ls", "--running", "--raw-list"], stderr=subprocess.PIPE
)
sdlog.info("VMs running before start of {}: {}".format(vm, running_vms))
subprocess.check_output(
["qvm-start", "--skip-if-running", vm], stderr=subprocess.PIPE
)
subprocess.check_output(["qvm-start", "--skip-if-running", vm], stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
sdlog.error("Error while starting {}".format(vm))
sdlog.error(str(e))
Expand All @@ -493,9 +473,7 @@ def should_launch_updater(interval):
return False
elif status["status"] == UpdateStatus.REBOOT_REQUIRED.value:
if last_required_reboot_performed():
sdlog.info(
"Required reboot performed, updating status and launching client."
)
sdlog.info("Required reboot performed, updating status and launching client.")
_write_updates_status_flag_to_disk(UpdateStatus.UPDATES_OK)
return False
else:
Expand Down
36 changes: 9 additions & 27 deletions launcher/sdw_updater_gui/UpdaterApp.py
Expand Up @@ -46,9 +46,7 @@ def __init__(self, parent=None):

self.show()

self.proposedActionDescription.setText(
strings.description_status_checking_updates
)
self.proposedActionDescription.setText(strings.description_status_checking_updates)

self.progress += 1
self.progressBar.setProperty("value", self.progress)
Expand Down Expand Up @@ -78,18 +76,14 @@ def update_status(self, result):
self.applyUpdatesButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_updates_available
)
self.proposedActionDescription.setText(strings.description_status_updates_available)
elif result["recommended_action"] == UpdateStatus.UPDATES_OK:
logger.info("VMs up-to-date, OK to start client")
self.clientOpenButton.setEnabled(True)
self.clientOpenButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_up_to_date
)
self.proposedActionDescription.setText(strings.description_status_up_to_date)
elif result["recommended_action"] == UpdateStatus.REBOOT_REQUIRED:
logger.info("Reboot will be required")
# We also have further updates to do, let's apply updates and reboot
Expand All @@ -101,25 +95,19 @@ def update_status(self, result):
self.applyUpdatesButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_updates_available
)
self.proposedActionDescription.setText(strings.description_status_updates_available)
# No updates required, show reboot button.
else:
logger.info("Reboot required")
self.rebootButton.setEnabled(True)
self.rebootButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_reboot_required
)
self.proposedActionDescription.setText(strings.description_status_reboot_required)
else:
logger.error("Error checking for updates")
logger.error(str(result))
self.proposedActionDescription.setText(
strings.description_error_check_updates_failed
)
self.proposedActionDescription.setText(strings.description_error_check_updates_failed)

@pyqtSlot(dict)
def upgrade_status(self, result):
Expand All @@ -137,18 +125,14 @@ def upgrade_status(self, result):
self.rebootButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_reboot_required
)
self.proposedActionDescription.setText(strings.description_status_reboot_required)
elif result["recommended_action"] == UpdateStatus.UPDATES_OK:
logger.info("VMs have been succesfully updated, OK to start client")
self.clientOpenButton.setEnabled(True)
self.clientOpenButton.show()
self.cancelButton.setEnabled(True)
self.cancelButton.show()
self.proposedActionDescription.setText(
strings.description_status_updates_complete
)
self.proposedActionDescription.setText(strings.description_status_updates_complete)
else:
logger.info("Error upgrading VMs")
self.cancelButton.setEnabled(True)
Expand Down Expand Up @@ -194,9 +178,7 @@ def apply_all_updates(self):
logger.info("Starting UpgradeThread")
self.progress = 5
self.progressBar.setProperty("value", self.progress)
self.proposedActionDescription.setText(
strings.description_status_applying_updates
)
self.proposedActionDescription.setText(strings.description_status_applying_updates)
self.applyUpdatesButton.setEnabled(False)
self.applyUpdatesButton.hide()
self.cancelButton.setEnabled(False)
Expand Down
12 changes: 3 additions & 9 deletions launcher/sdw_updater_gui/UpdaterAppUi.py
Expand Up @@ -78,9 +78,7 @@ def setupUi(self, UpdaterDialog):
QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop
)
self.proposedActionDescription.setWordWrap(True)
self.proposedActionDescription.setObjectName(
_fromUtf8("proposedActionDescription")
)
self.proposedActionDescription.setObjectName(_fromUtf8("proposedActionDescription"))
self.gridLayout.addWidget(self.proposedActionDescription, 3, 0, 1, 5)
self.label = QtGui.QLabel(self.layoutWidget)
self.label.setMinimumSize(QtCore.QSize(0, 20))
Expand All @@ -94,14 +92,10 @@ def setupUi(self, UpdaterDialog):

def retranslateUi(self, UpdaterDialog):
UpdaterDialog.setWindowTitle(
_translate(
"UpdaterDialog", "SecureDrop Workstation preflight updater", None
)
_translate("UpdaterDialog", "SecureDrop Workstation preflight updater", None)
)
self.rebootButton.setText(_translate("UpdaterDialog", "Reboot", None))
self.applyUpdatesButton.setText(
_translate("UpdaterDialog", "Start Updates", None)
)
self.applyUpdatesButton.setText(_translate("UpdaterDialog", "Start Updates", None))
self.cancelButton.setText(_translate("UpdaterDialog", "Cancel", None))
self.clientOpenButton.setText(_translate("UpdaterDialog", "Continue", None))
self.proposedActionDescription.setText(
Expand Down
6 changes: 2 additions & 4 deletions launcher/sdw_updater_gui/strings.py
Expand Up @@ -15,8 +15,7 @@
"to continue.</p>"
)
description_status_up_to_date = (
"<h3> No updates today!</h3>"
"<p>Click <em>Continue</em> to launch the SecureDrop app.</p>"
"<h3> No updates today!</h3>" "<p>Click <em>Continue</em> to launch the SecureDrop app.</p>"
)
description_error_check_updates_failed = (
"<h3> Cannot check for updates </h3> There was an error retrieving updates. "
Expand Down Expand Up @@ -51,6 +50,5 @@
"<h3> Error rebooting workstation</h3>" "<p>Please contact your administrator.</p>"
)
description_error_lauching_client = (
"<h3> Error launching the SecureDrop application</h3>"
"Please contact your administrator."
"<h3> Error launching the SecureDrop application</h3>" "Please contact your administrator."
)
4 changes: 1 addition & 3 deletions launcher/sdw_util/Util.py
Expand Up @@ -22,9 +22,7 @@
LOCK_ERROR = "Error obtaining lock on '{}'. Process may already be running."

# Format for those logs
LOG_FORMAT = (
"%(asctime)s - %(name)s:%(lineno)d(%(funcName)s) " "%(levelname)s: %(message)s"
)
LOG_FORMAT = "%(asctime)s - %(name)s:%(lineno)d(%(funcName)s) " "%(levelname)s: %(message)s"

sdlog = logging.getLogger(__name__)

Expand Down
11 changes: 3 additions & 8 deletions launcher/tests/test_notify.py
Expand Up @@ -8,15 +8,11 @@
from tempfile import TemporaryDirectory

relpath_notify = "../sdw_notify/Notify.py"
path_to_notify = os.path.join(
os.path.dirname(os.path.abspath(__file__)), relpath_notify
)
path_to_notify = os.path.join(os.path.dirname(os.path.abspath(__file__)), relpath_notify)
notify = SourceFileLoader("Notify", path_to_notify).load_module()

relpath_updater = "../sdw_updater_gui/Updater.py"
path_to_updater = os.path.join(
os.path.dirname(os.path.abspath(__file__)), relpath_updater
)
path_to_updater = os.path.join(os.path.dirname(os.path.abspath(__file__)), relpath_updater)
updater = SourceFileLoader("Updater", path_to_updater).load_module()


Expand All @@ -39,8 +35,7 @@

# Regex for info log if we've updated recently enough
NO_WARNING_REGEX = (
r"Last successful update \(.* hours ago\) is below the warning threshold "
r"\(.* hours\)."
r"Last successful update \(.* hours ago\) is below the warning threshold " r"\(.* hours\)."
)

# Regex for bad contents in `sdw-last-updated` file
Expand Down

0 comments on commit 99c931e

Please sign in to comment.