#!c:/Python26/python.exe -u
#######################################################################
# $Id: defaultTemplate.py,v 1.1.1.1 2005/07/22 13:35:49 slothrop Exp $
#######################################################################
#
# File:          proe-installer.py
# Description:   This script is used to install Pro/E from inside WPKG.
#                The initial process used to install Pro/E, setup.exe, 
#                sponses a secondary process and exits.  The exit of setup.exe
#                cuases WPKG to think the install is over and the next package 
#                is installed. This script starts setup.exe and monitors the 
#                active process list for the secondary Pro/E install process.
#                Once the secondary Pro/E install process has completed this
#                script will exit.  The script has a timeout to prevent
#                an infinate loop.
#
# Return Values:
# 0 = Success
#
# Changelog:
# * 2008/12/12 breese <breese@boston-engineering.com>
# - Intial script
#######################################################################
from win32com.client import GetObject
import sys
import re
import time
import os

#************************************************#
# Local Vars
#************************************************#
SLEEP_TIME=20                    # Time to sleep on each loop.
PROCESS_REGEX="^ptc.*_tmp\.exe$" # regex string to match Pro/E install process
MAX_LOOPS=50                     # Maxium number of wait loops
REMOTE_DATA_PATH="\\\\samba2\\data"# Remote drive share that contains install data

#************************************************#
# Inputs
#************************************************#
TRAIL_PATH=""
INSTALL_CMD=""
MOUNT_DRIVE=""
if len(sys.argv) >= 4:
    MOUNT_DRIVE=sys.argv[1]
    INSTALL_CMD=sys.argv[2]
    TRAIL_PATH=sys.argv[3]

#************************************************#
# Local Functions
#************************************************#
def foundProcess(processes, processNamePattern):
    """
    Search for a process name pattern in set of processes.
    @type processes: Win32_Process
    @param processes: A group of system processes
    @type processNamePattern: string
    @param processNamePattern: A regex pattern
    """
    regex = re.compile(processNamePattern)
    for process in processes:
        processName = process.Properties_('Name').Value
        if regex.match(processName) is not None:
            print "Found process %s." % (processName)
            return True
    return False

def mountDrive(drive, systemToMount):
    """
    Mount the given drive. ***WINDOWS ONLY***
    @type drive: string
    @param drive: The drive to mount
    @type systemToMount: string
    @param systemToMount: The system to mount
    """
    unmountDrive(drive)
    os.system("c:\\windows\\system32\\net.exe use %s: %s" % (drive,
                systemToMount)) 

def unmountDrive(drive):
    """
    Unmount the given drive. ***WINDOWS ONLY***
    @type drive: string
    @param drive: The drive to unmount
    """
    os.system("c:\\windows\\system32\\net.exe use %s: /DELETE /YES" % (drive))


#************************************************#
#Main script start
#************************************************#

print "Preping system for install..."
mountDrive(MOUNT_DRIVE, REMOTE_DATA_PATH)

print "Installing Pro/E..."
print "%s:\%s %s:\%s" % (MOUNT_DRIVE, INSTALL_CMD, MOUNT_DRIVE, TRAIL_PATH)
os.system("%s:\%s %s:\%s" % (MOUNT_DRIVE, INSTALL_CMD, MOUNT_DRIVE, TRAIL_PATH))

# Setup windows system objects
wmi = GetObject('winmgmts:')

# Wait for Pro/E to complete install before exiting. This loop will
# sleep for SLEEP_TIME if the Pro/E install process is found.  The loop will
# exit once the process is complete or if MAC_LOOP is reached.
loopCounter=1
while loopCounter < MAX_LOOPS:
    print "Loop %s of %s. Waiting %s secs..." % (loopCounter, MAX_LOOPS, 
            SLEEP_TIME)  
    time.sleep(SLEEP_TIME)
    processes = wmi.InstancesOf('Win32_Process')
    if not foundProcess(processes, PROCESS_REGEX):
        print "Install complete. Cleaning up system..."
        unmountDrive(MOUNT_DRIVE)
        sys.exit(0)
    loopCounter = loopCounter + 1
