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, item_ids = results

    total_success = True

    file_type = "serviceDefinition"

    print "=" * 150
    print "Publish Hosted Services"
    print "=" * 150

    try:

        # Create portal connection
        portal = Portal(portal_address, adminuser, password)

        # Get ids of the source items to publish
        valid_items = True
        if not item_ids:
            print "\n- Searching for valid source items to publish..."
            item_ids = get_source_item_ids(portal)
        else:
            print "\n- Validating specified source item guids..."
            for item_id in item_ids:
                if not is_valid_source_item(portal, item_id):
                    valid_items = False
                    print "ERROR: Item with GUID {} does not exist or is not a valid source type.".format(item_id)

        if not valid_items:
            print "ERROR: At least one specified GUID is invalid. Stopping script execution."
            sys.exit(exit_err_code)

        num_src_items = len(item_ids)
        startTime = datetime.now()

        print "\n- Will attempt to publish the following {} source item(s)...\n".format(num_src_items)
        for item_id in item_ids:
            print_item_info(portal.item(item_id))

        # TODO: may want to prompt user if they want to continue at this point so
        # they can review the items before trying to publish

        print "\n- Publish the source items..."

        i = 0
        grand_total_pub_jobs = 0  # Total number of jobs needed to publish all source items
        grand_total_pub_jobs_succeed = 0
        grand_total_skipped_transfer = 0

        for item_id in item_ids:
            i += 1

            item = portal.item(item_id)

            print "\n{}".format("-" * 100)
            print "{} out of {}\n".format(i, num_src_items)
            print_item_info(item)

            total_pub_jobs, total_pub_jobs_success, total_skipped_transfer = publish_source_item(
                portal, item_id, file_type
            )

            grand_total_pub_jobs += total_pub_jobs
            grand_total_pub_jobs_succeed += total_pub_jobs_success
            grand_total_skipped_transfer += total_skipped_transfer

        endTime = datetime.now()
        print
        print "-" * 100
        print "Summary"
        print "-" * 100
        print "Total number of..."
        print "Source items to publish: {}".format(num_src_items)
        print "Publishing jobs: {}".format(grand_total_pub_jobs)
        print "Publishing jobs completed: {}".format(grand_total_pub_jobs_succeed)
        print "Publishing jobs that failed: {}".format(grand_total_pub_jobs - grand_total_pub_jobs_succeed)
        print 'Services where info transfer was skipped because "original" service item did not exist: {}'.format(
            grand_total_skipped_transfer
        )

        print "\nStart time: {}".format(startTime)
        print "End time: {}".format(endTime)

        print "\nDone."
    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 total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)
예제 #2
0
def main():
    output_root = None

    # Get script parameters
    results = check_args()
    if not results:
        sys.exit(0)
    portal_address, adminuser, password, src_ids = results

    try:

        # Create portal connection object
        portal = Portal(portal_address, adminuser, password)

        # Check if any specified GUIDs do not exist
        invalid_guids = validate_guids(portal, src_ids)
        if len(invalid_guids) > 0:
            raise Exception(
                'ERROR: The following portal items do not exist: {}'.format(
                    invalid_guids))

        # Create list of users
        users = [org_user['username'] for org_user in portal.org_users()]
        target_users = [user for user in users if user not in exclude_users]

        # -----------------------------------------------------------------
        # Extract portal items
        # -----------------------------------------------------------------
        print '\n\n{}\nExtracting select portal items...\n{}\n'.format(
            sec_char * sec_len, sec_char * sec_len)

        # Create temporary extract folder in OS users' temp directory
        output_root = os.path.join(
            tempfile.gettempdir(),
            os.path.basename(sys.argv[0]).split('.')[0] + '_Extract')
        os.makedirs(output_root)

        print 'Extract folder: {}'.format(output_root)

        # Extract specified portal item(s)
        for src_id in src_ids:
            src_item = portal.item(src_id)
            os.chdir(output_root)
            print '- Extracting item {} "{}" ({}) user account {}...'.format(
                src_item['id'], src_item['title'], src_item['type'],
                src_item['owner'])
            PortalContentExtract.extract_item(portal, src_item['id'],
                                              src_item['owner'])

        # Create list of paths to individual extracted portal item folders
        src_item_paths = [
            os.path.join(output_root, src_id) for src_id in src_ids
        ]

        # -----------------------------------------------------------------
        # Publish extracted portal items for each user
        # -----------------------------------------------------------------
        print '\n\n{}\nPublish extracted items to each portal' \
                    'user account...\n{}'.format(sec_char * sec_len,
                                                 sec_char * sec_len)
        print 'NOTE: not publishing to the following users:'
        print exclude_users

        for target_user in target_users:
            print '\n\nUser Account: {}'.format(target_user)

            # Get info about user folders
            target_user_folders = portal.folders(target_user)

            for src_item_path in src_item_paths:

                # Get info about the source item
                os.chdir(src_item_path)
                src_item_json = json.load(open('item.json'))
                item_title = src_item_json['title']
                item_type = src_item_json['type']
                item_id = src_item_json['id']
                item_owner = src_item_json['owner']
                item_folder_id = src_item_json['ownerFolder']

                # Create folder in user account for item
                item_folder_name = get_folder_name(portal, item_owner,
                                                   item_folder_id)
                if item_folder_name:
                    if not has_folder(portal, target_user, item_folder_name):
                        print 'Creating target folder "{}" in account ' \
                                '{}...'.format(item_folder_name, target_user)
                        portal.create_folder(target_user, item_folder_name)

                # Check if user already owns item
                user_items = portal.search(
                    q='owner:{} AND type:{} AND title:{}'.format(
                        target_user, item_type, item_title))

                # Add item if item does not exist in user account or
                # update item if it already exists
                if len(user_items) == 0:
                    print '\n- Add item "{}" ({}) to user account {}...'.format(
                        item_title, item_type,
                        portal.logged_in_user()['username'])
                    item, orig_id = provision.load_item(portal, src_item_path)

                    print '- Reassign item to user account {}, ' \
                                'folder "{}"...'.format(target_user,
                                                        item_folder_name)
                    portal.reassign_item(item.get('id'), target_user,
                                         item_folder_name)
                else:
                    for user_item in user_items:
                        if user_item['id'] <> item_id:
                            print '\n- Update existing item {} ' \
                                    '"{}" ({}) user account {}...'.format(
                                            user_item['id'], user_item['title'],
                                            user_item['type'], user_item['owner'])
                            item, orig_id = provision.load_item(
                                portal, src_item_path, user_item['id'])

                            print '- Reassign item to user account {}, ' \
                                'folder "{}"...'.format(target_user,
                                                        item_folder_name)
                            portal.reassign_item(item.get('id'), target_user,
                                                 item_folder_name)

                        else:
                            print '*** No need to update item {}; ' \
                                    'user is owner of extracted item.'.format(
                                    user_item['id'])
        print '\n\nDone.'

    except:

        # 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:

        # Change directory to starting directory, otherwise the
        # delete will fail.
        os.chdir(start_dir)

        # Delete temp extracted folder/files
        if output_root:
            if os.path.exists(output_root):
                shutil.rmtree(output_root)
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, scene_parameter_folder, item_ids = results

    total_success = True

    print '=' * 150
    print 'Publish Hosted Scene Services'
    print '=' * 150

    try:

        # Create portal connection
        portal = Portal(portal_address, adminuser, password)

        # Get ids of the source items to publish
        valid_items = True
        if not item_ids:
            print '\n- Searching for valid source items to publish...'
            item_ids = get_source_item_ids(portal)
        else:
            print '\n- Validating specified source item guids...'
            for item_id in item_ids:
                if not is_valid_source_item(portal, item_id):
                    valid_items = False
                    print 'ERROR: Item with GUID {} does not exist, ' + \
                        'is not a hosted feature service, or does not ' + \
                        'have an associated scene service'.format(item_id)

        if not valid_items:
            print 'ERROR: At least one specified GUID is invalid. Stopping script execution.'
            sys.exit(exit_err_code)

        print '\n- Validating scene server parameter folder {}...'.format(
            scene_parameter_folder)
        is_valid, msg = validate_scene_parameter_folder(
            portal, scene_parameter_folder, item_ids)
        if not is_valid:
            print msg
            sys.exit(exit_err_code)

        num_src_items = len(item_ids)
        startTime = datetime.now()

        print '\n- Will attempt to publish the following {} source item(s)...\n'.format(
            num_src_items)
        for item_id in item_ids:
            print_item_info(portal.item(item_id))

        # TODO: may want to prompt user if they want to continue at this point so
        # they can review the items before trying to publish

        print '\n- Publish the source items...'

        i = 0
        grand_total_pub_jobs = 0  #Total number of jobs needed to publish all source items
        grand_total_pub_jobs_succeed = 0
        grand_total_skipped_transfer = 0

        os.chdir(scene_parameter_folder)

        for item_id in item_ids:
            i += 1

            item = portal.item(item_id)

            print '\n{}'.format('-' * 100)
            print '{} out of {}\n'.format(i, num_src_items)
            print_item_info(item)

            parameter_file = item['url'].split('/')[-2] + '_sceneserver.json'
            print 'Scene service publishing parameter file: {}'.format(
                parameter_file)

            publish_parameters = json.load(open(parameter_file))

            total_pub_jobs, total_pub_jobs_success, total_skipped_transfer = \
                publish_source_item(portal,
                                    item_id,
                                    file_type,
                                    publish_parameters=publish_parameters,
                                    output_type=output_type)

            grand_total_pub_jobs += total_pub_jobs
            grand_total_pub_jobs_succeed += total_pub_jobs_success
            grand_total_skipped_transfer += total_skipped_transfer

        endTime = datetime.now()
        print
        print '-' * 100
        print 'Summary'
        print '-' * 100
        print 'Total number of...'
        print 'Source items to publish: {}'.format(num_src_items)
        print 'Publishing jobs: {}'.format(grand_total_pub_jobs)
        print 'Publishing jobs completed: {}'.format(
            grand_total_pub_jobs_succeed)
        print 'Publishing jobs that failed: {}'.format(
            grand_total_pub_jobs - grand_total_pub_jobs_succeed)
        print 'Services where info transfer was skipped because "original" service item did not exist: {}'.format(
            grand_total_skipped_transfer)

        print '\nStart time: {}'.format(startTime)
        print 'End time: {}'.format(endTime)

        print '\nDone.'

    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 total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)
def main():
    
    totalSuccess = True
    
    # -------------------------------------------------
    # Check arguments
    # -------------------------------------------------
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    server, port, adminuser, password, use_ssl, output_file, owners = results
    
    if debug:
        print server, port, adminuser, password, use_ssl, output_file, owners
    
    if use_ssl:
        protocol = 'https'
    else:
        protocol = 'http'
            
    try:
        
        # Create connection to portal; need to get service item owner information
        portal = Portal('https://' + server + ':7443/arcgis', adminuser, password)
        
        # Verify if user specify owners exist
        invalid_owners = get_invalid_owners(portal, owners)
        
        if len(invalid_owners) > 0:
            print '\nThe following owners do not exist. Exiting script: \n{}'.format(', '.join(invalid_owners))
            totalSuccess = False
        
        else:
            
            # Get all services that exist on server
            all_services = getServiceList(server, port, adminuser, password)
            all_services = [service.replace('//', '/') for service in all_services]
            
            # Create collection of only hosted services
            print '\n{}'.format('=' * 80)
            print 'Extracting service definition information (editing, sync, export, tracking)'
            print 'for hosted feature services that meet filter criteria.'
            print '{}\n'.format('=' * 80)
            
            definitions = {}
            for service in all_services:
                folder, serviceNameType = parseService(service)
                if serviceNameType.endswith('.FeatureServer'):
                    portal_props = getServicePortalProps(server, port, adminuser, password, folder, serviceNameType)
                    if portal_props:
                        if portal_props['isHosted']:
                            portal_items = portal_props['portalItems']
                            for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item
                                do_write = True
                                
                                item_id = portal_item['itemID']
                                item = portal.item(item_id)
                                
                                if item is None:
                                    print '\tPortal item {} for service does not exist.'.format(item_id)
                                    item_owner = None
                                else:
                                    item_owner = item['owner']
                                    
                                if owners:
                                    if item_owner not in owners:
                                        do_write = False
                                
                                if do_write:
                                    print '{} ({})...'.format(service, item_owner)
                                    definition = getHostedServiceDefinition(server, port, adminuser, password, folder, serviceNameType)
                                    definitions[service] = {'owner': item_owner, 'updateDefinition': definition}
            
            # Write info to json file
            print '\nWriting "definition" information for {} services to file {}'.format(len(definitions), output_file)
            json.dump(definitions, open(output_file,'w'))
        
        print '\nDone.\n'
        
    except:
        totalSuccess = 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 totalSuccess:
            sys.exit(0)
        else:
            sys.exit(1)
예제 #5
0
def main():

    totalSuccess = True

    # -------------------------------------------------
    # Check arguments
    # -------------------------------------------------
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    server, port, adminuser, password, use_ssl, output_file, owners = results

    if debug:
        print server, port, adminuser, password, use_ssl, output_file, owners

    if use_ssl:
        protocol = 'https'
    else:
        protocol = 'http'

    try:

        # Create connection to portal; need to get service item owner information
        portal = Portal('https://' + server + ':7443/arcgis', adminuser,
                        password)

        # Verify if user specify owners exist
        invalid_owners = get_invalid_owners(portal, owners)

        if len(invalid_owners) > 0:
            print '\nThe following owners do not exist. Exiting script: \n{}'.format(
                ', '.join(invalid_owners))
            totalSuccess = False

        else:

            # Get all services that exist on server
            all_services = getServiceList(server, port, adminuser, password)
            all_services = [
                service.replace('//', '/') for service in all_services
            ]

            # Create collection of only hosted services
            print '\n{}'.format('=' * 80)
            print 'Extracting service definition information (editing, sync, export, tracking)'
            print 'for hosted feature services that meet filter criteria.'
            print '{}\n'.format('=' * 80)

            definitions = {}
            for service in all_services:
                folder, serviceNameType = parseService(service)
                if serviceNameType.endswith('.FeatureServer'):
                    portal_props = getServicePortalProps(
                        server, port, adminuser, password, folder,
                        serviceNameType)
                    if portal_props:
                        if portal_props['isHosted']:
                            portal_items = portal_props['portalItems']
                            for portal_item in portal_items:  #NOTE: for hosted services there should only be one portal item
                                do_write = True

                                item_id = portal_item['itemID']
                                item = portal.item(item_id)

                                if item is None:
                                    print '\tPortal item {} for service does not exist.'.format(
                                        item_id)
                                    item_owner = None
                                else:
                                    item_owner = item['owner']

                                if owners:
                                    if item_owner not in owners:
                                        do_write = False

                                if do_write:
                                    print '{} ({})...'.format(
                                        service, item_owner)
                                    definition = getHostedServiceDefinition(
                                        server, port, adminuser, password,
                                        folder, serviceNameType)
                                    definitions[service] = {
                                        'owner': item_owner,
                                        'updateDefinition': definition
                                    }

            # Write info to json file
            print '\nWriting "definition" information for {} services to file {}'.format(
                len(definitions), output_file)
            json.dump(definitions, open(output_file, 'w'))

        print '\nDone.\n'

    except:
        totalSuccess = 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 totalSuccess:
            sys.exit(0)
        else:
            sys.exit(1)
def main():
    output_root = None
    
    # Get script parameters
    results = check_args()
    if not results:
        sys.exit(0)
    portal_address, adminuser, password, src_ids = results
        
    try:        
        
        # Create portal connection object
        portal = Portal(portal_address, adminuser, password)
        
        # Check if any specified GUIDs do not exist
        invalid_guids = validate_guids(portal, src_ids)
        if len(invalid_guids) > 0:
            raise Exception(
                'ERROR: The following portal items do not exist: {}'.format(
                                                            invalid_guids))
        
        # Create list of users
        users = [org_user['username'] for org_user in portal.org_users()]
        target_users = [user for user in users if user not in exclude_users]
        
        # -----------------------------------------------------------------
        # Extract portal items
        # -----------------------------------------------------------------
        print '\n\n{}\nExtracting select portal items...\n{}\n'.format(
                                    sec_char * sec_len, sec_char * sec_len)
        
        # Create temporary extract folder in OS users' temp directory
        output_root = os.path.join(tempfile.gettempdir(), 
                                    os.path.basename(
                                    sys.argv[0]).split('.')[0] + '_Extract' )
        os.makedirs(output_root)
        
        print 'Extract folder: {}'.format(output_root)
        
        # Extract specified portal item(s)
        for src_id in src_ids:
            src_item = portal.item(src_id)
            os.chdir(output_root)
            print '- Extracting item {} "{}" ({}) user account {}...'.format(
                                    src_item['id'], src_item['title'],
                                    src_item['type'], src_item['owner'])
            PortalContentExtract.extract_item(
                                    portal, src_item['id'], 
                                    src_item['owner'])
        
        # Create list of paths to individual extracted portal item folders
        src_item_paths = [os.path.join(output_root, 
                                        src_id) for src_id in src_ids]
        
        # -----------------------------------------------------------------
        # Publish extracted portal items for each user
        # -----------------------------------------------------------------
        print '\n\n{}\nPublish extracted items to each portal' \
                    'user account...\n{}'.format(sec_char * sec_len,
                                                 sec_char * sec_len)
        print 'NOTE: not publishing to the following users:'
        print exclude_users
        
        for target_user in target_users:
            print '\n\nUser Account: {}'.format(target_user)
            
            # Get info about user folders
            target_user_folders = portal.folders(target_user)
            
            for src_item_path in src_item_paths:
                
                # Get info about the source item
                os.chdir(src_item_path)
                src_item_json = json.load(open('item.json'))
                item_title = src_item_json['title']
                item_type = src_item_json['type']
                item_id = src_item_json['id']
                item_owner = src_item_json['owner']
                item_folder_id = src_item_json['ownerFolder']
                
                # Create folder in user account for item
                item_folder_name = get_folder_name(portal, item_owner,
                                                   item_folder_id)
                if item_folder_name:
                    if not has_folder(portal, target_user, item_folder_name):
                        print 'Creating target folder "{}" in account ' \
                                '{}...'.format(item_folder_name, target_user)
                        portal.create_folder(target_user, item_folder_name)
                
                # Check if user already owns item
                user_items = portal.search(
                                q='owner:{} AND type:{} AND title:{}'.format(
                                target_user, item_type, item_title))
                
                # Add item if item does not exist in user account or 
                # update item if it already exists
                if len(user_items) == 0:
                    print '\n- Add item "{}" ({}) to user account {}...'.format(
                                        item_title, item_type,
                                        portal.logged_in_user()['username'])
                    item, orig_id = provision.load_item(portal, src_item_path)
                    
                    print '- Reassign item to user account {}, ' \
                                'folder "{}"...'.format(target_user,
                                                        item_folder_name)
                    portal.reassign_item(item.get('id'), target_user, item_folder_name)
                else:
                    for user_item in user_items:
                        if user_item['id'] <> item_id:
                            print '\n- Update existing item {} ' \
                                    '"{}" ({}) user account {}...'.format(
                                            user_item['id'], user_item['title'],
                                            user_item['type'], user_item['owner'])
                            item, orig_id = provision.load_item(
                                            portal, src_item_path, 
                                            user_item['id'])
                            
                            print '- Reassign item to user account {}, ' \
                                'folder "{}"...'.format(target_user,
                                                        item_folder_name)
                            portal.reassign_item(item.get('id'), target_user, item_folder_name)
                            
                        else:
                            print '*** No need to update item {}; ' \
                                    'user is owner of extracted item.'.format(
                                    user_item['id'])
        print '\n\nDone.'
        
    except:
        
        # 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:
        
        # Change directory to starting directory, otherwise the
        # delete will fail.
        os.chdir(start_dir)
        
        # Delete temp extracted folder/files
        if output_root:
            if os.path.exists(output_root):
                shutil.rmtree(output_root)
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, scene_parameter_folder, item_ids = results
    
    total_success = True
    
    print '=' * 150
    print 'Publish Hosted Scene Services'
    print '=' * 150
    
    try:
        
        # Create portal connection
        portal = Portal(portal_address, adminuser, password)
        
        # Get ids of the source items to publish
        valid_items = True
        if not item_ids:
            print '\n- Searching for valid source items to publish...'
            item_ids = get_source_item_ids(portal)
        else:
            print '\n- Validating specified source item guids...'
            for item_id in item_ids:
                if not is_valid_source_item(portal, item_id):
                    valid_items = False
                    print 'ERROR: Item with GUID {} does not exist, ' + \
                        'is not a hosted feature service, or does not ' + \
                        'have an associated scene service'.format(item_id)
        
        if not valid_items:
            print 'ERROR: At least one specified GUID is invalid. Stopping script execution.'
            sys.exit(exit_err_code)
    
        print '\n- Validating scene server parameter folder {}...'.format(scene_parameter_folder)
        is_valid, msg = validate_scene_parameter_folder(portal, scene_parameter_folder, item_ids)
        if not is_valid:
            print msg
            sys.exit(exit_err_code)
        
        num_src_items = len(item_ids)
        startTime = datetime.now()
        
        print '\n- Will attempt to publish the following {} source item(s)...\n'.format(num_src_items)
        for item_id in item_ids:
            print_item_info(portal.item(item_id))
        
        # TODO: may want to prompt user if they want to continue at this point so
        # they can review the items before trying to publish
        
        print '\n- Publish the source items...'
        
        i = 0
        grand_total_pub_jobs = 0 #Total number of jobs needed to publish all source items
        grand_total_pub_jobs_succeed = 0
        grand_total_skipped_transfer = 0
        
        os.chdir(scene_parameter_folder)
        
        for item_id in item_ids:
            i += 1
            
            item = portal.item(item_id)
            
            print '\n{}'.format('-' * 100)
            print '{} out of {}\n'.format(i, num_src_items)
            print_item_info(item)
            
            parameter_file = item['url'].split('/')[-2] + '_sceneserver.json'
            print 'Scene service publishing parameter file: {}'.format(parameter_file)
            
            publish_parameters = json.load(open(parameter_file))

            total_pub_jobs, total_pub_jobs_success, total_skipped_transfer = \
                publish_source_item(portal,
                                    item_id,
                                    file_type,
                                    publish_parameters=publish_parameters,
                                    output_type=output_type)
            
            grand_total_pub_jobs += total_pub_jobs
            grand_total_pub_jobs_succeed += total_pub_jobs_success
            grand_total_skipped_transfer += total_skipped_transfer

        endTime = datetime.now()
        print
        print '-' * 100
        print 'Summary'
        print '-' * 100
        print 'Total number of...'
        print 'Source items to publish: {}'.format(num_src_items)
        print 'Publishing jobs: {}'.format(grand_total_pub_jobs)
        print 'Publishing jobs completed: {}'.format(grand_total_pub_jobs_succeed)
        print 'Publishing jobs that failed: {}'.format(grand_total_pub_jobs - grand_total_pub_jobs_succeed)
        print 'Services where info transfer was skipped because "original" service item did not exist: {}'.format(grand_total_skipped_transfer)
        
        print '\nStart time: {}'.format(startTime)
        print 'End time: {}'.format(endTime)
    
        print '\nDone.'
        
    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 total_success:
            sys.exit(0)
        else:
            sys.exit(exit_err_code)
def main():
    
    totalSuccess = True
    
    # -------------------------------------------------
    # Check arguments
    # -------------------------------------------------
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    server, port, adminuser, password, use_ssl, output_folder, owners = results
    
    if debug:
        print server, port, adminuser, password, use_ssl, output_folder, owners
    
    if use_ssl:
        protocol = 'https'
    else:
        protocol = 'http'
            
    try:
        
        # Create connection to portal; need to get service item owner information
        portal = Portal('https://' + server + ':7443/arcgis', adminuser, password)
        
        # Verify if user specify owners exist
        invalid_owners = get_invalid_owners(portal, owners)
        
        if len(invalid_owners) > 0:
            print '\nThe following owners do not exist. Exiting script: \n{}'.format(', '.join(invalid_owners))
            totalSuccess = False
        
        else:
            
            # Get all services that exist on server
            all_services = getServiceList(server, port, adminuser, password)
            all_services = [service.replace('//', '/') for service in all_services]
            
            # Create collection of only hosted services
            print '\n{}'.format('=' * 80)
            print 'Extracting scene service definition information for hosted scene services'
            print 'that meet criteria.'
            print '{}\n'.format('=' * 80)
            
            total_scene_services = 0
            total_files_created = 0
            
            for service in all_services:
                folder, serviceNameType = parseService(service)

                if serviceNameType.endswith('.SceneServer'):
                    total_scene_services += 1
                    print '-' * 80
                    print 'Service: {}'.format(service)
                    
                    portal_props = getServicePortalProps(server, port, adminuser, password, folder, serviceNameType)
                    
                    if portal_props:
                        if portal_props['isHosted']:
                            portal_items = portal_props['portalItems']
                            for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item
                                do_write = True
                                
                                item_id = portal_item['itemID']
                                item = portal.item(item_id)
                                
                                if item is None:
                                    print '\tPortal item {} for service does not exist.'.format(item_id)
                                    item_owner = None
                                else:
                                    item_owner = item['owner']
                                    item_title = item['title']
                                    
                                print 'Item id: {}, Owner: {}, Title: {}'.format(item_id, item_owner, item_title)
                                
                                if owners:
                                    if item_owner not in owners:
                                        do_write = False
                                
                                if do_write:
                                    
                                    service_json = getServiceJSON(server, port, adminuser, password, folder, serviceNameType)
                                    
                                    definition = {}
                                    definition['name'] = serviceNameType.split('.')[0]
                                    definition['serviceDescription'] = service_json
                                    
                                    output_file = os.path.join(output_folder, serviceNameType.replace('.SceneServer', '') + '_sceneserver.json')
                                    
                                    # Write info to json file
                                    print '\nWriting "definition" to file {}'.format(output_file)
                                    json.dump(definition, open(output_file,'w'))
                                    total_files_created += 1
                                
                                else:
                                    print 'Does not meet criteria. Will not write "definition" file.'
        print '-' * 80
        print '\nTotal number of scene services: {}'.format(total_scene_services)
        print 'Total number of scene service definition files created: {}'.format(total_files_created)
        print '\nDone.\n'
        
    except:
        totalSuccess = 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 totalSuccess:
            sys.exit(0)
        else:
            sys.exit(1)
def main():

    totalSuccess = True

    # -------------------------------------------------
    # Check arguments
    # -------------------------------------------------
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    server, port, adminuser, password, use_ssl, output_folder, owners = results

    if debug:
        print server, port, adminuser, password, use_ssl, output_folder, owners

    if use_ssl:
        protocol = 'https'
    else:
        protocol = 'http'

    try:

        # Create connection to portal; need to get service item owner information
        portal = Portal('https://' + server + ':7443/arcgis', adminuser,
                        password)

        # Verify if user specify owners exist
        invalid_owners = get_invalid_owners(portal, owners)

        if len(invalid_owners) > 0:
            print '\nThe following owners do not exist. Exiting script: \n{}'.format(
                ', '.join(invalid_owners))
            totalSuccess = False

        else:

            # Get all services that exist on server
            all_services = getServiceList(server, port, adminuser, password)
            all_services = [
                service.replace('//', '/') for service in all_services
            ]

            # Create collection of only hosted services
            print '\n{}'.format('=' * 80)
            print 'Extracting scene service definition information for hosted scene services'
            print 'that meet criteria.'
            print '{}\n'.format('=' * 80)

            total_scene_services = 0
            total_files_created = 0

            for service in all_services:
                folder, serviceNameType = parseService(service)

                if serviceNameType.endswith('.SceneServer'):
                    total_scene_services += 1
                    print '-' * 80
                    print 'Service: {}'.format(service)

                    portal_props = getServicePortalProps(
                        server, port, adminuser, password, folder,
                        serviceNameType)

                    if portal_props:
                        if portal_props['isHosted']:
                            portal_items = portal_props['portalItems']
                            for portal_item in portal_items:  #NOTE: for hosted services there should only be one portal item
                                do_write = True

                                item_id = portal_item['itemID']
                                item = portal.item(item_id)

                                if item is None:
                                    print '\tPortal item {} for service does not exist.'.format(
                                        item_id)
                                    item_owner = None
                                else:
                                    item_owner = item['owner']
                                    item_title = item['title']

                                print 'Item id: {}, Owner: {}, Title: {}'.format(
                                    item_id, item_owner, item_title)

                                if owners:
                                    if item_owner not in owners:
                                        do_write = False

                                if do_write:

                                    service_json = getServiceJSON(
                                        server, port, adminuser, password,
                                        folder, serviceNameType)

                                    definition = {}
                                    definition['name'] = serviceNameType.split(
                                        '.')[0]
                                    definition[
                                        'serviceDescription'] = service_json

                                    output_file = os.path.join(
                                        output_folder,
                                        serviceNameType.replace(
                                            '.SceneServer', '') +
                                        '_sceneserver.json')

                                    # Write info to json file
                                    print '\nWriting "definition" to file {}'.format(
                                        output_file)
                                    json.dump(definition,
                                              open(output_file, 'w'))
                                    total_files_created += 1

                                else:
                                    print 'Does not meet criteria. Will not write "definition" file.'
        print '-' * 80
        print '\nTotal number of scene services: {}'.format(
            total_scene_services)
        print 'Total number of scene service definition files created: {}'.format(
            total_files_created)
        print '\nDone.\n'

    except:
        totalSuccess = 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 totalSuccess:
            sys.exit(0)
        else:
            sys.exit(1)