Commit 07f3a65c authored by GoodCrypto's avatar GoodCrypto Committed by segfault
Browse files

Improved conversion

parent 3b5dd412
#! /usr/bin/python
#! /usr/bin/python3
'''
Tails upgrade frontend wrapper.
Conversion from bash to python by goodcrypto.com
'''
from __future__ import print_function
Test with "python3 tails-upgrade-frontend-wrapper.py doctest".
goodcrypto.com converted from bash to python and added basic tests.
'''
import os
import sys
import time
from gettext import gettext
from sh import awk, basename, df, sudo, xhost, zenity, ErrorReturnCode
import sh
os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
CMD = basename(sys.argv[0]).stdout.strip()
CMD = os.path.basename(sys.argv[0])
os.environ['TEXTDOMAIN'] = 'tails'
......@@ -24,7 +25,36 @@ INOTIFY_TIMEOUT = 60
MIN_REAL_MEMFREE = (300 * 1024)
RUN_AS_USER = 'tails-upgrade-frontend'
### Functions
def main(*args):
"""
Tails upgrade frontend wrapper.
>>> main([])
"""
time.sleep(30)
check_free_memory(MIN_REAL_MEMFREE)
# Go to a place where everyone, especially Archive::Tar::Wrapper called by
# tails-install-iuk, can chdir back after it has chdir'd elsewhere to do
# its job.
os.chdir('/')
sh.xhost('+SI:localuser:{}'.format(RUN_AS_USER))
# try forever
done = False
while not done:
try:
result = sh.sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend', *args)
except sh.ErrorReturnCode:
pass
else:
done = True
sh.xhost('-SI:localuser:{}'.format(RUN_AS_USER))
sys.exit(result.exit_code)
def error(msg):
"""
......@@ -36,7 +66,8 @@ def error(msg):
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)
zenity('--error', '--title', '', '--text', dialog_text, _ok_code=[0, 1])
sh.zenity('--error', '--title', "", '--text', '{}'.format(dialog_text), _ok_code=[0,1,5])
sys.exit(1)
def check_free_memory(min_real_memfree):
......@@ -46,10 +77,13 @@ def check_free_memory(min_real_memfree):
>>> check_free_memory(MIN_REAL_MEMFREE)
"""
memfree = awk('/^MemFree:/{print $2}', '/proc/meminfo')
buffers = awk('/^Buffers:/{print $2}', '/proc/meminfo')
cached = awk('/^Cached:/{print $2}', '/proc/meminfo')
df_text = df('--type=tmpfs', '--local', '--output=used', '--total').stdout
memfree = sh.awk('/^MemFree:/{print $2}', '/proc/meminfo')
buffers = sh.awk('/^Buffers:/{print $2}', '/proc/meminfo')
cached = sh.awk('/^Cached:/{print $2}', '/proc/meminfo')
df_text = sh.df('--type=tmpfs', '--local', '--output=used', '--total').stdout.decode()
df_text = df_text.strip() # DEBUG
df_text = df_text.split('\n') # DEBUG
df_text = df_text[-1] # DEBUG
tmpfs = int(df_text.strip().split('\n')[-1])
real_memfree = (memfree + buffers + cached) - tmpfs
......@@ -69,35 +103,12 @@ See https://tails.boum.org/doc/first_steps/upgrade#manual\"''')
print('while {} is needed.'.format(MIN_REAL_MEMFREE), file=sys.stderr)
error(errormsg)
def main(*args):
"""
Tails upgrade frontend wrapper.
"""
time.sleep(30)
check_free_memory(MIN_REAL_MEMFREE)
# Go to a place where everyone, especially Archive::Tar::Wrapper called by
# tails-install-iuk, can chdir back after it has chdir'd elsewhere to do
# its job.
os.chdir('/')
xhost('+SI:localuser:{}'.format(RUN_AS_USER))
# try forever
done = False
while not done:
try:
result = sudo('-u', RUN_AS_USER, '/usr/bin/tails-upgrade-frontend', *args)
except ErrorReturnCode:
pass
else:
done = True
xhost('-SI:localuser:{}'.format(RUN_AS_USER))
sys.exit(result.exit_code)
'''
>>> # 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':
......
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