587
|
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):
|
628
|
42 |
""" Compares the required space with current free space on the provided drive.
|
587
|
43 |
"""
|
|
44 |
try:
|
|
45 |
if sys.platform == "win32":
|
628
|
46 |
import win32file # pylint: disable=F0401
|
587
|
47 |
free_bytes = win32file.GetDiskFreeSpaceEx(drive)[0]
|
|
48 |
elif 'java' in sys.platform:
|
628
|
49 |
import java.io # pylint: disable=F0401
|
587
|
50 |
free_bytes = java.io.File(drive).getFreeSpace()
|
|
51 |
else:
|
|
52 |
import os
|
|
53 |
import statvfs
|
628
|
54 |
# pylint: disable=E1101
|
587
|
55 |
stats = os.statvfs(drive)
|
|
56 |
free_bytes = stats[statvfs.F_BSIZE] * stats[statvfs.F_BAVAIL]
|
|
57 |
|
628
|
58 |
except (IOError, win32file.error), err_type:
|
587
|
59 |
print "ERROR: Either specified drive doesn't exist or an unknown error"
|
|
60 |
print str(err_type)
|
|
61 |
print HELP_STRING
|
|
62 |
sys.exit(-2)
|
|
63 |
|
|
64 |
free_space = free_bytes / (1024 * 1024)
|
|
65 |
|
|
66 |
print "drive:", drive
|
|
67 |
print "Required Space:", space_required
|
|
68 |
print "Free Space:", free_space
|
|
69 |
|
|
70 |
if space_required < free_space:
|
|
71 |
print "Enough free space"
|
|
72 |
else:
|
|
73 |
print "Not enough free space, exiting"
|
|
74 |
sys.exit(-1)
|
|
75 |
|
|
76 |
|
|
77 |
def main():
|
628
|
78 |
""" Gets and parse options and verifies the option values.
|
587
|
79 |
"""
|
|
80 |
try:
|
|
81 |
opts = getopt.getopt(sys.argv[1:], "hs:d:", \
|
|
82 |
["space=", "drive=", "help"])[0]
|
|
83 |
except getopt.GetoptError:
|
|
84 |
# print help information and exit:
|
|
85 |
print "ERROR: Couldn't parse the command line parameters."
|
|
86 |
print HELP_STRING
|
|
87 |
sys.exit(2)
|
|
88 |
|
|
89 |
drive = None
|
|
90 |
required_space = None
|
|
91 |
|
|
92 |
for opt, attr in opts:
|
|
93 |
|
|
94 |
if opt in ("-s", "--space"):
|
|
95 |
required_space = int(attr)
|
|
96 |
|
|
97 |
if opt in ("-d", "--drive"):
|
|
98 |
drive = attr
|
|
99 |
|
|
100 |
if opt in ("-h", "--help"):
|
|
101 |
print HELP_STRING
|
|
102 |
sys.exit()
|
|
103 |
|
|
104 |
if required_space == None and drive == None:
|
|
105 |
print "ERROR: No parameters are defined"
|
|
106 |
print HELP_STRING
|
|
107 |
sys.exit (-3)
|
|
108 |
|
|
109 |
if required_space == None:
|
|
110 |
print "ERROR: Required Disk Space parameter is not defined to" \
|
|
111 |
"check space on the disk"
|
|
112 |
print HELP_STRING
|
|
113 |
sys.exit (-3)
|
|
114 |
|
|
115 |
if drive == None:
|
|
116 |
print "ERROR: Drive parameter is missing"
|
|
117 |
print HELP_STRING
|
|
118 |
sys.exit (-3)
|
|
119 |
else:
|
|
120 |
if sys.platform == "win32":
|
|
121 |
if not ":" in drive:
|
|
122 |
drive = drive + ":"
|
|
123 |
|
|
124 |
print_space_report(drive, required_space)
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
if __name__ == '__main__':
|
|
130 |
sys.exit(main())
|