#!/usr/bin/env python """Frontend for knoppix-customize. It automates setting a new language and it allows to replace the logo image. It will take care that the file size does not change. The file size of a replacement logo must be samller that the original. This program will pad smaller files and abort on larger files. 'ppmtolss16' is required for this. The action is based on the program name: knxiso-setlang knxiso-setlogo Installation: Save this script as 'knxiso-setlang', then use ln -s knxiso-setlang knxiso-setlogo to create a link. 'knoppix-customize' must be in the search PATH. (install it in /usr/local/bin) Examples: knxiso-setlang knoppix.iso us knxiso-setlogo knoppix.iso logo.ppm pxctoppm from http://hydra.hq.linuxtag.net/~kester/knoppix-customize/ Python license, Chris Liechti This is experimental software. Use at your own risk. """ import os, re, sys # helpers #~ class Tempfile: #~ """Create a temporary file that can be used by external programs. #~ This is a proxy object taht can be used as file.""" #~ def __init__(self): #~ """Create a random name and open the file""" #~ import tempfile #~ self.name = tempfile.mktemp() #~ self.file = open(self.name, 'w+b') #~ def __getattr__(self, name): #~ return getattr(self.file, name) #~ def __del__(self): #~ self.file.close() #~ os.unlink(self.name) class ISO: """take an ISO image or disk image and allow file operations on it""" def __init__(self, filename, simulate = 0): self.name = filename self.simulate = simulate def readFile(self, name): fin = os.popen('knoppix-customize --image %s --action export_file --image_file %s --local_file -' % (self.name, name)) data = fin.read() #and read all the data fin.close() return data def writeFile(self, name, data): if self.simulate: sys.stdout.write('Would have written %d bytes to file %s.\n' % (len(data), name)) else: fin, fout = os.popen2('knoppix-customize --image %s --action import_file --image_file %s --local_file -' % (self.name, name)) fin.write(data) fin.close() fout.close() def getBootdisk(self): fin = os.popen('knoppix-customize --image %s --action export_floppy --local_file -' % (self.name,)) data = fin.read() #and read all the data fin.close() return data # operations def setlang(simulate): #incomplete list of languages supported by knoppix at boot param languages = ['de', 'bg', 'ch', 'cn', 'cs', 'cz', 'da', 'dk', 'es', 'fr', 'it', 'nl', 'pl', 'ru', 'sk', 'tr', 'tw', 'uk', 'us'] if len(sys.argv) < 3: sys.stderr.write("""USAGE: knxiso-setlang imagename language use knoppix-customize to set the language of a KNOPPIX ISO image or boot disk image. supported languages (incomplete list):\n %s\n""" % ', '.join(languages)) sys.exit(1) isoname = sys.argv[1] language = sys.argv[2] sysconfig = "syslinux.cfg" if language not in languages: sys.stderr.write("warning: language not in builtin list. its possibly not supported.\n") iso = ISO(isoname, simulate) cfg = iso.readFile(sysconfig) origlen = len(cfg) cfg = re.sub(r'lang=(\w+)', 'lang=%s' % language, cfg) diffsize = len(cfg) - origlen if diffsize > 0: cfg = cfg.replace('##'+'#'*diffsize, '##', 1) #use two char at min to not acidentaly uncomment a comment elif diffsize < 0: cfg = cfg.replace('##', '##'*(-diffsize), 1) #use two char at min to not touch a comment iso.writeFile(sysconfig, cfg) def setlogo(simulate): if len(sys.argv) < 3: sys.stderr.write("""USAGE: knxiso-setlogo imagename ppmlogo use knoppix-customize to set the boot logo of a KNOPPIX ISO image or boot disk image. Examples: knxiso-setlogo knoppix.iso logo.ppm pxctoppm