-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6720 from freedomofpress/pyupgrade
Apply pyupgrade to Python files
- Loading branch information
Showing
119 changed files
with
345 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# -*- mode: python; coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2013-2018 Freedom of the Press Foundation & al | ||
# Copyright (C) 2018 Loic Dachary <[email protected]> | ||
|
@@ -53,9 +52,8 @@ def run_command(command: List[str]) -> Iterator[bytes]: | |
""" | ||
popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
if popen.stdout is None: | ||
raise EnvironmentError("Could not run command: None stdout") | ||
for stdout_line in iter(popen.stdout.readline, b""): | ||
yield stdout_line | ||
raise OSError("Could not run command: None stdout") | ||
yield from iter(popen.stdout.readline, b"") | ||
popen.stdout.close() | ||
return_code = popen.wait() | ||
if return_code: | ||
|
@@ -111,10 +109,8 @@ def install_apt_dependencies(args: argparse.Namespace) -> None: | |
""" | ||
sdlog.info("Installing SecureDrop Admin dependencies") | ||
sdlog.info( | ||
( | ||
"You'll be prompted for the temporary Tails admin password," | ||
" which was set on Tails login screen" | ||
) | ||
"You'll be prompted for the temporary Tails admin password," | ||
" which was set on Tails login screen" | ||
) | ||
|
||
apt_command = [ | ||
|
@@ -142,7 +138,7 @@ def install_apt_dependencies(args: argparse.Namespace) -> None: | |
# under Tails 2.x. If updates are being applied, don't try to pile | ||
# on with more apt requests. | ||
sdlog.error( | ||
("Failed to install apt dependencies. Check network" " connection and try again.") | ||
"Failed to install apt dependencies. Check network" " connection and try again." | ||
) | ||
raise | ||
|
||
|
@@ -181,7 +177,7 @@ def envsetup(args: argparse.Namespace, virtualenv_dir: str = VENV_DIR) -> None: | |
) | ||
except subprocess.CalledProcessError as e: | ||
sdlog.debug(e.output) | ||
sdlog.error(("Unable to create virtualenv. Check network settings" " and try again.")) | ||
sdlog.error("Unable to create virtualenv. Check network settings" " and try again.") | ||
sdlog.debug("Cleaning up virtualenv") | ||
if os.path.exists(virtualenv_dir): | ||
shutil.rmtree(virtualenv_dir) | ||
|
@@ -234,23 +230,21 @@ def install_pip_dependencies( | |
"only-if-needed", | ||
] | ||
|
||
sdlog.info("Checking {} for securedrop-admin".format(desc)) | ||
sdlog.info(f"Checking {desc} for securedrop-admin") | ||
try: | ||
pip_output = subprocess.check_output( | ||
maybe_torify() + pip_install_cmd, stderr=subprocess.STDOUT | ||
) | ||
except subprocess.CalledProcessError as e: | ||
sdlog.debug(e.output) | ||
sdlog.error( | ||
("Failed to install {}. Check network" " connection and try again.".format(desc)) | ||
) | ||
sdlog.error("Failed to install {}. Check network" " connection and try again.".format(desc)) | ||
raise | ||
|
||
sdlog.debug(pip_output) | ||
if "Successfully installed" in str(pip_output): | ||
sdlog.info("{} for securedrop-admin upgraded".format(desc)) | ||
sdlog.info(f"{desc} for securedrop-admin upgraded") | ||
else: | ||
sdlog.info("{} for securedrop-admin are up-to-date".format(desc)) | ||
sdlog.info(f"{desc} for securedrop-admin are up-to-date") | ||
|
||
|
||
def parse_argv(argv: List[str]) -> argparse.Namespace: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# -*- mode: python; coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2013-2018 Freedom of the Press Foundation & al | ||
# Copyright (C) 2018 Loic Dachary <[email protected]> | ||
|
@@ -26,7 +25,6 @@ | |
import argparse | ||
import base64 | ||
import functools | ||
import io | ||
import ipaddress | ||
import json | ||
import logging | ||
|
@@ -154,7 +152,7 @@ def split_list(text: str) -> List[str]: | |
class ValidatePath(Validator): | ||
def __init__(self, basedir: str) -> None: | ||
self.basedir = basedir | ||
super(SiteConfig.ValidatePath, self).__init__() | ||
super().__init__() | ||
|
||
def validate(self, document: Document) -> bool: | ||
if document.text == "": | ||
|
@@ -168,7 +166,7 @@ class ValidateOptionalPath(ValidatePath): | |
def validate(self, document: Document) -> bool: | ||
if document.text == "": | ||
return True | ||
return super(SiteConfig.ValidateOptionalPath, self).validate(document) | ||
return super().validate(document) | ||
|
||
class ValidateYesNo(Validator): | ||
def validate(self, document: Document) -> bool: | ||
|
@@ -192,15 +190,15 @@ class ValidateOptionalFingerprint(ValidateFingerprint): | |
def validate(self, document: Document) -> bool: | ||
if document.text == "": | ||
return True | ||
return super(SiteConfig.ValidateOptionalFingerprint, self).validate(document) | ||
return super().validate(document) | ||
|
||
class ValidateInt(Validator): | ||
def validate(self, document: Document) -> bool: | ||
if re.match(r"\d+$", document.text): | ||
return True | ||
raise ValidationError(message="Must be an integer") | ||
|
||
class Locales(object): | ||
class Locales: | ||
def __init__(self, appdir: str) -> None: | ||
self.translation_dir = os.path.realpath(os.path.join(appdir, "translations")) | ||
|
||
|
@@ -216,7 +214,7 @@ def __init__(self, basedir: str, supported: Set[str]) -> None: | |
present = SiteConfig.Locales(basedir).get_translations() | ||
self.available = present & supported | ||
|
||
super(SiteConfig.ValidateLocales, self).__init__() | ||
super().__init__() | ||
|
||
def validate(self, document: Document) -> bool: | ||
desired = document.text.split() | ||
|
@@ -252,7 +250,7 @@ def validate(self, document: Document) -> bool: | |
|
||
class ValidateOSSECEmail(ValidateEmail): | ||
def validate(self, document: Document) -> bool: | ||
super(SiteConfig.ValidateOSSECEmail, self).validate(document) | ||
super().validate(document) | ||
text = document.text | ||
if "[email protected]" != text: | ||
return True | ||
|
@@ -264,7 +262,7 @@ class ValidateOptionalEmail(ValidateEmail): | |
def validate(self, document: Document) -> bool: | ||
if document.text == "": | ||
return True | ||
return super(SiteConfig.ValidateOptionalEmail, self).validate(document) | ||
return super().validate(document) | ||
|
||
def __init__(self, args: argparse.Namespace) -> None: | ||
self.args = args | ||
|
@@ -604,9 +602,9 @@ def validate_gpg_keys(self) -> bool: | |
except subprocess.CalledProcessError as e: | ||
sdlog.debug(e.output) | ||
raise FingerprintException( | ||
"fingerprint {} ".format(fingerprint) | ||
f"fingerprint {fingerprint} " | ||
+ "does not match " | ||
+ "the public key {}".format(public_key) | ||
+ f"the public key {public_key}" | ||
) | ||
return True | ||
|
||
|
@@ -631,7 +629,7 @@ def exists(self) -> bool: | |
return os.path.exists(self.args.site_config) | ||
|
||
def save(self) -> None: | ||
with io.open(self.args.site_config, "w") as site_config_file: | ||
with open(self.args.site_config, "w") as site_config_file: | ||
yaml.safe_dump(self.config, site_config_file, default_flow_style=False) | ||
|
||
def clean_config(self, config: Dict) -> Dict: | ||
|
@@ -683,14 +681,14 @@ def load(self, validate: bool = True) -> Dict: | |
to current specifications. | ||
""" | ||
try: | ||
with io.open(self.args.site_config) as site_config_file: | ||
with open(self.args.site_config) as site_config_file: | ||
c = yaml.safe_load(site_config_file) | ||
return self.clean_config(c) if validate else c | ||
except IOError: | ||
except OSError: | ||
sdlog.error("Config file missing, re-run with sdconfig") | ||
raise | ||
except yaml.YAMLError: | ||
sdlog.error("There was an issue processing {}".format(self.args.site_config)) | ||
sdlog.error(f"There was an issue processing {self.args.site_config}") | ||
raise | ||
|
||
|
||
|
@@ -736,10 +734,10 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: | |
"You are not running the most recent signed SecureDrop release " | ||
"on this workstation." | ||
) | ||
sdlog.error("Latest available version: {}".format(latest_tag)) | ||
sdlog.error(f"Latest available version: {latest_tag}") | ||
|
||
if branch_status is not None: | ||
sdlog.error("Current branch status: {}".format(branch_status)) | ||
sdlog.error(f"Current branch status: {branch_status}") | ||
else: | ||
sdlog.error("Problem determining current branch status.") | ||
|
||
|
@@ -805,7 +803,7 @@ def find_or_generate_new_torv3_keys(args: argparse.Namespace) -> int: | |
""" | ||
secret_key_path = os.path.join(args.ansible_path, "tor_v3_keys.json") | ||
if os.path.exists(secret_key_path): | ||
print("Tor v3 onion service keys already exist in: {}".format(secret_key_path)) | ||
print(f"Tor v3 onion service keys already exist in: {secret_key_path}") | ||
return 0 | ||
# No old keys, generate and store them first | ||
app_journalist_public_key, app_journalist_private_key = generate_new_v3_keys() | ||
|
@@ -823,7 +821,7 @@ def find_or_generate_new_torv3_keys(args: argparse.Namespace) -> int: | |
} | ||
with open(secret_key_path, "w") as fobj: | ||
json.dump(tor_v3_service_info, fobj, indent=4) | ||
print("Tor v3 onion service keys generated and stored in: {}".format(secret_key_path)) | ||
print(f"Tor v3 onion service keys generated and stored in: {secret_key_path}") | ||
return 0 | ||
|
||
|
||
|
@@ -886,7 +884,7 @@ def restore_securedrop(args: argparse.Namespace) -> int: | |
] | ||
|
||
ansible_cmd_extras = [ | ||
"restore_file='{}'".format(restore_file_basename), | ||
f"restore_file='{restore_file_basename}'", | ||
] | ||
|
||
if args.restore_skip_tor: | ||
|
@@ -904,10 +902,8 @@ def run_tails_config(args: argparse.Namespace) -> int: | |
"""Configure Tails environment post SD install""" | ||
sdlog.info("Configuring Tails workstation environment") | ||
sdlog.info( | ||
( | ||
"You'll be prompted for the temporary Tails admin password," | ||
" which was set on Tails login screen" | ||
) | ||
"You'll be prompted for the temporary Tails admin password," | ||
" which was set on Tails login screen" | ||
) | ||
ansible_cmd = [ | ||
os.path.join(args.ansible_path, "securedrop-tails.yml"), | ||
|
@@ -1034,7 +1030,7 @@ def update(args: argparse.Namespace) -> int: | |
): | ||
# Finally, we check that there is no branch of the same name | ||
# prior to reporting success. | ||
cmd = ["git", "show-ref", "--heads", "--verify", "refs/heads/{}".format(latest_tag)] | ||
cmd = ["git", "show-ref", "--heads", "--verify", f"refs/heads/{latest_tag}"] | ||
try: | ||
# We expect this to produce a non-zero exit code, which | ||
# will produce a subprocess.CalledProcessError | ||
|
@@ -1063,7 +1059,7 @@ def update(args: argparse.Namespace) -> int: | |
git_checkout_cmd = ["git", "checkout", latest_tag] | ||
subprocess.check_call(git_checkout_cmd, cwd=args.root) | ||
|
||
sdlog.info("Updated to SecureDrop {}.".format(latest_tag)) | ||
sdlog.info(f"Updated to SecureDrop {latest_tag}.") | ||
return 0 | ||
|
||
|
||
|
@@ -1213,10 +1209,10 @@ def main(argv: List[str]) -> None: | |
print("Process was interrupted.") | ||
sys.exit(EXIT_INTERRUPT) | ||
except subprocess.CalledProcessError as e: | ||
print("ERROR (run with -v for more): {msg}".format(msg=e), file=sys.stderr) | ||
print(f"ERROR (run with -v for more): {e}", file=sys.stderr) | ||
sys.exit(EXIT_SUBPROCESS_ERROR) | ||
except Exception as e: | ||
raise SystemExit("ERROR (run with -v for more): {msg}".format(msg=e)) | ||
raise SystemExit(f"ERROR (run with -v for more): {e}") | ||
if return_code == 0: | ||
sys.exit(EXIT_SUCCESS) | ||
else: | ||
|
Oops, something went wrong.