sbsv2/raptor/python/sbs_dist.py
changeset 399 d84804af662b
parent 382 b8a82bb868e0
parent 398 dde9ce4a8aa8
child 406 698b9b7141d7
equal deleted inserted replaced
382:b8a82bb868e0 399:d84804af662b
     1 #
       
     2 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # raptor linux distribution creation module
       
    16 # This module allow to crate raptor linux distribution archive (tar.gz) e.g. sbs_linux_dist.tar.gz
       
    17 # Script extect the following command-line parameters:
       
    18 # 1. Archive name
       
    19 # 2. List of files/directories to include
       
    20 # By default windows specific and source files are excluded.
       
    21 # example including bin and python subdir:
       
    22 # sbs_dist.py sbs_linux_dist.tar.gz bin python
       
    23 #
       
    24 
       
    25 import os
       
    26 import re
       
    27 import tarfile
       
    28 import sys
       
    29 import dos2unix
       
    30 
       
    31 #------------------------------------------------------------------------------
       
    32 # Create tar.gz archive including given files (fileName list and tarinfo list)
       
    33 #------------------------------------------------------------------------------
       
    34 def createTarGZ(tarName, fileList):
       
    35     tar = tarfile.open(tarName, "w|gz")
       
    36     for name in fileList:
       
    37         tar.add(name)
       
    38     return tar
       
    39 
       
    40 #------------------------------------------------------------------------------
       
    41 # Lists files in each of given directories
       
    42 #------------------------------------------------------------------------------
       
    43 def listFilesInDirs(paths):
       
    44     fileList = []
       
    45     for path in paths:
       
    46         fileList.extend(listFiles(path))
       
    47     return fileList
       
    48 
       
    49 #------------------------------------------------------------------------------
       
    50 # Lists files in given directory
       
    51 #------------------------------------------------------------------------------
       
    52 def listFiles(path):
       
    53     fileList = []
       
    54     for root, dirs, files in os.walk(path):
       
    55         for index in range(len(files)):
       
    56             fileList.append(root + "/" + files[index])
       
    57     return fileList
       
    58 
       
    59 #------------------------------------------------------------------------------
       
    60 # Excludes files matching "pattern" from given files list
       
    61 #------------------------------------------------------------------------------
       
    62 def excludeFiles(fileList, pattern):
       
    63     filteredFileList = []
       
    64     regExp = re.compile(pattern)
       
    65     for fileName in fileList:
       
    66         if not(regExp.match(fileName)):
       
    67            filteredFileList.append(fileName)
       
    68     return filteredFileList
       
    69 
       
    70 
       
    71 #------------------------------------------------------------------------------
       
    72 # Groups given paths as files or directories
       
    73 #------------------------------------------------------------------------------
       
    74 def groupFilesAndDirs(filesAndDirs):
       
    75     files = []
       
    76     dirs = []
       
    77     for name in filesAndDirs:
       
    78         if os.path.isdir(name):
       
    79              dirs.append(name)
       
    80         else:
       
    81              if os.path.isfile(name):
       
    82                 files.append(name)
       
    83              else:
       
    84                   print "Warning: Neither a file nor a directory! Ignoring parameter - " + name
       
    85     return (files,dirs)
       
    86 
       
    87 #------------------------------------------------------------------------------
       
    88 # Prepares regular expression to exclude unnecessary files
       
    89 #------------------------------------------------------------------------------
       
    90 def prepareExcludedFilesRegExp():
       
    91     pathPrefixRegExp = ".*[\\\/]"
       
    92     filesRegExp = "((sbs)|(.*\.bat)|(.*\.pyc)|(.*\.cmd)|(.*\.exe)|(.*\.dll)|(sbs_dist.py)"
       
    93     filesRegExp = filesRegExp + "|(dos2unix.py)|(raptor_py2exe_setup.py)|(make)|(bash)|(bashbug))+"
       
    94     return "^" + pathPrefixRegExp + filesRegExp + "$"
       
    95 
       
    96 #------------------------------------------------------------------------------
       
    97 # Includes all files in fileList in given tar with altered executable permision (+X) for all
       
    98 #------------------------------------------------------------------------------
       
    99 def includeAsExecutable(tar, fileList):
       
   100     for f in fileList:
       
   101         tarinfo = tar.gettarinfo(f)
       
   102         # OR with 73 (001 001 001) - +X for all
       
   103         tarinfo.mode = tarinfo.mode | 73
       
   104         tar.addfile(tarinfo,file(f, "rb"))
       
   105 
       
   106 
       
   107 #------------------------------------------------------------------------------
       
   108 # Validate script parameters
       
   109 #------------------------------------------------------------------------------
       
   110 def validateParameters(tarFileName, filesToInclude):
       
   111     if not(len(tarFileName) > 0):
       
   112        print "Error: No archive name given."
       
   113        sys.exit()
       
   114     if not(len(filesToInclude) > 0):
       
   115        print "Error: No files/directories names to include in archive given."
       
   116        sys.exit()
       
   117 
       
   118 
       
   119 tarFileName = sys.argv[1]
       
   120 # files and directories
       
   121 filesAndDirsToInclude = sys.argv[2:]
       
   122 
       
   123 validateParameters(tarFileName, filesAndDirsToInclude)
       
   124 
       
   125 (filesToInclude,dirsToInclude) = groupFilesAndDirs(filesAndDirsToInclude)
       
   126 
       
   127 fileList = listFilesInDirs(dirsToInclude)
       
   128 fileList.extend(filesToInclude)
       
   129 
       
   130 filteredFileList = excludeFiles(fileList, prepareExcludedFilesRegExp())
       
   131 
       
   132 dos2unix.dos2unix("bin/sbs")
       
   133 
       
   134 tar = createTarGZ(tarFileName, filteredFileList)
       
   135 fileToBeExecutableList = ["bin/sbs", "linux-i386/bin/make", "linux-i386/bin/bash", "linux-i386/bin/bashbug",
       
   136 			  "bin/sbs_descramble"]
       
   137 includeAsExecutable(tar, fileToBeExecutableList)
       
   138 tar.close()
       
   139