common/tools/findPhysicalDrive.pl
author Dario Sestito <darios@symbian.org>
Thu, 02 Sep 2010 13:32:47 +0100
changeset 1244 c04fc0405d17
parent 955 28714977dccb
permissions -rw-r--r--
Account for total number of releasables when translating uh to brag and to Diamonds
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
858
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     1
#!perl -w
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     2
#
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     3
# Copyright (c) 2010 Symbian Foundation Ltd
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     4
# This component and the accompanying materials are made available
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     6
# which accompanies this distribution, and is available
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     8
#
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     9
# Initial Contributors:
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    10
# Symbian Foundation Ltd - initial contribution.
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    11
# 
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    12
# Contributors:
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    13
#
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    14
# Description:
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    15
# Search physical drives to find either:
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    16
#  * The one with the largest amount of free space
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    17
#  * The one with the greatest capacity
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    18
#  * The list of all such drives
858
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    19
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    20
use strict;
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    21
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    22
use Getopt::Long;
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    23
use Win32::OLE;
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    24
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    25
# Read option arguments
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    26
my $option;
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    27
my $ok = GetOptions(
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    28
	'capacity' => \$option->{capacity},
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    29
	'space' => \$option->{space},
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    30
	'all' => \$option->{all},
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    31
	'help|?' => \$option->{help},
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    32
);
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    33
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    34
if (defined $option->{help})
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    35
{
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    36
	usage();
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    37
	exit;
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    38
}
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    39
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    40
if (!$ok || @ARGV || 1 != scalar grep { defined $option->{$_} } keys %$option)
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    41
{
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    42
	warn "Exactly one option must be supplied to indicate the required output\n$ok\n@ARGV\n";
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    43
	usage();
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    44
	exit(1);
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    45
}
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    46
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    47
# Connect to WMI services on this machine (".")
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    48
my $wmiServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel=impersonate,(security)}//." ) or die;
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    49
# Get list of all volumes (drive letters)
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    50
my @volumes = Win32::OLE::in($wmiServices->InstancesOf( "Win32_LogicalDisk" ));
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    51
# Get list of substed drives
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    52
my %subst = map { (substr $_, 0, 2) => 1 } `subst`;
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    53
# Filter volumes to remove non-Partitions, and substed drives
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    54
@volumes = grep { $_->{DriveType} == 3 && !exists $subst{$_->{DeviceID}} } @volumes;
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    55
# Also remove the system drive (usually C:) unless it's the only drive in the box!
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    56
@volumes = grep { $_->{DeviceID} ne $ENV{SystemDrive} } @volumes if scalar(@volumes) > 1;
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    57
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    58
die "Unable to find any suitable drives at all\n" unless @volumes;
858
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    59
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    60
if ($option->{all})
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    61
{
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    62
	print join ",", map { $_->{DeviceID} } @volumes;
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    63
	print "\n";
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    64
}
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    65
elsif ($option->{capacity})
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    66
{
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    67
	# Sort by capacity to find the largest volume and print out the corresponding letter
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    68
	@volumes = reverse sort { $a->{Size} <=> $b->{Size} } @volumes;
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    69
	print "$volumes[0]->{DeviceID}\n";
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    70
}
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    71
elsif ($option->{space})
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    72
{
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    73
	# Sort by space to find the volume with the largest amount of space and print out the corresponding letter
955
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    74
	@volumes = reverse sort { $a->{FreeSpace} <=> $b->{FreeSpace} } @volumes;
28714977dccb Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
Simon Howkins <simonh@symbian.org>
parents: 924
diff changeset
    75
	print "$volumes[0]->{DeviceID}\n";
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    76
}
858
f9fc2a3f8f70 Code to automatically find a physical drive for running a build, triggered as necessary.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    77
891
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    78
exit;
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    79
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    80
sub usage
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    81
{
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    82
	$0 =~ m{[\\/]([^\\/]*)$};
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    83
	print <<EOT;
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    84
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    85
Usage: $1 -all | -capacity | -space | -help
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    86
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    87
  -all          Outputs all physical drives in the system (separated by ',').
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    88
  -capacity     Outputs physical drive of greatest capacity in the system.
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    89
  -space        Outputs physical drive with greatest free space in the system.
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    90
  -help         Outputs this help message.
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    91
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    92
EOT
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    93
}
6c56420d1006 Updated capabilities of findPhysicalDrive to support imminent work on locating the hg cache.
Simon Howkins <simonh@symbian.org>
parents: 858
diff changeset
    94