def test_error(self): target = self.make_target('original content') os.chmod(target, 0) self.addCleanup(os.chmod, target, 0666) with self.assertRaises(IOError) as ctx: configfile.write({'bind': '1.2.3.4'}, target) expected_error = "[Errno 13] Permission denied: '{}'".format(target) self.assertEqual(expected_error, bytes(ctx.exception))
def callback(service_name): options = _get_service_options(config, slave_relation) hookutils.log( 'Writing configuration file for {}.'.format(service_name)) changed = configfile.write(options, settings.REDIS_CONF) if changed: hookutils.log('Restarting service due to configuration change.') host.service_restart(settings.SERVICE_NAME) # If the configuration changed, it is possible that related units # require notification of changes. For this reason, update all the # existing established relations. This is required because # "services.provide_data" is only called when the current hook # is a relation joined or changed. _update_relations(filter(None, [db_relation, master_relation])) else: hookutils.log('No changes detected in the configuration file.')
def test_no_changes(self): target = self.make_target('bind 1.2.3.4\n') changed = configfile.write({'bind': '1.2.3.4'}, target) self.assertFalse(changed) self.assert_file_content(target, 'bind 1.2.3.4\n') self.assertFalse(os.path.exists(target + '.bak'))
def test_existing_target(self): target = self.make_target('original content') changed = configfile.write({'bind': '1.2.3.4', 'port': 7000}, target) self.assertTrue(changed) self.assert_file_content(target, 'bind 1.2.3.4\nport 7000\n') self.assert_file_content(target + '.bak', 'original content')
def test_unexisting_target(self): target = self.make_target() changed = configfile.write({'bind': '1.2.3.4', 'port': 4242}, target) self.assertTrue(changed) self.assert_file_content(target, 'bind 1.2.3.4\nport 4242\n') self.assertFalse(os.path.exists(target + '.bak'))
def parse(): """parse arguments given in command line and fetch default config from config file.""" import shlex from subprocess import call import sys import configfile import constants from misc import itmfilter, rm_brackets parser = argparse.ArgumentParser( description='qstat wrapper for better output. \ Available ITEMS are "' + ''.join(constants.itms.keys()) + '" see -i option for their description.', add_help=False) parser.add_argument('-c', '--config', nargs='?', const=None, default=constants.path_config, metavar='FILE', help='specify config file, write current config \ if called without argument') parser.add_argument('--default_config', action='store_true', help='config file set to default config') args, remaining_argv = parser.parse_known_args() if args.default_config: configfile.write(constants.default_config, constants.path_config) sys.exit() config_to_stdout = not args.config parser = add_args(parser, configfile.read(args)) args = parser.parse_args(remaining_argv) args.out = itmfilter(args.out) args.total = itmfilter(args.total, True) args.sort = itmfilter(args.sort) if config_to_stdout: configfile.write(vars(args), sys.stdout) sys.exit() if args.edit_config: call(shlex.split(args.editor + ' ' + constants.path_config)) sys.exit() if args.edit_interactive: return args args.sep = rm_brackets(args.sep) args.sep_tot = rm_brackets(args.sep_tot) args.start_format = args.start_format.replace('{', '%').replace('}', '') if not args.out_format: args.out_format = args.sep.join('{{' + itm + ':{' + itm + '}}}' for itm in args.out) return args
def main(): """execute qstat and produces output according to chosen options.""" from datetime import datetime from subprocess import Popen, PIPE import xml.etree.ElementTree as ET import cmdargs import configfile import constants from job import Job, JobList, JobGroup args = cmdargs.parse() if args.edit_interactive: print(constants.path_config+':\n') print('option: current value (default)> enter new value') print('empty string to keep current value') print('single x to set to default value') print('trailing spaces to set to an actual x/empty string', end='\n\n') args = vars(args) for opt, dflt_val in constants.default_config.items(): new_val = input('{}: {} ({})> '.format(opt, args[opt], dflt_val)) if new_val: if new_val == 'x': args[opt] = dflt_val else: args[opt] = new_val if not str(args['width_tot']).isdigit(): args['width_tot'] = constants.default_config['width_tot'] configfile.write(args, constants.path_config) sys.exit() if args.items: print(*('{}: {}'.format(k, v.dscr) for k, v in constants.itms.items()), sep='\n') sys.exit() if args.file: qstat_out = args.file else: qstat_out = Popen(args.qstat_cmd + ' -u "' + args.users + '" -xml -r', shell=True, stdout=PIPE).stdout qstat_out = ET.parse(qstat_out).getroot().iter('job_list') alljobs = [] today = datetime.today() for j in qstat_out: alljobs.append(Job(j, args, today)) if not alljobs: if not args.mute: print('No pending or running job.') else: alljobs = JobList(alljobs, args) out_gen = (alljobs.rep(), alljobs.rep_tot()) for line in out_gen[args.reverse]: print(line) if args.out and args.total: print() for line in out_gen[not args.reverse]: print(line)