예제 #1
0
def main():
    exit_err_code = 1
    starting_cwd = os.getcwd()

    # Print/get script arguments
    results = print_args()
    if not results:
        sys.exit(exit_err_code)
    portal_address, adminuser, password, id_mapping_file_path, search_query = results

    total_success = True
    title_break_count = 100
    section_break_count = 75

    print '=' * title_break_count
    print 'Update Portal GUIDs'
    print '=' * title_break_count

    if not os.path.exists(id_mapping_file_path):
        print '\nFile {} does not exist. Exiting.'.format(id_mapping_file_path)
        sys.exit(0)

    try:

        portal = Portal(portal_address, adminuser, password)

        print '\n{}'.format('-' * section_break_count)
        print '- Searching for portal items...\n'
        items_temp = portal.search(q=search_query, sort_field='owner')

        items = []
        for item in items_temp:
            if not item['owner'].startswith('esri_'):
                items.append(item)

        for item in items:
            print format_item_info(item)

        print '\nFound {} items.'.format(len(items))

        if len(items) > 0:
            user_response = raw_input(
                "\nDo you want to continue with the update? Enter 'y' to continue: "
            )
            if validate_user_repsonse_yesno(user_response):

                # Open id mapping file
                file_dir = os.path.dirname(id_mapping_file_path)
                file_name = os.path.basename(id_mapping_file_path)
                if len(file_dir) == 0:
                    file_dir = os.getcwd()
                os.chdir(file_dir)
                id_mapping = json.load(open(file_name))

                print '\n{}'.format('-' * section_break_count)
                print '- Updating item and item data...\n'
                for item in items:
                    print format_item_info(item)
                    for id_map in id_mapping:
                        search = id_map.get(search_key)
                        replace = id_map.get(replace_key)
                        update_item_properties(portal, item, search, replace)
                        update_item_data(portal, item, search, replace)

    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:
        os.chdir(starting_cwd)
        print '\nDone.'
        if total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)
def main():
    exit_err_code = 1
    
    # Print/get script arguments
    results = print_args()
    if not results:
        sys.exit(exit_err_code)
    portal_address, adminuser, password, delete_option = results
    
    total_success = True
    title_break_count = 150
    section_break_count = 100
    
    print '=' * title_break_count
    print 'Find Hosted Services'
    print '=' * title_break_count
    
    try:
        file_name = 'hosted_service_item_mapping.json'
        if os.path.exists(file_name):
            os.remove(file_name)
                
        portal = Portal(portal_address, adminuser, password)
        
        print '\n{}'.format('-' * section_break_count)
        print '- Searching for hosted service items...'
        hosted_service_items = get_hosted_service_items(portal)
        
        print '- Found {} hosted service items:'.format(len(hosted_service_items))
        for hosted_service_item in hosted_service_items:
            print format_item_info(hosted_service_item)
        
        print '\n{}'.format('-' * section_break_count)
        print '- Searching for orphaned hosted service items...'
        o_hosted_service_items = get_orphaned_hosted_service_items(portal)
        
        print '- Found {} orphaned hosted service items:'.format(len(o_hosted_service_items))
        for o_hosted_service_item in o_hosted_service_items:
            print format_item_info(o_hosted_service_item)
        
        print '\n{}'.format('-' * section_break_count)
        print '- Map orphaned hosted service item to hosted service item...'
        item_mapping = create_search_replace_id_map(portal)
        if len(item_mapping) == 0:
            print 'No orphaned service items to map.'
            
        if len(item_mapping) > 0:
            for hosted_service_item in hosted_service_items:
                orphanedID = 'NOT FOUND'
                for id_map in item_mapping:
                    if hosted_service_item['id'] == id_map[replace_key]:
                        orphanedID = id_map[search_key]
                print format_item_info(hosted_service_item) + \
                                ' ***[OrphanedID: {}]'.format(orphanedID)
            
            print '\nNOTE: item mapping info written to file "{}" in directory: {}'.format(file_name, os.getcwd())
            json.dump(item_mapping, open(file_name,'w'))
        
        if len(o_hosted_service_items) > 0 and delete_option:
            print '\n{}'.format('-' * section_break_count)
            print '- You selected to delete orphaned hosted service items.'
            print '  Will delete the following items...'
            
            for o_hosted_service_item in o_hosted_service_items:
                print format_item_info(o_hosted_service_item)
        
            user_response = raw_input("\nDo you want to continue with the delete? Enter 'y' to continue: ")
    
            if validate_user_repsonse_yesno(user_response):
                for o_hosted_service_item in o_hosted_service_items:
                    print 'Deleting...'
                    print '\t{}'.format(format_item_info(o_hosted_service_item))
                    resp = portal.delete_item(o_hosted_service_item['id'])
                    print resp
                
    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:
        print '\nDone.'
        if total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)
예제 #3
0
def main():
    exit_err_code = 1
    starting_cwd = os.getcwd()
    
    # Print/get script arguments
    results = print_args()
    if not results:
        sys.exit(exit_err_code)
    portal_address, adminuser, password, id_mapping_file_path, search_query = results
    
    total_success = True
    title_break_count = 100
    section_break_count = 75
    
    print '=' * title_break_count
    print 'Update Portal GUIDs'
    print '=' * title_break_count

    if not os.path.exists(id_mapping_file_path):
         print '\nFile {} does not exist. Exiting.'.format(id_mapping_file_path)
         sys.exit(0)
            
    try:
        
        portal = Portal(portal_address, adminuser, password)
        
        print '\n{}'.format('-' * section_break_count)
        print '- Searching for portal items...\n'
        items_temp = portal.search(q=search_query, sort_field='owner')
        
        items = []
        for item in items_temp:
            if not item['owner'].startswith('esri_'):
                items.append(item)
                
        for item in items:
            print format_item_info(item)
        
        print '\nFound {} items.'.format(len(items))

        if len(items) > 0:
            user_response = raw_input("\nDo you want to continue with the update? Enter 'y' to continue: ")
            if validate_user_repsonse_yesno(user_response):
                
                # Open id mapping file
                file_dir = os.path.dirname(id_mapping_file_path)
                file_name = os.path.basename(id_mapping_file_path)
                if len(file_dir) == 0:
                    file_dir = os.getcwd()
                os.chdir(file_dir)
                id_mapping = json.load(open(file_name))
                
                print '\n{}'.format('-' * section_break_count)
                print '- Updating item and item data...\n'
                for item in items:
                    print format_item_info(item)
                    for id_map in id_mapping:
                        search = id_map.get(search_key)
                        replace = id_map.get(replace_key)
                        update_item_properties(portal, item, search, replace)
                        update_item_data(portal, item, search, replace)
                    
    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:
        os.chdir(starting_cwd)
        print '\nDone.'
        if total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)