def test_get_fullpath(self): '''test_get_fullpath will test the get_fullpath function ''' print("Testing utils.get_fullpath...") from sutils import get_fullpath tmpfile = tempfile.mkstemp()[1] print("Case 1: File exists, should return full path") self.assertEqual(get_fullpath(tmpfile), tmpfile) print("Case 2: File doesn't exist, should return error") os.remove(tmpfile) with self.assertRaises(SystemExit) as cm: get_fullpath(tmpfile) self.assertEqual(cm.exception.code, 1) print("Case 3: File doesn't exist, should return None") self.assertEqual(get_fullpath(tmpfile, required=False), None)
def configure(args): # Get fullpath to each file, and concurrently check that exists defaultfile = get_fullpath(args.defaults) # ../src/lib/config_defaults.h infile = get_fullpath(args.infile) # singularity.conf.in # Find define statements define_re = re.compile("#define ([A-Z_]+) (.*)") # Read in input and default files defaultfile = read_file(defaultfile) data = "".join(read_file(infile)) # Lookup for values we want replaced lookup = {'0': 'no', '1': 'yes'} defaults = {} # Read in defaults to dictionary for line in defaultfile: match = define_re.match(line) if match: key, value = match.groups() # Maintain the original default set by user defaults[key] = value # Use parsed value for final config new_value = value.replace('"', '') if new_value in lookup: new_value = lookup[new_value] data = data.replace("@" + key + "@", new_value) # Write to output file outfile = "%s.tmp" % args.outfile write_file(outfile, data) os.rename(outfile, args.outfile) bot.info("*** FINISHED PYTHON CONFIGURATION HELPER ****\n")