Remove yast from utils

From: Goldwyn Rodrigues <rgoldwyn@suse.com>

This is the yast cleanup from the utils code. All yast communication
should be done with JSON interface now.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>



Acked-by: Christian Boltz <apparmor@cboltz.de>
This commit is contained in:
Goldwyn Rodrigues 2017-06-11 13:03:44 +02:00 committed by Christian Boltz
parent 49420c51f3
commit 0ce0fa45aa
3 changed files with 64 additions and 357 deletions

View file

@ -14,7 +14,6 @@
# ----------------------------------------------------------------------
# No old version logs, only 2.6 + supported
from __future__ import division, with_statement
import inspect
import os
import re
import shutil
@ -64,8 +63,6 @@ from apparmor.rule import quote_if_needed
ruletypes = ['capability', 'change_profile', 'dbus', 'file', 'network', 'ptrace', 'rlimit', 'signal']
from apparmor.yasti import SendDataToYast, GetDataFromYast, shutdown_yast
# setup module translations
from apparmor.translations import init_translation
_ = init_translation()
@ -146,15 +143,9 @@ def fatal_error(message):
# Add the traceback to message
message = tb_stack + '\n\n' + message
debug_logger.error(message)
caller = inspect.stack()[1][3]
# If caller is SendDataToYast or GetDatFromYast simply exit
if caller == 'SendDataToYast' or caller == 'GetDatFromYast':
sys.exit(1)
# Else tell user what happened
aaui.UI_Important(message)
shutdown_yast()
sys.exit(1)
def check_for_apparmor(filesystem='/proc/filesystems', mounts='/proc/mounts'):
@ -539,7 +530,6 @@ def confirm_and_abort():
ans = aaui.UI_YesNo(_('Are you sure you want to abandon this set of profile changes and exit?'), 'n')
if ans == 'y':
aaui.UI_Info(_('Abandoning all changes.'))
shutdown_yast()
for prof in created:
delete_profile(prof)
sys.exit(0)
@ -601,20 +591,12 @@ def get_profile(prof_name):
p = profile_hash[options[arg]]
q.selected = options.index(options[arg])
if ans == 'CMD_VIEW_PROFILE':
if aaui.UI_mode == 'yast':
SendDataToYast({'type': 'dialogue-view-profile',
'user': options[arg],
'profile': p['profile'],
'profile_type': p['profile_type']
})
ypath, yarg = GetDataFromYast()
else:
pager = get_pager()
proc = subprocess.Popen(pager, stdin=subprocess.PIPE)
pager = get_pager()
proc = subprocess.Popen(pager, stdin=subprocess.PIPE)
# proc.communicate('Profile submitted by %s:\n\n%s\n\n' %
# (options[arg], p['profile']))
proc.communicate(p['profile'].encode())
proc.kill()
proc.communicate(p['profile'].encode())
proc.kill()
elif ans == 'CMD_USE_PROFILE':
if p['profile_type'] == 'INACTIVE_LOCAL':
profile_data = p['profile_data']
@ -864,76 +846,16 @@ def fetch_profiles_by_user(url, distro, user):
def submit_created_profiles(new_profiles):
#url = cfg['repository']['url']
if new_profiles:
if aaui.UI_mode == 'yast':
title = 'New Profiles'
message = 'Please select the newly created profiles that you would like to store in the repository'
yast_select_and_upload_profiles(title, message, new_profiles)
else:
title = 'Submit newly created profiles to the repository'
message = 'Would you like to upload newly created profiles?'
console_select_and_upload_profiles(title, message, new_profiles)
title = 'Submit newly created profiles to the repository'
message = 'Would you like to upload newly created profiles?'
console_select_and_upload_profiles(title, message, new_profiles)
def submit_changed_profiles(changed_profiles):
#url = cfg['repository']['url']
if changed_profiles:
if aaui.UI_mode == 'yast':
title = 'Changed Profiles'
message = 'Please select which of the changed profiles would you like to upload to the repository'
yast_select_and_upload_profiles(title, message, changed_profiles)
else:
title = 'Submit changed profiles to the repository'
message = 'The following profiles from the repository were changed.\nWould you like to upload your changes?'
console_select_and_upload_profiles(title, message, changed_profiles)
def yast_select_and_upload_profiles(title, message, profiles_up):
url = cfg['repository']['url']
profile_changes = hasher()
profs = profiles_up[:]
for p in profs:
profile_changes[p[0]] = get_profile_diff(p[2], p[1])
SendDataToYast({'type': 'dialog-select-profiles',
'title': title,
'explanation': message,
'default_select': 'false',
'disable_ask_upload': 'true',
'profiles': profile_changes
})
ypath, yarg = GetDataFromYast()
selected_profiles = []
changelog = None
changelogs = None
single_changelog = False
if yarg['STATUS'] == 'cancel':
return
else:
selected_profiles = yarg['PROFILES']
changelogs = yarg['CHANGELOG']
if changelogs.get('SINGLE_CHANGELOG', False):
changelog = changelogs['SINGLE_CHANGELOG']
single_changelog = True
user, passw = get_repo_user_pass()
for p in selected_profiles:
profile_string = serialize_profile(aa[p], p)
if not single_changelog:
changelog = changelogs[p]
status_ok, ret = upload_profile(url, user, passw, cfg['repository']['distro'],
p, profile_string, changelog)
if status_ok:
newprofile = ret
newid = newprofile['id']
set_repo_info(aa[p][p], url, user, newid)
write_profile_ui_feedback(p)
else:
if not ret:
ret = 'UNKNOWN ERROR'
aaui.UI_Important(_('WARNING: An error occurred while uploading the profile %(profile)s\n%(ret)s') % { 'profile': p, 'ret': ret })
aaui.UI_Info(_('Uploaded changes to repository.'))
if yarg.get('NEVER_ASK_AGAIN'):
unselected_profiles = []
for p in profs:
if p[0] not in selected_profiles:
unselected_profiles.append(p[0])
set_profiles_local_only(unselected_profiles)
title = 'Submit changed profiles to the repository'
message = 'The following profiles from the repository were changed.\nWould you like to upload your changes?'
console_select_and_upload_profiles(title, message, changed_profiles)
def upload_profile(url, user, passw, distro, p, profile_string, changelog):
# To-Do
@ -1925,10 +1847,6 @@ def do_logprof_pass(logmark='', passno=0, log_pid=log_pid):
ask_the_questions(log_dict)
if aaui.UI_mode == 'yast':
# To-Do
pass
finishing = False
# Check for finished
save_profiles()
@ -1958,79 +1876,51 @@ def save_profiles():
changed_list = sorted(changed.keys())
if changed_list:
if aaui.UI_mode == 'yast':
# To-Do
# selected_profiles = [] # XXX selected_profiles_ref?
profile_changes = dict()
for prof in changed_list:
oldprofile = serialize_profile(original_aa[prof], prof)
newprofile = serialize_profile(aa[prof], prof)
profile_changes[prof] = get_profile_diff(oldprofile, newprofile)
explanation = _('Select which profile changes you would like to save to the\nlocal profile set.')
title = _('Local profile changes')
SendDataToYast({'type': 'dialog-select-profiles',
'title': title,
'explanation': explanation,
'dialog_select': 'true',
'get_changelog': 'false',
'profiles': profile_changes
})
ypath, yarg = GetDataFromYast()
if yarg['STATUS'] == 'cancel':
return None
else:
selected_profiles_ref = yarg['PROFILES']
for profile_name in selected_profiles_ref:
write_profile_ui_feedback(profile_name)
reload_base(profile_name)
else:
q = aaui.PromptQuestion()
q.title = 'Changed Local Profiles'
q.explanation = _('The following local profiles were changed. Would you like to save them?')
q.functions = ['CMD_SAVE_CHANGES', 'CMD_SAVE_SELECTED', 'CMD_VIEW_CHANGES', 'CMD_VIEW_CHANGES_CLEAN', 'CMD_ABORT']
q.default = 'CMD_VIEW_CHANGES'
q.options = changed
q.selected = 0
ans = ''
arg = None
while ans != 'CMD_SAVE_CHANGES':
if not changed:
return
ans, arg = q.promptUser()
if ans == 'CMD_SAVE_SELECTED':
profile_name = list(changed.keys())[arg]
write_profile_ui_feedback(profile_name)
reload_base(profile_name)
elif ans == 'CMD_VIEW_CHANGES':
which = list(changed.keys())[arg]
oldprofile = None
if aa[which][which].get('filename', False):
oldprofile = aa[which][which]['filename']
else:
oldprofile = get_profile_filename(which)
try:
newprofile = serialize_profile_from_old_profile(aa[which], which, '')
except AttributeError:
# see https://bugs.launchpad.net/ubuntu/+source/apparmor/+bug/1528139
newprofile = "###\n###\n### Internal error while generating diff, please use '%s' instead\n###\n###\n" % _('View Changes b/w (C)lean profiles')
display_changes_with_comments(oldprofile, newprofile)
elif ans == 'CMD_VIEW_CHANGES_CLEAN':
which = list(changed.keys())[arg]
oldprofile = serialize_profile(original_aa[which], which, '')
newprofile = serialize_profile(aa[which], which, '')
display_changes(oldprofile, newprofile)
for profile_name in sorted(changed.keys()):
q = aaui.PromptQuestion()
q.title = 'Changed Local Profiles'
q.explanation = _('The following local profiles were changed. Would you like to save them?')
q.functions = ['CMD_SAVE_CHANGES', 'CMD_SAVE_SELECTED', 'CMD_VIEW_CHANGES', 'CMD_VIEW_CHANGES_CLEAN', 'CMD_ABORT']
q.default = 'CMD_VIEW_CHANGES'
q.options = changed
q.selected = 0
ans = ''
arg = None
while ans != 'CMD_SAVE_CHANGES':
if not changed:
return
ans, arg = q.promptUser()
if ans == 'CMD_SAVE_SELECTED':
profile_name = list(changed.keys())[arg]
write_profile_ui_feedback(profile_name)
reload_base(profile_name)
elif ans == 'CMD_VIEW_CHANGES':
which = list(changed.keys())[arg]
oldprofile = None
if aa[which][which].get('filename', False):
oldprofile = aa[which][which]['filename']
else:
oldprofile = get_profile_filename(which)
try:
newprofile = serialize_profile_from_old_profile(aa[which], which, '')
except AttributeError:
# see https://bugs.launchpad.net/ubuntu/+source/apparmor/+bug/1528139
newprofile = "###\n###\n### Internal error while generating diff, please use '%s' instead\n###\n###\n" % _('View Changes b/w (C)lean profiles')
display_changes_with_comments(oldprofile, newprofile)
elif ans == 'CMD_VIEW_CHANGES_CLEAN':
which = list(changed.keys())[arg]
oldprofile = serialize_profile(original_aa[which], which, '')
newprofile = serialize_profile(aa[which], which, '')
display_changes(oldprofile, newprofile)
for profile_name in sorted(changed.keys()):
write_profile_ui_feedback(profile_name)
reload_base(profile_name)
def get_pager():
return 'less'
@ -2065,33 +1955,26 @@ def get_profile_diff(oldprofile, newprofile):
return ''.join(diff)
def display_changes(oldprofile, newprofile):
if aaui.UI_mode == 'yast':
aaui.UI_LongMessage(_('Profile Changes'), get_profile_diff(oldprofile, newprofile))
else:
difftemp = generate_diff(oldprofile, newprofile)
subprocess.call('less %s' % difftemp.name, shell=True)
difftemp.delete = True
difftemp.close()
difftemp = generate_diff(oldprofile, newprofile)
subprocess.call('less %s' % difftemp.name, shell=True)
difftemp.delete = True
difftemp.close()
def display_changes_with_comments(oldprofile, newprofile):
"""Compare the new profile with the existing profile inclusive of all the comments"""
if not os.path.exists(oldprofile):
raise AppArmorException(_("Can't find existing profile %s to compare changes.") % oldprofile)
if aaui.UI_mode == 'yast':
#To-Do
pass
else:
newtemp = tempfile.NamedTemporaryFile('w')
newtemp.write(newprofile)
newtemp.flush()
newtemp = tempfile.NamedTemporaryFile('w')
newtemp.write(newprofile)
newtemp.flush()
difftemp = tempfile.NamedTemporaryFile('w')
difftemp = tempfile.NamedTemporaryFile('w')
subprocess.call('diff -u -p %s %s > %s' % (oldprofile, newtemp.name, difftemp.name), shell=True)
subprocess.call('diff -u -p %s %s > %s' % (oldprofile, newtemp.name, difftemp.name), shell=True)
newtemp.close()
subprocess.call('less %s' % difftemp.name, shell=True)
difftemp.close()
newtemp.close()
subprocess.call('less %s' % difftemp.name, shell=True)
difftemp.close()
def set_process(pid, profile):
# If process not running don't do anything

View file

@ -14,7 +14,6 @@
import sys
import re
import readline
from apparmor.yasti import yastLog, SendDataToYast, GetDataFromYast
from apparmor.common import readkey, AppArmorException, DebugLogger
@ -47,18 +46,11 @@ def UI_Info(text):
debug_logger.info(text)
if UI_mode == 'text':
sys.stdout.write(text + '\n')
else:
yastLog(text)
def UI_Important(text):
debug_logger.debug(text)
if UI_mode == 'text':
sys.stdout.write('\n' + text + '\n')
else:
SendDataToYast({'type': 'dialog-error',
'message': text
})
path, yarg = GetDataFromYast()
def get_translated_hotkey(translated, cmsg=''):
msg = 'PromptUser: ' + _('Invalid hotkey for')
@ -105,15 +97,6 @@ def UI_YesNo(text, default):
continue # If user presses any other button ask again
else:
ans = default
else:
SendDataToYast({'type': 'dialog-yesno',
'question': text
})
ypath, yarg = GetDataFromYast()
ans = yarg['answer']
if not ans:
ans = default
return ans
def UI_YesNoCancel(text, default):
@ -160,14 +143,6 @@ def UI_YesNoCancel(text, default):
default = 'c'
else:
ans = default
else:
SendDataToYast({'type': 'dialog-yesnocancel',
'question': text
})
ypath, yarg = GetDataFromYast()
ans = yarg['answer']
if not ans:
ans = default
return ans
def UI_GetString(text, default):
@ -181,13 +156,6 @@ def UI_GetString(text, default):
string = ''
finally:
readline.set_startup_hook()
else:
SendDataToYast({'type': 'dialog-getstring',
'label': text,
'default': default
})
ypath, yarg = GetDataFromYast()
string = yarg['string']
return string.strip()
def UI_GetFile(file):
@ -196,29 +164,15 @@ def UI_GetFile(file):
if UI_mode == 'text':
sys.stdout.write(file['description'] + '\n')
filename = sys.stdin.read()
else:
file['type'] = 'dialog-getfile'
SendDataToYast(file)
ypath, yarg = GetDataFromYast()
if yarg['answer'] == 'okay':
filename = yarg['filename']
return filename
def UI_BusyStart(message):
debug_logger.debug('UI_BusyStart: %s' % UI_mode)
if UI_mode == 'text':
UI_Info(message)
else:
SendDataToYast({'type': 'dialog-busy-start',
'message': message
})
ypath, yarg = GetDataFromYast()
def UI_BusyStop():
debug_logger.debug('UI_BusyStop: %s' % UI_mode)
if UI_mode != 'text':
SendDataToYast({'type': 'dialog-busy-stop'})
ypath, yarg = GetDataFromYast()
CMDS = {'CMD_ALLOW': _('(A)llow'),
'CMD_OTHER': _('(M)ore'),
@ -302,13 +256,6 @@ class PromptQuestion(object):
arg = None
if UI_mode == 'text':
cmd, arg = self.Text_PromptUser()
else:
self.type = 'wizard'
SendDataToYast(self)
ypath, yarg = GetDataFromYast()
if not cmd:
cmd = 'CMD_ABORT'
arg = yarg['selected']
if cmd == 'CMD_ABORT':
confirm_and_abort()
cmd = 'XXXINVALIDXXX'
@ -447,25 +394,8 @@ def confirm_and_abort():
ans = UI_YesNo(_('Are you sure you want to abandon this set of profile changes and exit?'), 'n')
if ans == 'y':
UI_Info(_('Abandoning all changes.'))
#shutdown_yast()
#for prof in created:
# delete_profile(prof)
sys.exit(0)
def UI_ShortMessage(title, message):
SendDataToYast({'type': 'short-dialog-message',
'headline': title,
'message': message
})
ypath, yarg = GetDataFromYast()
def UI_LongMessage(title, message):
SendDataToYast({'type': 'long-dialog-message',
'headline': title,
'message': message
})
ypath, yarg = GetDataFromYast()
def is_number(number):
try:
return int(number)

View file

@ -1,106 +0,0 @@
# ----------------------------------------------------------------------
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ----------------------------------------------------------------------
import re
import sys
try:
import ycp
except ImportError:
# ycp isn't found everywhere.
ycp = None
from apparmor.common import error, DebugLogger
# Set up UI logger for separate messages from YaST module
debug_logger = DebugLogger('YaST')
def setup_yast():
# To-Do
pass
def shutdown_yast():
# To-Do
pass
def yastLog(text):
ycp.y2milestone(text)
def SendDataToYast(data):
debug_logger.info('SendDataToYast: Waiting for YCP command')
for line in sys.stdin:
ycommand, ypath, yargument = ParseCommand(line)
if ycommand and ycommand == 'Read':
debug_logger.info('SendDataToYast: Sending--%s' % data)
ycp.Return(data)
return True
else:
debug_logger.info('SendDataToYast: Expected \'Read\' but got-- %s' % line)
error('SendDataToYast: didn\'t receive YCP command before connection died')
def GetDataFromYast():
debug_logger.inf('GetDataFromYast: Waiting for YCP command')
for line in sys.stdin:
debug_logger.info('GetDataFromYast: YCP: %s' % line)
ycommand, ypath, yarg = ParseCommand(line)
debug_logger.info('GetDataFromYast: Recieved--\n%s' % yarg)
if ycommand and ycommand == 'Write':
ycp.Return('true')
return ypath, yarg
else:
debug_logger.info('GetDataFromYast: Expected Write but got-- %s' % line)
error('GetDataFromYast: didn\'t receive YCP command before connection died')
def ParseCommand(commands):
term = ParseTerm(commands)
if term:
command = term[0]
term = term[1:]
else:
command = ''
path = ''
pathref = None
if term:
pathref = term[0]
term = term[1:]
if pathref:
if pathref.strip():
path = pathref.strip()
elif command != 'result':
ycp.y2error('The first arguement is not a path. (%s)' % pathref)
argument = None
if term:
argument = term[0]
if len(term) > 1:
ycp.y2warning('Superfluous command arguments ignored')
return (command, path, argument)
def ParseTerm(inp):
regex_term = re.compile('^\s*`?(\w*)\s*')
term = regex_term.search(inp)
ret = []
symbol = None
if term:
symbol = term.groups()[0]
else:
ycp.y2error('No term symbol')
ret.append(symbol)
inp = regex_term.sub('', inp)
if not inp.startswith('('):
ycp.y2error('No term parantheses')
argref, err, rest = ycp.ParseYcpTermBody(inp)
if err:
ycp.y2error('%s (%s)' % (err, rest))
else:
ret += argref
return ret