home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / installHelper.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  4.8 KB  |  182 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: installHelper.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. __doc__ = 'Spyce install helper script'
  10.  
  11. import os, imp, sys, getopt, string, re, time
  12.  
  13. CONF_BEGIN_MARK = '### BEGIN SPYCE CONFIG MARKER'
  14. CONF_END_MARK = '### END SPYCE CONFIG MARKER'
  15. HTTPD_LOCATIONS = [
  16.   '/etc/httpd/conf',
  17.   r'C:\Program Files\Apache Group\Apache2\conf',
  18.   r'C:\Program Files\Apache Group\Apache\conf',
  19.   '/etc',
  20.   '/']
  21. APACHE_EXE_LOCATIONS = [
  22.   r'C:\Program Files\Apache Group\Apache2\bin',
  23.   r'C:\Program Files\Apache Group\Apache2',
  24.   r'C:\Program Files\Apache Group\Apache\bin',
  25.   r'C:\Program Files\Apache Group\Apache',
  26. ]
  27. SYS_LOCATIONS = [
  28.   r'C:\winnt\system32',
  29. ]
  30.  
  31. def endsWith(s, suffix):
  32.   suffixLen = len(suffix)
  33.   return s[-suffixLen:] == suffix
  34.  
  35. def listDirFilter(dir, extension):
  36.   files = os.listdir(dir)
  37.   files = filter(lambda file: endsWith(file, extension), files)
  38.   return files
  39.  
  40. def compilePythonDir(dir):
  41.   print '** Compiling Python files in: %s' % dir
  42.   for file in listDirFilter(dir, '.py'):
  43.     print 'Compiling: %s' % file
  44.     try:
  45.       p = os.path.join(dir, file)
  46.       f = None
  47.       try:
  48.         f = open(p, 'r')
  49.         imp.load_source(os.path.split(file)[1][:-3], p, f)
  50.       finally:
  51.         if f: f.close()
  52.     except: pass
  53.  
  54. def compileSpyceDir(dir):
  55.   import spyceCmd
  56.   print '** Processing Spyce files in: %s' % dir
  57.   for file in listDirFilter(dir, '.spy'):
  58.     print 'Processing: %s' % file
  59.     sys.argv = ['', '-o', os.path.join(dir, file[:-4]+'.html'), os.path.join(dir, file)]
  60.     spyceCmd.spyceMain()
  61.  
  62. def findLine(array, line):
  63.   line = string.strip(line)
  64.   for i in range(len(array)):
  65.     if re.search(line, string.strip(array[i])):
  66.       return i
  67.   return None
  68.  
  69. def unconfig(s):
  70.   lines = string.split(s, '\n')
  71.   begin = findLine(lines, CONF_BEGIN_MARK)
  72.   end = findLine(lines, CONF_END_MARK)
  73.   if begin!=None and end!=None and end>begin:
  74.     del lines[begin:end+1]
  75.   s = string.join(lines, '\n')
  76.   return s
  77.  
  78. def config(s, root):
  79.   append = readFile('spyceApache.conf')
  80.   root = re.sub(r'\\', '/', root)
  81.   append = string.replace(append, 'XXX', root)
  82.   append = string.split(append, '\n')
  83.   if os.name=='nt':
  84.     row = findLine(append, 'ScriptInterpreterSource')
  85.     append[row] = string.strip(re.sub('#', '', append[row]))
  86.   lines = [s] + [CONF_BEGIN_MARK] + append + [CONF_END_MARK]
  87.   s = string.join(lines, '\n')
  88.   return s
  89.  
  90. def readFile(filename):
  91.   f = None
  92.   try:
  93.     f = open(filename, 'r')
  94.     return f.read()
  95.   finally:
  96.     if f: f.close()
  97.  
  98. def writeFileBackup(filename, new):
  99.   old = readFile(filename)
  100.   backupname = filename + '.save'
  101.   f = None
  102.   try:
  103.     f = open(backupname, 'w')
  104.     f.write(old)
  105.   finally:
  106.     if f: f.close()
  107.   f = None
  108.   try:
  109.     f = open(filename, 'w')
  110.     f.write(new)
  111.   finally:
  112.     if f: f.close()
  113.  
  114. def locateFile(file, locations):
  115.   def visit(arg, dirname, names, file=file):
  116.     path = os.path.join(dirname, file)
  117.     if os.path.exists(path):
  118.       arg.append(path)
  119.     if arg:
  120.       del names[:]
  121.   found = []
  122.   for path in locations:
  123.     os.path.walk(path, visit, found)
  124.     if found:
  125.       return found[0]
  126.  
  127. def configHTTPD(spyceroot):
  128.   print '** Searching for httpd.conf...'
  129.   file = locateFile('httpd.conf', HTTPD_LOCATIONS)
  130.   if file:
  131.     print '** Modifying httpd.conf'
  132.     s = readFile(file)
  133.     s = unconfig(s)
  134.     s = config(s, spyceroot)
  135.     writeFileBackup(file, s)
  136.  
  137. def unconfigHTTPD():
  138.   print '** Searching for httpd.conf...'
  139.   file = locateFile('httpd.conf', HTTPD_LOCATIONS)
  140.   if file:
  141.     print '** Modifying httpd.conf'
  142.     s = readFile(file)
  143.     s = unconfig(s)
  144.     writeFileBackup(file, s)
  145.  
  146. def restartApache():
  147.   print '** Searching for apache.exe...'
  148.   file = locateFile('apache.exe', APACHE_EXE_LOCATIONS)
  149.   cmd = locateFile('cmd.exe', SYS_LOCATIONS)
  150.   if file and cmd:
  151.     print '** Restarting Apache'
  152.     os.spawnl(os.P_WAIT, cmd, '/c "%s" -k restart'%file)
  153.     return
  154.   print 'Could not find apache.exe'
  155.  
  156.  
  157.  
  158. def main():
  159.   try:
  160.     opts, args = getopt.getopt(sys.argv[1:], '',
  161.       ['py=', 'spy=', 'apache=', 'apacheUN',
  162.       'apacheRestart']);
  163.   except getopt.error: 
  164.     print "Syntax error"
  165.     return -1
  166.   for o, a in opts:
  167.     if o == "--py":
  168.       compilePythonDir(a); return 0
  169.     if o == "--spy":
  170.       compileSpyceDir(a); return 0
  171.     if o == "--apache":
  172.       configHTTPD(a); return 0
  173.     if o == "--apacheUN":
  174.       unconfigHTTPD(); return 0
  175.     if o == "--apacheRestart":
  176.       restartApache(); return 0
  177.   print "Syntax error"
  178.   return -1
  179.  
  180. if __name__=='__main__': 
  181.   sys.exit(main())
  182.