OSDN Git Service

installer: modified a lot..
[karesansui/karesansui.git] / installer / installer / utils.py
index 328ff1d..1a6dc75 100644 (file)
@@ -1,9 +1,9 @@
-#/usr/bin/env python
+#!/usr/bin/env python
 # -*- coding: utf-8 -*-
 #
 # This file is part of Karesansui.
 #
-# Copyright (C) 2009 HDE, Inc.
+# Copyright (C) 2009-2010 HDE, Inc.
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -24,12 +24,26 @@ import fcntl
 import random
 import glob
 
+if __name__ == '__main__':
+    search_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+    if (search_path in sys.path) is False: sys.path.insert(0, search_path)
+
 from installer.const import *
 
 global logfile
 env = os._Environ(os.environ)
 logfile = env.get("LOGFILE")
 
+def preprint_r(var,indent=2,depth=None,return_var=False):
+    import pprint
+
+    pp = pprint.PrettyPrinter(indent=indent,depth=depth)
+    if return_var is True:
+        return pp.pformat(var)
+    else:
+        pp.pprint(var)
+        return True
+
 def dotsplit(val):
     """<comment-ja>
     ドット(.)区切りで文字列分割する。ドット(.)は)
@@ -248,19 +262,35 @@ def detect_encoding(string,encoding_list=None):
 
     return None
 
-def execute_command(command_args):
+def execute_command(command_args,user=None,env=None):
     global logfile
     ret = -1
     res = []
+    curdir = os.getcwd()
+
+    if user is None:
+        homedir = pwd.getpwuid(os.getuid())[5]
+    else:
+        try:
+            int(user)
+            homedir = pwd.getpwuid(int(user))[5]
+        except:
+            try:
+                homedir = pwd.getpwnam(user)[5]
+            except:
+                homedir = pwd.getpwuid(os.getuid())[5]
 
     subproc_args = { 'stdin': subprocess.PIPE,
                      'stdout': subprocess.PIPE,
                      'stderr': subprocess.STDOUT,
 #                     'shell': True,
-                     'cwd': os.getcwd(),
+                     'cwd': homedir,
                      'close_fds': True,
                    }
 
+    if env is not None:
+        subproc_args['env'] = env
+
     if logfile is None:
         logfile = "/dev/null"
     logf = open(logfile, "a")
@@ -1414,9 +1444,12 @@ def is_iso9660_filesystem_format(file):
                 step = step + 1
                 if step == 2:
                     break
-            elif regex.match(char):
+            #elif regex.match(char):
+            else:
                 label += char
 
+        label = label.strip()
+
         f.seek(extra_offset)
         data = f.read(len(extra_magic))
         if data == extra_magic:
@@ -1430,6 +1463,65 @@ def is_iso9660_filesystem_format(file):
 
     return retval
 
+def is_windows_bootable_iso(file):
+    retval = False
+    regexes = {
+      "Windows XP Home"                  :"WXH(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Professional"          :"WXP(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Home (SP1)"            :"XRMH(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Professional (SP1)"    :"XRMP(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Home (SP1a)"           :"X1AH(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Professional (SP1a)"   :"X1AP(CCP|FPP|OEM|VOL|OCCP)_[A-Z]{2}",
+      "Windows XP Home (SP2)"            :"VRMH(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Professional (SP2)"    :"VRMP(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Home (SP2b)"           :"VX2H(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Professional (SP2b)"   :"VX2P(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Home (SP3)"            :"GRTMH(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Home K (SP3)"          :"GRTMHK(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Home KN (SP3)"         :"GRTMHKN(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Professional (SP3)"    :"GRTMP(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Professional K (SP3)"  :"GRTMPK(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP Professional KN (SP3)" :"GRTMPKN(CCP|FPP|OEM|VOL)_[A-Z]{2}",
+      "Windows XP from Dell"             :"XP2_(PER|PRO)_ENG",
+      "Windows 7 Professional"           :"WIN_7_PROFESSIONAL",
+      "Windows 7"                        :"WIN7",
+     }
+    label = is_iso9660_filesystem_format(file)
+    if label is not False:
+        for k,v in regexes.iteritems():
+            regex_str = "%s.*\(bootable\)" % v
+            regex = re.compile(regex_str)
+            if regex.search(label):
+                retval = k
+                break
+    return retval
+
+def is_linux_bootable_iso(file):
+    retval = False
+    regexes = {
+      "Asianux"               :"Asianux",
+      "MIRACLE LINUX \\1.\\2" :"MLSE([0-9])([0-9])",
+      "Turbolinux"            :"Turbolinux",
+      "Fedora Core \\1"       :"^FC/([0-9\.]*)",
+      "CentOS \\2"            :"CentOS( \-_)([0-9].[0-9])",
+      "Red Hat Enterprise Linux \\2 \\3":"RHEL(/|\-)([0-9\.\-U]) (i386|x86_64)",
+      "Red Hat Linux/\\1"     :"Red Hat Linux/(.+)",
+      "openSUSE-\\1.\\2"      :"^SU(1[0-3])([0-9])0.00",
+      "Debian \\1"            :"^Debian (.+)",
+      "Buildix"               :"^Buildix",
+      "Ubuntu \\1"            :"^Ubuntu ([0-9].+)",
+      "Ubuntu Server \\1"     :"^Ubuntu-Server (.+)",
+     }
+    label = is_iso9660_filesystem_format(file)
+    if label is not False:
+        for k,v in regexes.iteritems():
+            regex_str = "%s.*\(bootable\)" % v
+            regex = re.compile(regex_str)
+            if regex.search(label):
+                retval = re.sub(r"""%s\(bootable\)""" % v,k,label).strip()
+                break
+    return retval
+
 def get_xml_parse(file):
     from xml.dom import minidom
 
@@ -1487,4 +1579,22 @@ def get_nums_xml_xpath(document, expression):
     result = None
     return xpath.Evaluate('count(%s)' % expression, document.documentElement)
 
+def write_ini_samevalue(param, src_filename, dst_filename):
+    retval = False
+
+    if not os.path.exists(src_filename) or not os.path.exists(dst_filename):
+        return retval
+
+    src_opts = sh_config_read(src_filename)
+    try:
+        src_opts[param]
+        dst_opts = sh_config_read(dst_filename)
+        dst_opts[param] = src_opts[param]
+        retval = sh_config_write(dst_filename,dst_opts)
+    except:
+        retval = False
+
+    return retval
 
+if __name__ == '__main__':
+    sys.exit()