buildframework/helium/sf/python/pythoncore/lib/freedisk.py
changeset 587 85df38eb4012
child 628 7c4a911dc066
equal deleted inserted replaced
217:0f5e3a7fb6af 587:85df38eb4012
       
     1 #============================================================================ 
       
     2 #Name        : freedisk.py 
       
     3 #Part of     : Helium 
       
     4 
       
     5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     6 #All rights reserved.
       
     7 #This component and the accompanying materials are made available
       
     8 #under the terms of the License "Eclipse Public License v1.0"
       
     9 #which accompanies this distribution, and is available
       
    10 #at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    11 #
       
    12 #Initial Contributors:
       
    13 #Nokia Corporation - initial contribution.
       
    14 #
       
    15 #Contributors:
       
    16 #
       
    17 #Description:
       
    18 #===============================================================================
       
    19 
       
    20 """ Checks free space on the disk before the build starts.
       
    21 
       
    22 The script is being called from the preparation.ant.xml file
       
    23 
       
    24 """
       
    25 
       
    26 import getopt, sys
       
    27 
       
    28 
       
    29 HELP_STRING = """
       
    30     -h or --help     : Displays help
       
    31 
       
    32     -d or --drive    : Requires a drive letter to be checked.
       
    33                      : E.g. -d C: (case insensitive; ':' is optional)
       
    34                      
       
    35     -s or --space    : Required space to compare with the drive for the free space"
       
    36                      : E.g. -s 2658 (an integer value in MB)
       
    37 """
       
    38 
       
    39 
       
    40 
       
    41 def print_space_report(drive, space_required):
       
    42     """
       
    43     compares the required space with current free space on the provided drive
       
    44     """
       
    45     try:
       
    46         if sys.platform == "win32":
       
    47             import win32file # pylint: disable-msg=F0401
       
    48             free_bytes = win32file.GetDiskFreeSpaceEx(drive)[0]
       
    49         elif 'java' in sys.platform:
       
    50             import java.io # pylint: disable-msg=F0401
       
    51             free_bytes = java.io.File(drive).getFreeSpace()
       
    52         else:
       
    53             import os
       
    54             import statvfs
       
    55             # pylint: disable-msg=E1101
       
    56             stats = os.statvfs(drive)
       
    57             free_bytes = stats[statvfs.F_BSIZE] * stats[statvfs.F_BAVAIL]
       
    58             
       
    59     except Exception, err_type:
       
    60         print "ERROR: Either specified drive doesn't exist or an unknown error"
       
    61         print str(err_type)
       
    62         print HELP_STRING
       
    63         sys.exit(-2)
       
    64 
       
    65     free_space = free_bytes / (1024 * 1024)
       
    66 
       
    67     print "drive:", drive
       
    68     print "Required Space:", space_required
       
    69     print "Free Space:", free_space
       
    70     
       
    71     if space_required < free_space:
       
    72         print "Enough free space"
       
    73     else:
       
    74         print "Not enough free space, exiting"
       
    75         sys.exit(-1)
       
    76 
       
    77     
       
    78 def main():
       
    79     """
       
    80     Gets and parse options and verifies the option values
       
    81     """
       
    82     try:
       
    83         opts = getopt.getopt(sys.argv[1:], "hs:d:", \
       
    84                                    ["space=", "drive=", "help"])[0]
       
    85     except getopt.GetoptError:
       
    86         # print help information and exit:
       
    87         print "ERROR: Couldn't parse the command line parameters."
       
    88         print HELP_STRING
       
    89         sys.exit(2)
       
    90 
       
    91     drive = None
       
    92     required_space = None
       
    93 
       
    94     for opt, attr in opts:
       
    95 
       
    96         if opt in ("-s", "--space"):
       
    97             required_space = int(attr)
       
    98             
       
    99         if opt in ("-d", "--drive"):
       
   100             drive = attr
       
   101             
       
   102         if opt in ("-h", "--help"):
       
   103             print HELP_STRING
       
   104             sys.exit()
       
   105     
       
   106     if required_space == None and drive == None:
       
   107         print "ERROR: No parameters are defined"
       
   108         print HELP_STRING
       
   109         sys.exit (-3)
       
   110 
       
   111     if required_space == None:
       
   112         print "ERROR: Required Disk Space parameter is not defined to" \
       
   113               "check space on the disk"
       
   114         print HELP_STRING
       
   115         sys.exit (-3)
       
   116         
       
   117     if drive == None:
       
   118         print "ERROR: Drive parameter is missing"
       
   119         print HELP_STRING
       
   120         sys.exit (-3)
       
   121     else:
       
   122         if sys.platform == "win32":
       
   123             if not ":" in drive:
       
   124                 drive = drive + ":"
       
   125 
       
   126     print_space_report(drive, required_space)
       
   127 
       
   128 
       
   129 
       
   130 
       
   131 if __name__ == '__main__':
       
   132     sys.exit(main())