Skip to content

Commit

Permalink
python 2/3 compatibility: cleaning up nits before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Apr 16, 2019
1 parent 3606e48 commit 3d3e0d8
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 114 deletions.
4 changes: 2 additions & 2 deletions molecule/testinfra/staging/app/test_apparmor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def test_apparmor_total_profiles(host):
""" Ensure number of total profiles is sum of enforced and
complaining profiles """
with host.sudo():
total_expected = str((len(sdvars.apparmor_enforce)
+ len(sdvars.apparmor_complain)))
total_expected = str(len(sdvars.apparmor_enforce)
+ len(sdvars.apparmor_complain))
# Trusty has ~10, Xenial about ~20 profiles, so let's expect
# *at least* the sum.
assert host.check_output("aa-status --profiled") >= total_expected
Expand Down
7 changes: 3 additions & 4 deletions securedrop/create-dev-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ def create_source_and_submissions(num_submissions=2, num_replies=2):

db.session.commit()

print(("Test source (codename: '{}', journalist designation '{}') "
"added with {} submissions and {} replies")
.format(codename, journalist_designation, num_submissions,
num_replies))
print("Test source (codename: '{}', journalist designation '{}') "
"added with {} submissions and {} replies").format(
codename, journalist_designation, num_submissions, num_replies)


if __name__ == "__main__": # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions securedrop/dockerfiles/xenial/python2/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ubuntu 16.04 image - 2019-01-22
FROM ubuntu@sha256:e4a134999bea4abb4a27bc437e6118fdddfb172e1b9d683129b74d254af51675
# ubuntu 16.04 image from 2019-03-12
FROM ubuntu@sha256:58d0da8bc2f434983c6ca4713b08be00ff5586eb5cdff47bcde4b2e88fd40f88
ARG USER_NAME
ENV USER_NAME ${USER_NAME:-root}
ARG USER_ID
Expand Down
4 changes: 2 additions & 2 deletions securedrop/i18n_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ def add(p):
def upstream_commit(self, args, code):
self.require_git_email_name(args.root)
authors = set()
diffs = str(git('--no-pager', '-C', args.root,
'diff', '--name-only', '--cached').stdout)
diffs = six.text_type(git('--no-pager', '-C', args.root,
'diff', '--name-only', '--cached').stdout)
for path in diffs.strip().split('\n'):
previous_message = str(git(
'--no-pager', '-C', args.root, 'log', '-n', '1', path,
Expand Down
2 changes: 1 addition & 1 deletion securedrop/journalist_app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def all_source_replies(source_uuid):

user = get_user_object(request)

data = json.loads(request.data.decode('utf-8'))
data = request.json
if not data['reply']:
abort(400, 'reply should not be empty')

Expand Down
36 changes: 18 additions & 18 deletions securedrop/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _get_username():
try:
Journalist.check_username_acceptable(username)
except InvalidUsernameException as e:
print(('Invalid username: ' + str(e)))
print('Invalid username: ' + str(e))
else:
return username

Expand Down Expand Up @@ -154,7 +154,7 @@ def _add_user(is_admin=False):

print("Note: Passwords are now autogenerated.")
password = _make_password()
print(("This user's password is: {}".format(password)))
print("This user's password is: {}".format(password))

is_hotp = _get_yubikey_usage()
otp_secret = None
Expand All @@ -166,9 +166,9 @@ def _add_user(is_admin=False):
if otp_secret:
tmp_str = otp_secret.replace(" ", "")
if len(tmp_str) != 40:
print(("The length of the secret is not correct. "
"Expected 40 characters, but received {0}. "
"Try again.".format(len(tmp_str))))
print("The length of the secret is not correct. "
"Expected 40 characters, but received {0}. "
"Try again.".format(len(tmp_str)))
continue
if otp_secret:
break
Expand All @@ -186,11 +186,11 @@ def _add_user(is_admin=False):
print('ERROR: That username is already taken!')
else:
exc_type, exc_value, exc_traceback = sys.exc_info()
print((repr(traceback.format_exception(exc_type, exc_value,
exc_traceback))))
print(repr(traceback.format_exception(exc_type, exc_value,
exc_traceback)))
return 1
else:
print(('User "{}" successfully added'.format(username)))
print('User "{}" successfully added'.format(username))
if not otp_secret:
# Print the QR code for FreeOTP
print('\nScan the QR code below with FreeOTP:\n')
Expand All @@ -199,13 +199,13 @@ def _add_user(is_admin=False):
qr = qrcode.QRCode()
qr.add_data(uri)
qr.print_ascii(tty=sys.stdout.isatty())
print(('\nIf the barcode does not render correctly, try '
"changing your terminal's font (Monospace for Linux, "
'Menlo for OS X). If you are using iTerm on Mac OS X, '
'you will need to change the "Non-ASCII Font", which '
"is your profile\'s Text settings.\n\nCan't scan the "
'barcode? Enter following shared secret manually:'
'\n{}\n'.format(user.formatted_otp_secret)))
print('\nIf the barcode does not render correctly, try '
"changing your terminal's font (Monospace for Linux, "
'Menlo for OS X). If you are using iTerm on Mac OS X, '
'you will need to change the "Non-ASCII Font", which '
"is your profile\'s Text settings.\n\nCan't scan the "
'barcode? Enter following shared secret manually:'
'\n{}\n'.format(user.formatted_otp_secret))
return 0


Expand All @@ -217,8 +217,8 @@ def _get_delete_confirmation(user):
confirmation = obtain_input('Are you sure you want to delete user '
'"{}" (y/n)?'.format(user))
if confirmation.lower() != 'y':
print(('Confirmation not received: user "{}" was NOT '
'deleted'.format(user)))
print('Confirmation not received: user "{}" was NOT '
'deleted'.format(user))
return False
return True

Expand Down Expand Up @@ -253,7 +253,7 @@ def delete_user(args):
else:
raise e

print(('User "{}" successfully deleted'.format(username)))
print('User "{}" successfully deleted'.format(username))
return 0


Expand Down
7 changes: 3 additions & 4 deletions securedrop/secure_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ def __init__(self, store_dir):
"""
self.last_action = 'init'
self.create_key()
self.temp_file_id = ""
data = base64.urlsafe_b64encode(os.urandom(32))

data = base64.urlsafe_b64encode(os.urandom(32))
if not six.PY2: # For Python3
self.tmp_file_id = data.decode('utf-8').strip('=')
else:
Expand All @@ -69,8 +68,8 @@ def create_key(self):
grsecurity-patched kernel it uses (for further details consult
https://github.com/freedomofpress/securedrop/pull/477#issuecomment-168445450).
"""
self.key = os.urandom(int(self.AES_key_size / 8))
self.iv = os.urandom(int(self.AES_block_size / 8))
self.key = os.urandom(self.AES_key_size // 8)
self.iv = os.urandom(self.AES_block_size // 8)
self.initialize_cipher()

def initialize_cipher(self):
Expand Down
3 changes: 1 addition & 2 deletions securedrop/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ def journalist_api_token(journalist_app, test_journo):
'passphrase': test_journo['password'],
'one_time_code': valid_token}),
headers=utils.api_helper.get_api_headers())
observed_response = json.loads(response.data.decode('utf-8'))
return observed_response['token']
return response.json['token']


def _start_test_rqworker(config):
Expand Down
2 changes: 1 addition & 1 deletion securedrop/tests/functional/journalist_navigation_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _add_user(self, username, is_admin=False, hotp=None):
if hotp:
hotp_checkbox = self.driver.find_element_by_css_selector(
'input[name="is_hotp"]')
print((str(hotp_checkbox.__dict__)))
print(str(hotp_checkbox.__dict__))
hotp_checkbox.click()
hotp_secret = self.driver.find_element_by_css_selector(
'input[name="otp_secret"]')
Expand Down
2 changes: 1 addition & 1 deletion securedrop/tests/i18n/code.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from flask_babel import gettext

print((gettext('code hello i18n')))
print(gettext('code hello i18n'))

0 comments on commit 3d3e0d8

Please sign in to comment.