Skip to content

Commit b1ee410

Browse files
authored
Merge pull request #66 from freedomofpress/continue-flake8-linting
Completes flake8 linting of Python files
2 parents 5e5cf4b + 4c62353 commit b1ee410

File tree

8 files changed

+196
-113
lines changed

8 files changed

+196
-113
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ flake8: ## Lints all Python files with flake8
117117
# Not requiring dom0 since linting requires extra packages,
118118
# available only in the developer environment, i.e. Work VM.
119119
@flake8 .
120+
@find -type f -exec file -i {} + \
121+
| perl -F':\s+' -nE '$$F[1] =~ m/text\/x-python/ and say $$F[0]' \
122+
| xargs flake8
120123

121124
# Explanation of the below shell command should it ever break.
122125
# 1. Set the field separator to ": ##" to parse lines for make targets.

sd-decrypt/decrypt-sd-submission

+42-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
32
import sys
43
import tarfile
54
import tempfile
@@ -12,11 +11,12 @@ import shutil
1211

1312

1413
def send_progress(msg):
15-
p = subprocess.Popen(["qrexec-client-vm","sd-journalist",
16-
"sd-process.Feedback"],
17-
close_fds=True,
18-
stdin=subprocess.PIPE)
19-
p.communicate(msg)
14+
p = subprocess.Popen(["qrexec-client-vm", "sd-journalist",
15+
"sd-process.Feedback"],
16+
close_fds=True,
17+
stdin=subprocess.PIPE)
18+
p.communicate(msg)
19+
2020

2121
input = sys.argv[1]
2222

@@ -29,61 +29,63 @@ tmpdir = tempfile.mkdtemp()
2929
# first step, extract that archive
3030
try:
3131

32-
with tarfile.open(input) as tar:
33-
# potentially unsafe, can create arbitrary files on the filesystem
34-
# given a malicious tarball
35-
tar.extractall(tmpdir)
32+
with tarfile.open(input) as tar:
33+
# potentially unsafe, can create arbitrary files on the filesystem
34+
# given a malicious tarball
35+
tar.extractall(tmpdir)
3636

3737
except Exception as e:
38-
send_progress("DECRYPTION_BUNDLE_OPEN_FAILURE")
39-
sys.exit(0) # although we're exiting with failure, we return a
40-
# 0 exit code so xdg does not try to re-open this file with
41-
# another application
38+
send_progress("DECRYPTION_BUNDLE_OPEN_FAILURE")
39+
# although we're exiting with failure, we return a
40+
# 0 exit code so xdg does not try to re-open this file with
41+
# another application
42+
sys.exit(0)
4243

4344
send_progress("SUBMISSION_BUNDLE_UNBUNDLED")
4445

4546
# everything in the archive should be zips, as created by SD
4647
# let's unzip those here
4748
zips = glob.glob(tmpdir + "/*zip")
4849
for z in zips:
49-
with zipfile.ZipFile(z) as zf:
50-
zf.extractall(tmpdir + "/extracted/")
51-
os.unlink(z)
50+
with zipfile.ZipFile(z) as zf:
51+
zf.extractall(tmpdir + "/extracted/")
52+
os.unlink(z)
5253

5354
send_progress("SUBMISSION_FILES_EXTRACTED")
5455

5556
# great, we should be left with a directory tree filled with files
5657
# encrypted to our public key. let's find those and decrypt them
5758
for root, dirnames, filenames in os.walk(tmpdir):
58-
for fn in fnmatch.filter(filenames, '*.gpg'):
59-
[fn_no_ext, _] = os.path.splitext(fn)
60-
61-
out = open(os.path.join(root, fn_no_ext), 'w')
62-
err = tempfile.NamedTemporaryFile(suffix=".gpg-err")
63-
res = subprocess.call(["qubes-gpg-client", "--decrypt" , os.path.join(root, fn)], stdout=out, stderr=err)
64-
out.close()
65-
os.unlink(os.path.join(root, fn))
66-
if res != 0:
67-
os.unlink(os.path.join(root, fn_no_ext))
68-
with open(err.name) as e:
69-
msg = e.read()
70-
send_progress("SUBMISSION_FILE_DECRYPTION_FAILED")
71-
else:
72-
send_progress("SUBMISSION_FILE_DECRYPTION_SUCCEEDED")
73-
err.close()
59+
for fn in fnmatch.filter(filenames, '*.gpg'):
60+
[fn_no_ext, _] = os.path.splitext(fn)
61+
62+
out = open(os.path.join(root, fn_no_ext), 'w')
63+
err = tempfile.NamedTemporaryFile(suffix=".gpg-err")
64+
cmd = ["qubes-gpg-client", "--decrypt", os.path.join(root, fn)]
65+
res = subprocess.call(cmd, stdout=out, stderr=err)
66+
out.close()
67+
os.unlink(os.path.join(root, fn))
68+
if res != 0:
69+
os.unlink(os.path.join(root, fn_no_ext))
70+
with open(err.name) as e:
71+
msg = e.read()
72+
send_progress("SUBMISSION_FILE_DECRYPTION_FAILED")
73+
else:
74+
send_progress("SUBMISSION_FILE_DECRYPTION_SUCCEEDED")
75+
err.close()
7476

7577
# almost done. docs are gzipped. let's ungzip them.
7678
any_files = False
7779
for root, dirnames, filenames in os.walk(tmpdir):
78-
for fn in fnmatch.filter(filenames, '*.gz'):
79-
any_files = True
80-
# maybe sorta lazy, could do this using python gzip module.
81-
# XXX also catch errors here...
82-
subprocess.call(["gunzip", os.path.join(root, fn)])
80+
for fn in fnmatch.filter(filenames, '*.gz'):
81+
any_files = True
82+
# maybe sorta lazy, could do this using python gzip module.
83+
# XXX also catch errors here...
84+
subprocess.call(["gunzip", os.path.join(root, fn)])
8385

8486
if not any_files:
85-
send_progress("SUBMISSION_FILE_NO_FILES_FOUND")
86-
sys.exit(0)
87+
send_progress("SUBMISSION_FILE_NO_FILES_FOUND")
88+
sys.exit(0)
8789

8890
# ok. we're going to send all the decrypted stuff to the svs vm.
8991
# let's tar it all up again, so we can `qvm-open-in-vm` it.

sd-journalist/do-not-open-here

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import sys
44
from PyQt4 import Qt
55

6+
67
a = Qt.QApplication(sys.argv)
78

8-
nope = Qt.QLabel("Please do not use this VM to open any files aside from those downloaded from SecureDrop.")
9+
nope = Qt.QLabel("Please do not use this VM to open any files"
10+
" aside from those downloaded from SecureDrop.")
911

1012
nope.show()
1113
a.exec_()

sd-journalist/move-to-svs

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import tempfile
77
import os.path
88
import subprocess
99

10-
TB_DL_LOC="/home/user/.tb/tor-browser/Browser/Downloads"
10+
TB_DL_LOC = "/home/user/.tb/tor-browser/Browser/Downloads"
1111

1212
zips = glob.glob(TB_DL_LOC + "/*.zip")
1313

14-
matched_sd_pattern = [re.match('[a-z]+\_[a-z]+[\d-]+\.zip', os.path.basename(z)) for z in zips]
14+
matched_sd_pattern = [
15+
re.match('[a-z]+\_[a-z]+[\d-]+\.zip', os.path.basename(z)) for z in zips]
1516

1617
probable_sd_downloads = [z.group(0) for z in matched_sd_pattern if z]
1718

@@ -23,10 +24,11 @@ print "fh name " + fh.name
2324
out_tar = tarfile.open(mode='w', fileobj=fh)
2425

2526
for f in probable_sd_downloads:
26-
out_tar.add(os.path.join(TB_DL_LOC, f), arcname=f)
27+
out_tar.add(os.path.join(TB_DL_LOC, f), arcname=f)
2728

2829
out_tar.close()
2930
fh.close()
3031

3132
# ship this to the next phase
32-
subprocess.call(["qvm-open-in-vm", "$dispvm:sd-dispvm", os.path.join(TB_DL_LOC, fh.name)])
33+
cmd = ["qvm-open-in-vm", "$dispvm:sd-dispvm", os.path.join(TB_DL_LOC, fh.name)]
34+
subprocess.call(cmd)

sd-journalist/sd-process-display

+35-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import sys
22
import PyQt4.QtCore as QtCore
3-
from PyQt4.QtGui import QDialog, QDialogButtonBox, QApplication, QLabel, QVBoxLayout, QImage, QPixmap
3+
from PyQt4.QtGui import QDialog, QDialogButtonBox, QApplication, \
4+
QLabel, QVBoxLayout, QImage, QPixmap
45
from PyQt4.QtCore import Qt
56
import os
6-
import errno
77
import threading
88
import pipereader
99

10+
1011
class SDDialog(QDialog):
11-
def __init__(self, parent = None):
12+
def __init__(self, parent=None):
1213
fn = r'/usr/local/share/sd/logo-small.png'
1314
image = QImage(fn)
1415
self.logo = QLabel()
@@ -36,32 +37,47 @@ class SDDialog(QDialog):
3637

3738
messages = {
3839
# bootstrapping
39-
'EXISTING_SIGFILE': ("error", "Internal error: signal file given by caller already exists"),
40-
'POLLING_ERROR': {"error": "An internal error occurred while listening for events from VMs."},
40+
'EXISTING_SIGFILE': (
41+
"error", "Internal error: signal file given by caller already exists"),
42+
'POLLING_ERROR': {
43+
"error": ("An internal error occurred while listening"
44+
" for events from VMs.")},
4145

4246
# sd-journalist
43-
'DOWNLOAD_FILE_MISSING': ("error", "The file downloaded from SecureDrop wasn't found."),
47+
'DOWNLOAD_FILE_MISSING': (
48+
"error", "The file downloaded from SecureDrop wasn't found."),
4449
'DOWNLOAD_BUNDLE_CREATED': ("success", "Initial download bundle created."),
4550

4651
# decrypt
4752
'DECRYPTION_PROCESS_START': ("success", "Decryption process started."),
48-
'DECRYPTION_BUNDLE_OPEN_FAILURE': ("error", "Decryption bundle could not be opened."),
49-
'SUBMISSION_BUNDLE_UNBUNDLED': ("success", "Submission bundle looks valid."),
50-
'SUBMISSION_FILES_EXTRACTED': ("success", "Submission bundle files extracted."),
51-
'SUBMISSION_FILE_DECRYPTION_FAILED': ('error', "Submission file decryption failed."),
52-
'SUBMISSION_FILE_DECRYPTION_SUCCEEDED': ("success", "Submission file decrypted."),
53-
'SUBMISSION_DECRYPTED': ("success", "All submission files decrypted"),
53+
'DECRYPTION_BUNDLE_OPEN_FAILURE':
54+
("error", "Decryption bundle could not be opened."),
55+
'SUBMISSION_BUNDLE_UNBUNDLED':
56+
("success", "Submission bundle looks valid."),
57+
'SUBMISSION_FILES_EXTRACTED':
58+
("success", "Submission bundle files extracted."),
59+
'SUBMISSION_FILE_DECRYPTION_FAILED':
60+
('error', "Submission file decryption failed."),
61+
'SUBMISSION_FILE_DECRYPTION_SUCCEEDED':
62+
("success", "Submission file decrypted."),
63+
'SUBMISSION_DECRYPTED':
64+
("success", "All submission files decrypted"),
5465

5566
# SVS
56-
'DECRYPTED_BUNDLE_ON_SVS': ("success", "Decrypted file bundle arrived on SVS."),
57-
'DECRYPTED_FILES_AVAILABLE': ("success", "Submitted files available for use on SVS."),
58-
'DECRYPTED_BUNDLE UNBUNDLE_ERROR': ("error", "Bundle of decrypted files could not be unbundle on SVS." )
67+
'DECRYPTED_BUNDLE_ON_SVS':
68+
("success", "Decrypted file bundle arrived on SVS."),
69+
'DECRYPTED_FILES_AVAILABLE':
70+
("success", "Submitted files available for use on SVS."),
71+
'DECRYPTED_BUNDLE UNBUNDLE_ERROR':
72+
("error", "Bundle of decrypted files could not be unbundle on SVS.")
5973
}
6074

75+
6176
def finish():
6277
d.buttons.hide()
6378
d.layout.removeWidget(d.buttons)
6479

80+
6581
def display(keyword):
6682

6783
if keyword in messages:
@@ -70,6 +86,7 @@ def display(keyword):
7086
# XXX dev only, remove before deploying!
7187
d.display.setText("bad keyword: {}".format(keyword))
7288

89+
7390
def poller_cb(poller, msg, err):
7491
# we're called with a keyword in `msg`. We look up that keyword
7592
# for the user-facing message to display.
@@ -90,9 +107,9 @@ def create_sigfile(sigfile):
90107
display('EXISTING_SIGFILE')
91108
else:
92109
try:
93-
with open(sigfile,'a'):
110+
with open(sigfile, 'a'):
94111
pass
95-
except Exception as e:
112+
except Exception:
96113
display('BAD_SIGFILE')
97114

98115

@@ -111,7 +128,7 @@ if __name__ == '__main__':
111128
d.show()
112129

113130
if sigfile != "":
114-
# tyvm https://stackoverflow.com/questions/6215690/how-to-execute-a-method-automatically-after-entering-qt-event-loop
131+
# tyvm https://stackoverflow.com/questions/6215690/how-to-execute-a-method-automatically-after-entering-qt-event-loop # noqa: E501
115132
def on_start():
116133
create_sigfile(sigfile)
117134

sd-journalist/sd-process-download

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import subprocess
1010

1111
fn = sys.argv[1]
1212

13+
1314
# remember your Stevens and do a double fork
1415
def spawn_monitor_process(sigfile):
1516

1617
try:
1718
pid = os.fork()
1819
if pid > 0:
19-
# congrats, new parent!
20+
# congrats, new parent!
2021
return
2122
except OSError as e:
2223
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
@@ -50,6 +51,7 @@ def wait_for_sigfile(sigfile):
5051

5152
os.remove(sigfile)
5253

54+
5355
# spawn the GUI monitoring process, then wait for it to be available...
5456
sigfile = tempfile.mktemp()
5557
spawn_monitor_process(sigfile)
@@ -75,7 +77,8 @@ fh.close()
7577

7678
# this is sort of a one-off... how shall we send messages to the GUI
7779
# from the same machine? This can only happen on sd-journalist.
78-
c = subprocess.Popen(["/usr/local/bin/sd-process-feedback"], stdin=subprocess.PIPE)
80+
c = subprocess.Popen(["/usr/local/bin/sd-process-feedback"],
81+
stdin=subprocess.PIPE)
7982
c.communicate(input="DOWNLOAD_BUNDLE_CREATED\n")
8083

8184
# ship this to the next phase

sd-svs/accept-sd-xfer-extracted

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ import os
66

77
fn = sys.argv[1]
88

9-
## pull this in to a library... XXX
9+
10+
# TODO: pull this in to a library... XXX
1011
def send_progress(msg):
11-
p = subprocess.Popen(["qrexec-client-vm","sd-journalist",
12-
"sd-process.Feedback"],
13-
close_fds=True,
14-
stdin=subprocess.PIPE)
15-
p.communicate(msg)
12+
p = subprocess.Popen(["qrexec-client-vm", "sd-journalist",
13+
"sd-process.Feedback"],
14+
close_fds=True,
15+
stdin=subprocess.PIPE)
16+
p.communicate(msg)
17+
1618

1719
send_progress("DECRYPTED_BUNDLE_ON_SVS")
1820

1921
try:
20-
res = subprocess.call(["tar", "-xf", fn, "--strip-components=1", "-C", "/home/user/Sources"])
22+
cmd = ["tar", "-xf", fn,
23+
"--strip-components=1", "-C", "/home/user/Sources"]
24+
res = subprocess.call(cmd)
2125
if res != 0:
2226
send_progress("DECRYPTED_BUNDLE_UNBUNDLE_ERROR")
2327
sys.exit(1)

0 commit comments

Comments
 (0)