Commit f1a59084 authored by segfault's avatar segfault
Browse files

Improve style of tails-upgrade-frontend-wrapper.py

 - Use /usr/bin/env in shebang
 - Make docstrings PEP 257 compliant
 - Other minor code style improvements
parent 6b189500
#! /usr/bin/python3
'''
Tails upgrade frontend wrapper.
#! /usr/bin/env python3
"""
Tails upgrade frontend wrapper.
Test with "python3 tails-upgrade-frontend-wrapper.py doctest".
The tests will start the upgrade process which could pop up a dialog box
so you probably want to use a tester that handles user interaction or
run the tests from the command line and answer prompts as needed.
Test with "python3 tails-upgrade-frontend-wrapper.py doctest".
The tests will start the upgrade process which could pop up a dialog box
so you probably want to use a tester that handles user interaction or
run the tests from the command line and answer prompts as needed.
goodcrypto.com converted from bash to python and added basic tests.
>>> # run script
>>> this_command = sh.Command(sys.argv[0])
>>> this_command()
<BLANKLINE>
"""
goodcrypto.com converted from bash to python and added basic tests.
'''
import os
import sys
import time
from gettext import gettext
from contextlib import contextmanager
import sh
......@@ -26,19 +33,19 @@ INOTIFY_TIMEOUT = 60
MIN_REAL_MEMFREE = (300 * 1024)
RUN_AS_USER = 'tails-upgrade-frontend'
def main(*args):
"""
Tails upgrade frontend wrapper.
ERROR_MESSAGE = gettext('''\"<b>Not enough memory available to check for upgrades.</b>
>>> try:
... main()
... fail()
... except SystemExit:
... pass
"""
Make sure this system satisfies the requirements for running Tails.
See file:///usr/share/doc/tails/website/doc/about/requirements.en.html
time.sleep(30)
Try to restart Tails to check for upgrades again.
Or do a manual upgrade.
See https://tails.boum.org/doc/first_steps/upgrade#manual\"''')
def main(*args):
time.sleep(30)
check_free_memory(MIN_REAL_MEMFREE)
# Go to a place where everyone, especially Archive::Tar::Wrapper called by
......@@ -46,30 +53,28 @@ def main(*args):
# its job.
os.chdir('/')
sh.xhost('+SI:localuser:{}'.format(RUN_AS_USER))
if args:
sh.sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend', *args)
else:
sh.sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend')
try:
if len(args) > 0:
result = sh.sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend', sh.glob(args))
else:
result = sh.sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend')
except sh.ErrorReturnCode:
pass
@contextmanager
def allow_x_connection():
sh.xhost('+SI:localuser:{}'.format(RUN_AS_USER))
yield
sh.xhost('-SI:localuser:{}'.format(RUN_AS_USER))
sys.exit(result.exit_code)
def error(msg):
"""
Show error and exit.
"""Show error and exit.
>>> try:
... error('testing')
... fail()
... except SystemExit:
... pass
>>> try:
... error('testing')
... fail()
... except SystemExit:
... pass
"""
cli_text = '{}: {} {}'.format(CMD, gettext('error:'), msg)
dialog_text = '''<b><big>{}</big></b>\n\n{}'''.format(gettext('Error'), msg)
print(cli_text, file=sys.stderr)
......@@ -77,58 +82,42 @@ def error(msg):
sh.zenity('--error', '--title', "", '--text', '{}'.format(dialog_text), _ok_code=[0,1,5])
sys.exit(1)
def check_free_memory(min_real_memfree):
"""
Check for enough free memory.
"""Check for enough free memory.
>>> check_free_memory(MIN_REAL_MEMFREE)
>>> check_free_memory(MIN_REAL_MEMFREE)
"""
memfree = buffers = cached = None
with open('/proc/meminfo') as f:
for line in f:
if line.startswith('MemFree:'):
fields = line.split()
memfree = int(fields[1])
elif line.startswith('Buffers:'):
fields = line.split()
buffers = int(fields[1])
elif line.startswith('Cached:'):
fields = line.split()
cached = int(fields[1])
if not all((memfree, buffers, cached)):
raise RuntimeError("Parsing /proc/meminfo failed")
for line in open('/proc/meminfo'):
if line.startswith('MemFree:'):
fields = line.split()
memfree = int(fields[1])
elif line.startswith('Buffers:'):
fields = line.split()
buffers = int(fields[1])
elif line.startswith('Cached:'):
fields = line.split()
cached = int(fields[1])
df_text = sh.df('--type=tmpfs', '--local', '--output=used', '--total').stdout.decode()
tmpfs = int(df_text.strip().split('\n')[-1])
real_memfree = (memfree + buffers + cached) - tmpfs
errormsg = gettext('''\"<b>Not enough memory available to check for upgrades.</b>
Make sure this system satisfies the requirements for running Tails.
See file:///usr/share/doc/tails/website/doc/about/requirements.en.html
Try to restart Tails to check for upgrades again.
Or do a manual upgrade.
See https://tails.boum.org/doc/first_steps/upgrade#manual\"''')
if real_memfree < min_real_memfree:
print('Only {} MemFree + '.format(real_memfree), end=None)
print('Buffers + Cached - usage of tmpfs, ', end=None)
print('Only {} MemFree + '.format(real_memfree), end='')
print('Buffers + Cached - usage of tmpfs, ', end='')
print('while {} is needed.'.format(MIN_REAL_MEMFREE), file=sys.stderr)
error(errormsg)
'''
>>> # run script
>>> this_command = sh.Command(sys.argv[0])
>>> this_command()
<BLANKLINE>
'''
if __name__ == '__main__':
if sys.argv and len(sys.argv) > 1:
if sys.argv[1] == 'doctest':
from doctest import testmod
testmod()
else:
main(sys.argv[1:])
else:
main()
error(ERROR_MESSAGE)
sys.exit(0)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'doctest':
import doctest
doctest.testmod()
else:
main(*sys.argv[1:])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment