2013-06-21 01:35:26 +05:30
|
|
|
import configparser
|
2013-06-01 15:37:31 +05:30
|
|
|
import os
|
2013-06-21 01:35:26 +05:30
|
|
|
import shutil
|
2013-06-01 15:37:31 +05:30
|
|
|
import stat
|
2013-06-21 01:35:26 +05:30
|
|
|
import tempfile
|
|
|
|
|
2013-06-01 15:37:31 +05:30
|
|
|
|
|
|
|
confdir = '/etc/apparmor'
|
|
|
|
cfg = None
|
|
|
|
repo_cfg = None
|
|
|
|
|
|
|
|
def read_config(filename):
|
2013-06-18 03:49:05 +05:30
|
|
|
"""Reads the file and returns a configparser config[section][attribute]=property"""
|
|
|
|
config = configparser.ConfigParser()
|
2013-06-01 15:37:31 +05:30
|
|
|
filepath = confdir + '/' + filename
|
2013-06-18 03:49:05 +05:30
|
|
|
config.read(filepath)
|
2013-06-01 15:37:31 +05:30
|
|
|
# LP: Bug #692406
|
|
|
|
# Explicitly disabled repository
|
|
|
|
if filename == "repository.conf":
|
2013-06-01 16:11:55 +05:30
|
|
|
config['repository'] = {'enabled': 'no'}
|
2013-06-01 15:37:31 +05:30
|
|
|
return config
|
|
|
|
|
|
|
|
def write_config(filename, config):
|
2013-06-18 03:49:05 +05:30
|
|
|
"""Writes the given configparser to the specified file"""
|
2013-06-01 15:37:31 +05:30
|
|
|
filepath = confdir + '/' + filename
|
2013-06-21 01:35:26 +05:30
|
|
|
permission_600 = stat.S_IRUSR | stat.S_IWUSR # Owner read and write
|
2013-06-01 15:37:31 +05:30
|
|
|
try:
|
2013-06-21 01:35:26 +05:30
|
|
|
# Open a temporary file to write the config file
|
|
|
|
config_file = tempfile.NamedTemporaryFile('w', prefix='aa_temp', delete=False)
|
|
|
|
# Set file permissions as 0600
|
|
|
|
os.chmod(config_file.name, permission_600)
|
|
|
|
config.write(config_file)
|
|
|
|
config_file.close()
|
2013-06-01 15:56:56 +05:30
|
|
|
except IOError:
|
2013-06-01 15:37:31 +05:30
|
|
|
raise IOError("Unable to write to %s"%filename)
|
|
|
|
else:
|
2013-06-21 01:35:26 +05:30
|
|
|
# Move the temporary file to the target config file
|
|
|
|
shutil.move(config_file.name, filepath)
|
|
|
|
|
2013-06-01 15:37:31 +05:30
|
|
|
|
|
|
|
def find_first_file(file_list):
|
|
|
|
"""Returns name of first matching file None otherwise"""
|
|
|
|
# I don't understand why it searches the CWD, maybe I'll find out about it in some module
|
|
|
|
filename = None
|
|
|
|
if len(file_list):
|
|
|
|
for file in file_list.split():
|
|
|
|
if os.path.isfile(file):
|
|
|
|
filename = file
|
|
|
|
break
|
|
|
|
return filename
|
|
|
|
|
|
|
|
def find_first_dir(dir_list):
|
|
|
|
"""Returns name of first matching directory None otherwise"""
|
|
|
|
dirname = None
|
|
|
|
if (len(dir_list)):
|
|
|
|
for direc in dir_list.split():
|
|
|
|
if os.path.isdir(direc):
|
|
|
|
dirname = direc
|
|
|
|
break
|
|
|
|
return dirname
|
|
|
|
|