Example #1
0
def copyDataFolders(srcRootPath, destRootPath, ownerAccount=None):
    copySuccess = True
    
    # Check if there is available space on destination drive
    # to copy folders.
    freeSpace = getFreeSpace(destRootPath, "GB")
    
    # Get total size of source folders
    srcSize = getDirSize(srcRootPath, "GB")

    print '{:<34}{:>10.4f}{:>3}'.format("Available space to copy folders:", freeSpace, " GB")
    print '{:<34}{:>10.4f}{:>3}'.format("Size of folders to copy:", srcSize, " GB")
    print
        
    if srcSize >= freeSpace:
        totalCopySuccess = False
        print
        print "ERROR: Not enough available space to copy folders/files."
        print 
    else:
        
        # Get list of top-level directories and files
        returnFolders = 1   #Yes
        recursive = 0       #No
        dirList = listFiles(srcRootPath, "*", recursive, returnFolders)
        
        x = 0
        for srcPath in dirList:
            # List may have files so test for directory
            if os.path.isdir(srcPath):
                pathType = "Folder"
            elif os.path.isfile(srcPath):
                pathType = "File"
            else:
                pathType = ""
                
            # "Create" destination path
            destPath = os.path.join(destRootPath, os.path.basename(srcPath))
            
            print
            print "- Copying " + pathType.lower() + "..."
            print '{:<16}{:<100}'.format("\tSource:", srcPath)
            print '{:<16}{:<100}'.format("\tDestination:", destPath)
            
            # Copy data and check results
            results = copyData(srcPath, destPath)
            success = checkResults(results, printMsg)
            if not success:
                copySuccess = success
                
            # Change ownership function doesn't seem to be working
            # so for time being lets comment out this function call.
            # if not success:
            #     copySuccess = success
            # else:
            #     if ownerAccount:
            #         changeOwnership(destPath, ownerAccount)
                    
    return copySuccess
Example #2
0
def main():

    total_success = True

    # Before executing rest of script, lets check if 7-zip exe exists
    if not os.path.exists(sevenZipExePath):
        print 'ERROR: File path set by "sevenZipExePath" variable (' + \
            sevenZipExePath + ') does not exist. Exiting script.'
        sys.exit(exitErrCode)
        
    # Get script parameters
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    sd_root_folder, output_file = results
    
    try:            
        write_list = []
        
        sd_files = listFiles(sd_root_folder, '*.sd')
        
        for sd_file in sd_files:
            full_service_name = get_servicename_from_sdfile(sd_file)
            write_list.append(full_service_name)
        
        write_list.sort()
        
        if len(write_list) > 0:
            out_f = open(output_file, 'w')
            for write_str in write_list:
                out_f.write('{}\n'.format(write_str))
            
        print '\n\nDone. Output written to file: {}\n'.format(output_file)
        
    except:
        total_success = False
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
     
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        
    finally:
        if out_f:
            out_f.close()
            
        if total_success:
            sys.exit(0)
        else:
            sys.exit(exitErrCode)
Example #3
0
def findFilePath(rootPath, fileName, returnFirst=True):
    # Given a root path to search ("rootPath") find the path of the file
    # with name "fileName". "fileName" variable can also be a pattern
    # (i.e. *.txt).
    # NOTE: if more then one file is found, it returns only first path found.
    # NOTE: if returnFirst=True the only first found file is returned as string
    #   otherwise all files are returned as list

    fileList = walkingDirTrees.listFiles(rootPath, fileName)

    if returnFirst:
        return fileList[0]
    else:
        return fileList
Example #4
0
def findFilePath(rootPath, fileName, returnFirst=True):
    # Given a root path to search ("rootPath") find the path of the file
    # with name "fileName". "fileName" variable can also be a pattern
    # (i.e. *.txt).
    # NOTE: if more then one file is found, it returns only first path found.
    # NOTE: if returnFirst=True the only first found file is returned as string
    #   otherwise all files are returned as list
    
    fileList = walkingDirTrees.listFiles(rootPath, fileName)
    
    if returnFirst:
        return fileList[0]
    else:
        return fileList
Example #5
0
def findFolderPath(rootPath, folderName, returnFirst=True):
    # Given a root path to search ("rootPath") find the path of the folder
    # with name "folderName". "folderName" variable can also be a pattern
    # (i.e. *.txt).
    # NOTE: if more then one folder is found, it returns only first path found.
    # NOTE: if returnFirst=True the only first found folder is returned as string
    #   otherwise all files are returned as list
    
    # Traverse folder structure. This will return both folders and files.
    folderList = walkingDirTrees.listFiles(rootPath, folderName, recurse=1, return_folders=1)
    
    # Edit list in place; only keep those paths which are directories.
    folderList[:] = [folderPath for folderPath in folderList if os.path.isdir(folderPath)]
    
    if returnFirst:
        return folderList[0]
    else:
        return folderList
Example #6
0
def get_sd_files(sdRootFolder):
    ''' Return collection of latest .sd files contained with specified root folder '''
        
    fileInfo = dict()
    
    # Create temporary extract folder
    extractFolder = tempfile.mkdtemp()
    
    # Search for all .sd files contained with specified folder
    sdFilePaths = listFiles(sdRootFolder, sdFilePattern)
    
    # Create collection containing only the latest "version" of each .sd file
    for sdFile in sdFilePaths:

        # Extract service configuration json file from compressed .sd file
        extractFile = 'serviceconfiguration.json'
        jsonFile = os.path.join(extractFolder, extractFile)
        extractFromSDFile(sdFile, extractFolder, extractFile)
        
        # Extract service information from the service config json file
        os.chdir(extractFolder)
        serviceConfig = json.load(open(extractFile))
        folderName = serviceConfig['folderName']
        serviceName = serviceConfig['service']['serviceName']
        serviceType = serviceConfig['service']['type']
        
        # Create dictionary key (syntax: Folder//ServerName.ServiceType)
        fileKey = serviceName + '.' + serviceType
        if folderName:
            fileKey = folderName + '//' + fileKey
            
        # Get file creation/modified times (values in epoch time)
        # TODO: decide if we need to use creation or modified time (or compare the two)
        #  dates and use newest date to use as comparsion
        # Think we should use modfied time, because I've seen cases where you copy the file and the
        # creation time will reflect when you have copied the file and modified time reflected
        # the original creation date.
        creationTime = os.path.getctime(sdFile)
        modifiedTime = os.path.getmtime(sdFile)
        compareTime = modifiedTime # set which time to compare here; downstream code uses 'compareTime' variable.
        
        if fileKey in fileInfo:
            # Compare time of file that exists in collection and replace with
            # current file if timestamp is newer
            fileTime = fileInfo[fileKey]['timestamp']
            if compareTime > fileTime:
                fileInfo[fileKey] = {'path': sdFile, 'timestamp': compareTime}
        else:
            fileInfo[fileKey] = {'path': sdFile, 'timestamp': compareTime}
    
    if debug:
        print '\nwithin the get_sd_files function...'
        print 'fileInfo variable...'
        for i in fileInfo:
            print i + ': ' + fileInfo[i]['path'] + ': ' + str(fileInfo[i]['timestamp'])
    
    # Delete temporary extract folder
    # TODO: see if I can fix the error that is thrown when running the following code:
    # 'WindowsError: [Error 32] The process cannot access the file because it is being used
    # by another process'
    #if os.path.exists(extractFolder):
    #    rmtree(extractFolder)
    
    return fileInfo
    # ---------------------------------------------------------------------
    print "\n===================================================================================="
    print "Publishing Services to ArcGIS Server site"
    print "===================================================================================="
    print "Start time: " + str(startTime) + "\n"

    # ---------------------------------------------------------------------
    # Traverse SD root folder and look for .sd files
    # ---------------------------------------------------------------------
    if doFindSDFiles:

        print "\n---------------------------------------------------------------------------"
        print "- Looking for service definition files (" + filePattern + ") in folder:"
        print "\t" + rootSDFolderPath
        print "---------------------------------------------------------------------------"
        sdFilePaths = walkingDirTrees.listFiles(rootSDFolderPath, filePattern)
        
        numFilesFound = len(sdFilePaths)
        
        if numFilesFound == 0:
            totalSuccess = False
            raise Exception("ERROR: No .sd files were found in folder '" + rootSDFolderPath + "'. Can't continue publishing.")
        
        print "- Found the following " + str(numFilesFound) + " .sd files:\n"
        for sdFilePath in sdFilePaths:
            print "\t" + sdFilePath + "\n"        
        
    # ---------------------------------------------------------------------
    # Create AGS publisher connection file
    # ---------------------------------------------------------------------
    if doCreateAGSConnFile:
Example #8
0
def get_sd_files(sdRootFolder):
    ''' Return collection of latest .sd files contained with specified root folder '''

    fileInfo = dict()

    # Create temporary extract folder
    extractFolder = tempfile.mkdtemp()

    # Search for all .sd files contained with specified folder
    sdFilePaths = listFiles(sdRootFolder, sdFilePattern)

    # Create collection containing only the latest "version" of each .sd file
    for sdFile in sdFilePaths:

        # Extract service configuration json file from compressed .sd file
        extractFile = 'serviceconfiguration.json'
        jsonFile = os.path.join(extractFolder, extractFile)
        extractFromSDFile(sdFile, extractFolder, extractFile)

        # Extract service information from the service config json file
        os.chdir(extractFolder)
        serviceConfig = json.load(open(extractFile))
        folderName = serviceConfig['folderName']
        serviceName = serviceConfig['service']['serviceName']
        serviceType = serviceConfig['service']['type']

        # Create dictionary key (syntax: Folder//ServerName.ServiceType)
        fileKey = serviceName + '.' + serviceType
        if folderName:
            fileKey = folderName + '//' + fileKey

        # Get file creation/modified times (values in epoch time)
        # TODO: decide if we need to use creation or modified time (or compare the two)
        #  dates and use newest date to use as comparsion
        # Think we should use modfied time, because I've seen cases where you copy the file and the
        # creation time will reflect when you have copied the file and modified time reflected
        # the original creation date.
        creationTime = os.path.getctime(sdFile)
        modifiedTime = os.path.getmtime(sdFile)
        compareTime = modifiedTime  # set which time to compare here; downstream code uses 'compareTime' variable.

        if fileKey in fileInfo:
            # Compare time of file that exists in collection and replace with
            # current file if timestamp is newer
            fileTime = fileInfo[fileKey]['timestamp']
            if compareTime > fileTime:
                fileInfo[fileKey] = {'path': sdFile, 'timestamp': compareTime}
        else:
            fileInfo[fileKey] = {'path': sdFile, 'timestamp': compareTime}

    if debug:
        print '\nwithin the get_sd_files function...'
        print 'fileInfo variable...'
        for i in fileInfo:
            print i + ': ' + fileInfo[i]['path'] + ': ' + str(
                fileInfo[i]['timestamp'])

    # Delete temporary extract folder
    # TODO: see if I can fix the error that is thrown when running the following code:
    # 'WindowsError: [Error 32] The process cannot access the file because it is being used
    # by another process'
    #if os.path.exists(extractFolder):
    #    rmtree(extractFolder)

    return fileInfo
    # ---------------------------------------------------------------------
    print "\n===================================================================================="
    print "Publishing Services to ArcGIS Server site"
    print "===================================================================================="
    print "Start time: " + str(startTime) + "\n"

    # ---------------------------------------------------------------------
    # Traverse SD root folder and look for .sd files
    # ---------------------------------------------------------------------
    if doFindSDFiles:

        print "\n---------------------------------------------------------------------------"
        print "- Looking for service definition files (" + filePattern + ") in folder:"
        print "\t" + rootSDFolderPath
        print "---------------------------------------------------------------------------"
        sdFilePaths = walkingDirTrees.listFiles(rootSDFolderPath, filePattern)

        numFilesFound = len(sdFilePaths)

        if numFilesFound == 0:
            totalSuccess = False
            raise Exception("ERROR: No .sd files were found in folder '" +
                            rootSDFolderPath + "'. Can't continue publishing.")

        print "- Found the following " + str(numFilesFound) + " .sd files:\n"
        for sdFilePath in sdFilePaths:
            print "\t" + sdFilePath + "\n"

    # ---------------------------------------------------------------------
    # Create AGS publisher connection file
    # ---------------------------------------------------------------------