Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.35 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import os.path
  5. from optparse import OptionParser
  6. from shutil import rmtree, copy, copytree
  7. import subprocess
  8.  
  9. cmdOpts = ''
  10. cmdArgs = ''
  11. config = ''
  12.  
  13. menu = """
  14. Please select your operating system:
  15. 1. Windows 32 bit
  16. 2. Windows 64 bit
  17. 3. Linux 32 bit
  18. 4. Linux 65 bit
  19. 5. OSX 32 bit
  20. 6. OSX 64 bit
  21. """
  22.  
  23. def isInt( s ):
  24.    try:
  25.       int(s)
  26.       return True
  27.    except ValueError:
  28.       return False
  29.  
  30.  
  31. class SDKPackager:
  32.    def __init__( self ):
  33.       self.versionMajor = 1
  34.       self.versionMinor = 0
  35.       self.versionMacro = 1
  36.      
  37.       self.currentDirectory = os.path.dirname(os.path.realpath(__file__))
  38.       self.platform = ""
  39.      
  40.       self.linux_32 = []
  41.       self.linux_64 = []
  42.       self.osx = []
  43.       self.osx64 = []
  44.      
  45.       print "\nSixense SDK Installation Script v%02d.%02d.%02d" % ( self.versionMajor, self.versionMinor, self.versionMacro )
  46.       if( not cmdOpts.printVersionAndExit ):
  47.          print "============================================"
  48.  
  49.    def cleanString( self, string ):
  50.       string = string.lstrip()
  51.       string = string.rstrip(" \t\r\n\0")
  52.       return string
  53.  
  54.    def cleanList( self, lst):
  55.       if "" in lst:
  56.          lst.remove("")
  57.       return lst
  58.    
  59.    def forceString( self, value ):
  60.       if not isinstance(value, str):
  61.          return value[0]
  62.       else:
  63.          return value
  64.      
  65.    def getValue( self, key ):
  66.       returnValue = []
  67.  
  68.       file = open(cmdOpts.config)
  69.       for line in file:
  70.          line = self.cleanString(line)
  71.          if len(line) == 0:
  72.             continue
  73.            
  74.          if line[0] == '#':
  75.             continue
  76.  
  77.          pairs = line.split("=")
  78.      
  79.          keyFile = self.cleanString(pairs[0])
  80.          if keyFile == key:
  81.             for element in pairs[1].split(","):
  82.                returnValue.append(self.cleanString(element))
  83.             return returnValue
  84.  
  85.       return returnValue
  86.      
  87.    def find_in_file(self, filename, searchstr):    # see whether a string appears in a file at all, and tell on which line
  88.       libfd = open(filename,"r")
  89.       contents = libfd.readlines()
  90.       libfd.close()
  91.       index = 0
  92.       for fline in contents:
  93.          if searchstr in fline:
  94.             return True, index
  95.          index = index + 1
  96.       return False, index
  97.    
  98.    def append_to_file(self, filename, newstr):
  99.       libfd = open(filename,"a")
  100.       libfd.write(newstr)
  101.       libfd.close()
  102.              
  103.    def parseConfig( self ):
  104.    
  105.       print "-----------------------------------------------"
  106.       print "Parsing Config File %s" % cmdOpts.config
  107.       print "-----------------------------------------------"
  108.      
  109.       self.parseItem()
  110.  
  111.    def parseItem( self ):
  112.       print "Finding Items"
  113.       file = open(cmdOpts.config)
  114.      
  115.       for line in file:
  116.          line = self.cleanString(line)
  117.  
  118.          if len(line) == 0:
  119.             continue
  120.            
  121.          if line[0] == '#':
  122.             continue
  123.  
  124.          pairs = line.split("=")
  125.      
  126.          key = self.cleanString(pairs[0])
  127.          value = []
  128.      
  129.          if key == "linux_32" and self.platform == "linux_32":
  130.             sys.stdout.write("Found Linux 32 bit")
  131.             sys.stdout.flush()
  132.             for element in pairs[1].split(","):
  133.                if len(element) > 0:
  134.                   self.linux_32.append(self.cleanString(element))
  135.             print "                              Done"
  136.          if key == "linux_64" and self.platform == "linux_64":
  137.             sys.stdout.write("Found Linux 64 bit")
  138.             sys.stdout.flush()
  139.             for element in pairs[1].split(","):
  140.                if len(element) > 0:
  141.                   self.linux_64.append(self.cleanString(element))
  142.             print "                             Done"
  143.          if key == "osx_32" and self.platform == "osx_32":
  144.             sys.stdout.write("Found OSX 32 bit")
  145.             sys.stdout.flush()
  146.             for element in pairs[1].split(","):
  147.                if len(element) > 0:
  148.                   self.osx.append(self.cleanString(element))
  149.             print "                                 Done"
  150.          if key == "osx_64" and self.platform == "osx_64":
  151.             sys.stdout.write("Found OSX 64 bit")
  152.             sys.stdout.flush()
  153.             for element in pairs[1].split(","):
  154.                if len(element) > 0:
  155.                   self.osx64.append(self.cleanString(element))
  156.             print "                                 Done"
  157.                
  158.       file.close()
  159.       print "Done\n"
  160.  
  161.    def chooseTargetPlatform( self ):
  162.       response = ""
  163.       print menu
  164.  
  165.       response = raw_input("Enter Selection: ")
  166.  
  167.       if response == "3":
  168.          self.platform = "linux_32"
  169.       elif response == "4":
  170.          self.platform = "linux_64"
  171.       elif response == "5":
  172.          self.platform = "osx_32"
  173.       elif response == "6":
  174.          self.platform = "osx_64"
  175.       else:
  176.          if response == "1" or response == "2":
  177.         print "Currently Unsupported Target Operating System"
  178.      else:
  179.             print "Invalid Selection"
  180.          self.chooseTargetPlatform()
  181.      
  182.    def preInstall( self ):
  183.       return
  184.      
  185.    def install( self ):
  186.       self.copyFilesHelper( self.linux_32, "Linux 32 bit" )
  187.       self.copyFilesHelper( self.linux_64, "Linux 64 bit" )
  188.       self.copyFilesHelper( self.osx, "OSX 32 bit" )
  189.       self.copyFilesHelper( self.osx64, "OSX 64 bit" )
  190.       return
  191.  
  192.    def copyFilesHelper( self, folderList, userText ):
  193.       if not cmdOpts.verbose:
  194.          self.firstErrorAfterHeader = True
  195.       destination = ""
  196.       source = ""
  197.       files = []
  198.       if len(folderList) > 0:
  199.          sys.stdout.write("- for %s\r" % userText)  
  200.          sys.stdout.flush()
  201.          if cmdOpts.verbose:  
  202.             print ""
  203.          for element in folderList:
  204.             destination = self.getValue(element+"_destination")
  205.             source = self.getValue(element+"_source")
  206.             files = self.getValue(element+"_file")
  207.             source = self.cleanList(source)
  208.             files = self.cleanList(files)
  209.             for outfile in files:
  210.                self.copyFileParser( destination, source, outfile )
  211.          if not cmdOpts.verbose:
  212.             sys.stdout.write("%46s\r" % "Done")
  213.             sys.stdout.write("- for %s\n" % userText)
  214.             sys.stdout.flush()
  215.          else:
  216.             print "Done\n"
  217.  
  218.    def copyFileParser( self, dstPath, srcPath, srcFile ):
  219.       dst = ""
  220.       src = ""
  221.       if not isinstance(dstPath, str):
  222.          dst = dstPath[0]
  223.       else:
  224.          dst =  dstPath  
  225.       if not isinstance(srcPath, str):
  226.          src = srcPath[0]
  227.       else:
  228.          src =  srcPath
  229.  
  230.       pathsExist = True
  231.      
  232.       if not os.path.isdir(src):
  233.          if self.firstErrorAfterHeader:
  234.             print ""
  235.             self.firstErrorAfterHeader = False
  236.          print "Source Path Does Not Exist: %s" % src
  237.          pathsExist = False
  238.       if not os.path.isdir(dst):
  239.          if self.firstErrorAfterHeader:
  240.             print ""
  241.             self.firstErrorAfterHeader = False
  242.          print "Destination Path Does Not Exist: %s" % dst
  243.          pathsExist = False
  244.  
  245.       if not pathsExist:
  246.          return
  247.          
  248.       #copy all files
  249.       if srcFile.split('.')[0] == '*' and srcFile.split('.')[1] == '*':
  250.          for filename in os.listdir( os.path.join(".",src) ):
  251.             self.copyFile( dst, src, filename )
  252.       #copy all files by extention
  253.       elif srcFile.split('.')[0] == '*' and srcFile.split('.')[1] != '*':
  254.          for filename in os.listdir( os.path.join(".",src) ):
  255.             if os.path.isfile(filename):
  256.                if filename.split('.')[1] == srcFile.split('.')[1]:
  257.                   self.copyFile( dst, src, filename )
  258.       #copy all files starting with <>
  259.       elif srcFile.split('.')[0] != '*' and srcFile.split('.')[1] == '*':
  260.          for filename in os.listdir( os.path.join(".",src) ):
  261.             if filename.split('.')[0] == srcFile.split('.')[0]:
  262.                self.copyFile( dst, src, filename )
  263.       #copy individual file
  264.       else:
  265.          self.copyFile( dst, src, srcFile )
  266.    
  267.    def copyFile( self, dstPath, srcPath, srcFile ):
  268.       fileExists = True
  269.          
  270.       if not os.path.isfile(os.path.join(srcPath,srcFile)):
  271.          if os.path.isdir(os.path.join(srcPath,srcFile)):
  272.             if cmdOpts.verbose:
  273.                print "Copying all files from %s to %s" % (os.path.join(srcPath,srcFile), dstPath )
  274.             copytree(srcPath,os.path.join(dstPath,srcFile))
  275.             return
  276.          else:
  277.             if self.firstErrorAfterHeader:
  278.                print ""
  279.                self.firstErrorAfterHeader = False
  280.             print "Source File Does Not Exist: %s" % os.path.join(srcPath,srcFile)
  281.             fileExists = False
  282.  
  283.       if not fileExists:
  284.          return
  285.          
  286.       if cmdOpts.verbose:
  287.          print "Copying File %s from %s to %s" % (srcFile, srcPath, dstPath )
  288.       copy( os.path.join(srcPath,srcFile), dstPath)
  289.      
  290.    def postInstall( self ):
  291.       if self.platform =="linux_32" or self.platform == "linux_64":
  292.          configFile = self.forceString(self.getValue("linux_library_config_file"))
  293.          libPath = self.forceString(self.getValue("linux_library_path"))
  294.          if os.path.isfile( configFile ):
  295.             found, index = self.find_in_file(configFile,libPath)    # is lib path already there?
  296.             if found:
  297.                print "Library path is already registered in %s, on line %d." % (configFile,index)
  298.             else:
  299.                print "Library path not registered yet. Adding library path to %s..." % configFile
  300.                self.append_to_file(configFile,"\n"+libPath+"\n")
  301.             lib_update_cmd = "ldconfig"
  302.             p = subprocess.Popen(lib_update_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
  303.          else:
  304.             print "Unable to Find ld.so.conf"
  305.      
  306.    def run( self ):
  307.       self.chooseTargetPlatform()
  308.       self.parseConfig()
  309.       self.preInstall()
  310.       self.install()
  311.       self.postInstall()
  312.      
  313. # main program starts here
  314. if __name__ == '__main__':
  315.    #configFile = "sdk_hierarchy_default"
  316.    
  317.    parser = OptionParser(usage="%prog [options]")
  318.  
  319.    parser.add_option('',"--version",
  320.                      action="store_true",
  321.                      dest="printVersionAndExit",
  322.                      default=False,
  323.                      help="Prints the version and exits",)
  324.                      
  325.    parser.add_option('-c',"--config",
  326.                      action="store",
  327.                      dest="config",
  328.                      default="install.cfg",
  329.                      help="Config File to use",)
  330.  
  331.    parser.add_option('-v',"--verbose",
  332.                      action="store_true",
  333.                      dest="verbose",
  334.                      default=False,
  335.                      help="Print Extra Information",)
  336.  
  337.    parser.add_option('-w',"--warning",
  338.                      action="store_true",
  339.                      dest="warning",
  340.                      default=False,
  341.                      help="Print Only Warning Information",)
  342.                                          
  343.    (cmdOpts, cmdArgs) = parser.parse_args()
  344.    
  345.    package = SDKPackager()
  346.    
  347.    if( cmdOpts.printVersionAndExit ):
  348.       exit()
  349.    package.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement