+ bootid_re = re.compile("bootid=[\w_-]+")
+ live_media_path_re = re.compile("live-media-path=[\w_/-]+")
+
+ bootopt = get_bootoptions(grml_flavour)
+
+ remove_regexes = []
+ option_re = re.compile(r'(.*/boot/.*(linux26|vmlinuz).*)')
+
+ if options.removeoption:
+ for regex in options.removeoption:
+ remove_regexes.append(re.compile(regex))
+
+ shortname = get_shortname(grml_flavour)
+ for filename in glob.glob(grub_target + '*.cfg'):
+ for line in fileinput.input(filename, inplace=1):
+ line = line.rstrip("\r\n")
+ if option_re.search(line):
+ line = bootid_re.sub('', line)
+ if shortname in filename:
+ line = live_media_path_re.sub('', line)
+ line = line.rstrip() + ' live-media-path=/live/%s/ ' % (grml_flavour)
+ line = line.rstrip() + r' bootid=%s %s ' % (UUID, bootopt)
+ for regex in remove_regexes:
+ line = regex.sub(' ', line)
+ print(line)
+ fileinput.close()
+
+
+def initial_syslinux_config(target):
+ """Generates intial syslinux configuration
+
+ @target path of syslinux's configuration files"""
+
+ target = target + "/"
+ filename = target + "grmlmain.cfg"
+ if os.path.isfile(target + "grmlmain.cfg"):
+ return
+ data = open(filename, "w")
+ data.write(generate_main_syslinux_config())
+ data.close()
+
+ filename = target + "hiddens.cfg"
+ data = open(filename, "w")
+ data.write("include hidden.cfg\n")
+ data.close()
+
+
+def add_entry_if_not_present(filename, entry):
+ """Write entry into filename if entry is not already in the file
+
+ @filename: name of the file
+ @entry: data to write to the file
+ """
+ data = open(filename, "a+")
+ for line in data:
+ if line == entry:
+ break
+ else:
+ data.write(entry)
+
+ data.close()
+
+
+def get_flavour_filename(flavour):
+ """Generate a iso9960 save filename out of the specified flavour
+
+ @flavour: grml flavour
+ """
+ return flavour.replace('-', '_')
+
+
+def adjust_syslinux_bootoptions(src, flavour):
+ """Adjust existing bootoptions of specified syslinux config to
+ grml2usb specific ones, e.g. change the location of the kernel...
+
+ @src: config file to alter
+ @flavour: grml flavour
+ """
+
+ append_re = re.compile("^(\s*append.*/boot/.*)$", re.I)
+ # flavour_re = re.compile("(label.*)(grml\w+)")
+ default_re = re.compile("(default.cfg)")
+ bootid_re = re.compile("bootid=[\w_-]+")
+ live_media_path_re = re.compile("live-media-path=[\w_/-]+")
+
+ bootopt = get_bootoptions(flavour)
+
+ regexe = []
+ option_re = None
+ if options.removeoption:
+ option_re = re.compile(r'/boot/.*/(initrd.gz|initrd.img)')
+
+ for regex in options.removeoption:
+ regexe.append(re.compile(r'%s' % regex))
+
+ for line in fileinput.input(src, inplace=1):
+ # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
+ line = default_re.sub(r'%s-\1' % flavour, line)
+ line = bootid_re.sub('', line)
+ line = live_media_path_re.sub('', line)
+ line = append_re.sub(r'\1 live-media-path=/live/%s/ ' % flavour, line)
+ line = append_re.sub(r'\1 boot=live %s ' % bootopt, line)
+ line = append_re.sub(r'\1 %s=%s ' % ("bootid", UUID), line)
+ if option_re and option_re.search(line):
+ for regex in regexe:
+ line = regex.sub(' ', line)
+ sys.stdout.write(line)
+ fileinput.close()
+
+
+def adjust_labels(src, replacement):
+ """Adjust the specified labels in the syslinux config file src with
+ specified replacement
+ """
+ label_re = re.compile("^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
+ for line in fileinput.input(src, inplace=1):
+ line = label_re.sub(replacement, line)
+ sys.stdout.write(line)
+ fileinput.close()
+
+
+def add_syslinux_entry(filename, grml_flavour):
+ """Add includes for a specific grml_flavour to the specified filename
+
+ @filename: syslinux config file
+ @grml_flavour: grml flavour to add
+ """
+
+ entry_filename = "option_%s.cfg" % grml_flavour
+ entry = "include %s\n" % entry_filename
+
+ add_entry_if_not_present(filename, entry)
+ path = os.path.dirname(filename)
+
+ data = open(path + "/" + entry_filename, "w")
+ data.write(generate_flavour_specific_syslinux_config(grml_flavour))
+ data.close()
+
+
+def modify_filenames(grml_flavour, target, filenames):
+ """Replace the standard filenames with the new ones
+
+ @grml_flavour: grml-flavour strin
+ @target: directory where the files are located
+ @filenames: list of filenames to alter
+ """
+ grml_filename = get_flavour_filename(grml_flavour)
+ for filename in filenames:
+ old_filename = "%s/%s" % (target, filename)
+ new_filename = "%s/%s_%s" % (target, grml_filename, filename)
+ os.rename(old_filename, new_filename)
+
+
+def remove_default_entry(filename):
+ """Remove the default entry from specified syslinux file
+
+ @filename: syslinux config file
+ """
+ default_re = re.compile("^(\s*menu\s*default\s*)$", re.I)
+ for line in fileinput.input(filename, inplace=1):
+ if default_re.match(line):
+ continue
+ sys.stdout.write(line)
+ fileinput.close()
+
+
+def handle_syslinux_config(grml_flavour, target):
+ """Main handler for generating syslinux configuration
+
+ @grml_flavour: name of grml flavour the configuration should be generated for
+ @target: path of syslinux's configuration files"""
+
+ logging.debug("Generating syslinux configuration")