Implement prototype of copy_grml_files(), move f# files to isolinux directory again
[grml2usb.git] / grml2usb.py
index 8097a83..b4c971f 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # Filename:      grml2usb
-# Purpose:       install grml-system to usb-device
+# Purpose:       install grml system to usb device
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2 or any later version.
 # * write error messages to stderr
 # * log wrapper (log important messages to syslog, depending on loglevel)
 # * trap handling (umount devices when interrupting?)
+# * integrate https://www.mirbsd.org/cvs.cgi/src/sys/arch/i386/stand/mbr/mbr.S?rev=HEAD;content-type=text%2Fplain
+#   -> gcc -D_ASM_SOURCE  -D__BOOT_VER=\"GRML\" -DBOOTMANAGER -c mbr.S; ld
+#      -nostdlib -Ttext 0 -N -Bstatic --oformat binary mbr.o -o mbrmgr
 
 prog_version = "0.0.1"
 
 import os, re, subprocess, sys
 from optparse import OptionParser
+from os.path import exists, join, abspath
+from os import pathsep
+from string import split
 
 # cmdline parsing {{{
 usage = "Usage: %prog [options] <ISO[s]> <partition>\n\
@@ -79,6 +85,20 @@ def which(program):
                 return exe_file
 
     return None
+
+def search_file(filename, search_path='/bin' + pathsep + '/usr/bin'):
+   """Given a search path, find file"""
+   file_found = 0
+   paths = split(search_path, pathsep)
+   for path in paths:
+       for current_dir, directories, files in os.walk(path):
+           if exists(join(current_dir, filename)):
+              file_found = 1
+              break
+   if file_found:
+      return abspath(join(current_dir, filename))
+   else:
+      return None
 # }}}
 
 
@@ -90,7 +110,7 @@ def check_uid_root():
 
 def install_syslinux(device):
     """Install syslinux on specified device."""
-    print("syslinux %s") % device
+    print("debug: syslinux %s") % device
 
 
 def install_grub(device):
@@ -142,15 +162,44 @@ def check_for_vat(partition):
         print("Sorry, /lib/udev/vol_id not available.")
         return 1
 
-def copy_grml_files(target):
+def copy_grml_files(grml_flavour, iso_mount, target):
     """Copy files from ISO on given target"""
-    print("TODO")
+
+    # TODO: provide alternative search_file() if file information is stored in
+    # a config.ini file?
+    squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
+    print("debug: copy squashfs to %s") % target + '/live/' + grml_flavour + '.squashfs'
+
+    filesystem_module = search_file('filesystem.module', iso_mount)
+    print("debug: copy filesystem.module to %s") % target + '/live/' + grml_flavour + '.module'
+
+    kernel = search_file('linux26', iso_mount)
+    print("debug: copy kernel to %s") % target + '/boot/release/' + grml_flavour + '/linux26'
+
+    initrd = search_file('initrd.gz', iso_mount)
+    print("debug: copy initrd to %s") % target + '/boot/release/' + grml_flavour + '/initrd.gz'
+
+    logo = search_file('logo.16', iso_mount)
+    print("debug: copy logo.16 to %s") % target + '/boot/isolinux/' + 'logo.16'
+
+    for file in 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10':
+        bootsplash = search_file(file, iso_mount)
+        print("debug: copy %s to %s") % (bootsplash, target + '/boot/isolinux/' + file)
 
 
 def uninstall_files(device):
     print("TODO")
 
 
+def identify_grml_flavour(mountpath):
+    version_file = search_file('grml-version', mountpath)
+    file = open(version_file, 'r')
+    grml_info = file.readline()
+    file.close
+    grml_flavour = re.match(r'[\w-]*', grml_info).group()
+    return grml_flavour
+
+
 def main():
     if options.version:
         print("%s %s")% (os.path.basename(sys.argv[0]), prog_version)
@@ -164,25 +213,36 @@ def main():
     device = args[len(args) - 1]
     isos = args[0:len(args) - 1]
 
-    if not which("syslinux2"):
+    if not which("syslinux"):
         print("Sorry, syslinux not available. Exiting.")
         print("Please install syslinux or consider using the --grub option.")
         sys.exit(1)
-    
+
     # TODO check for valid blockdevice, vfat and mount functions
     # if device is not None:
         # check_for_vat(device)
         # mount_target(partition)
 
-    # TODO it doesn't need to be a ISO, could be /live/image as well!
+    target = '/mnt/target'
+
+    # TODO it doesn't need to be a ISO, could be /live/image as well
     for iso in isos:
-        print("iso = %s") % iso
+        print("debug: iso = %s") % iso
         # loopback_mount(iso)
         # copy_grml_files(iso, target)
         # loopback_unmount(iso)
+        iso_mount = '/mnt/test' # FIXME
+
+        grml_flavour = identify_grml_flavour(iso_mount)
+        print("debug: grml_flavour = %s") % grml_flavour
+
+        grml_flavour_short = grml_flavour.replace('-','')
+        print("debug: grml_flavour_short = %s") % grml_flavour_short
+
+        copy_grml_files(grml_flavour, iso_mount, target)
 
     if options.mbr:
-        print("would install MBR now")
+        print("debug: would install MBR now")
 
     install_bootloader(device)
 
@@ -190,4 +250,4 @@ if __name__ == "__main__":
     main()
 
 ## END OF FILE #################################################################
-# vim:foldmethod=marker expandtab ai ft=python
+# vim:foldmethod=marker expandtab ai ft=python tw=120