Exemplo n.º 1
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)
Exemplo n.º 2
0
def publish_user_items(portal, username, usercontentpath, old_hostname,
                       new_hostname, new_port, specified_groups,
                       id_mapping_file, grps_on_disk):
    ''' Publish all items for current user '''
    # Returns list of dictionaries of new items as well as a list of the
    # original item ids
    newitems, old_source_ids = [], []
    existing_portal_ids = []

    print "\n" + sectionBreak
    print "Publishing items for user '" + username + "'...\n"

    # Load 'id mapping' file if specified. Since this supports overwrite
    # capability, let's also create a list of all current item ids to verify that item
    # actually exists.
    id_mapping = None
    if id_mapping_file:
        filefolder = os.path.dirname(id_mapping_file)
        filename = os.path.basename(id_mapping_file)
        os.chdir(filefolder)
        id_mapping = json.load(open(filename))

        # Create list of existing portal items
        existing_portal_items = portal.search()
        for existing_portal_item in existing_portal_items:
            existing_portal_ids.append(existing_portal_item['id'])

    item_dirs = os.listdir(os.path.join(usercontentpath, "items"))

    n = 1
    for item_dir in item_dirs:
        overwrite_id = None
        do_load_item = False
        foldername = None

        print "\n\tPublishing item {} ({}/{})...".format(
            item_dir, n, len(item_dirs))

        if id_mapping:
            overwrite_id = id_mapping.get(item_dir)['id']
            if overwrite_id:
                print "\t   -Item referenced in id mapping file. Checking if item exists in portal..."
                if overwrite_id in existing_portal_ids:
                    print "\t   -Item exists in portal. Will update item."
                else:
                    overwrite_id = None
                    print "*** WARNING: item referenced in id mapping file does NOT exist in portal. Will add new item instead of updating."

        # Determine if item should be loaded base on specified group parameters
        if not specified_groups:
            do_load_item = True
        else:
            os.chdir(os.path.join(usercontentpath, "items", item_dir))
            sharing_info = json.load(open('sharing.json'))
            item_groups = sharing_info.get('groups')
            for item_group in item_groups:
                grp_on_disk = grps_on_disk.get(item_group)
                if grp_on_disk:
                    for specified_group in specified_groups:
                        if specified_group == grp_on_disk:
                            do_load_item = True

            if not do_load_item:
                print "\t   -Skipping item. Item groups do not match user specified group criteria."

        # Add/Update item
        if do_load_item:
            item, old_source_id = load_item(
                portal, os.path.join(usercontentpath, "items", item_dir),
                overwrite_id)
            newitems.append(item)
            old_source_ids.append(old_source_id)

            # Reassign item to target owner and folder
            if os.path.exists(os.path.join(usercontentpath, "folders.json")):
                os.chdir(usercontentpath)
                foldersinfo = json.load(open('folders.json'))
                foldername = publish_get_folder_name_for_item(
                    item, foldersinfo)

            portal.reassign_item(item['id'], username, foldername)

        n = n + 1

    return newitems, old_source_ids
Exemplo n.º 3
0
def publish_user_items(portal, username, usercontentpath, old_hostname, new_hostname, new_port, specified_groups, id_mapping_file, grps_on_disk):
    ''' Publish all items for current user '''
    # Returns list of dictionaries of new items as well as a list of the
    # original item ids
    newitems, old_source_ids = [], []
    existing_portal_ids = []
    
    print "\n" + sectionBreak
    print "Publishing items for user '" + username + "'...\n"
    
    # Load 'id mapping' file if specified. Since this supports overwrite
    # capability, let's also create a list of all current item ids to verify that item
    # actually exists.
    id_mapping = None
    if id_mapping_file:
        filefolder = os.path.dirname(id_mapping_file)
        filename = os.path.basename(id_mapping_file)
        os.chdir(filefolder)
        id_mapping = json.load(open(filename))
        
        # Create list of existing portal items
        existing_portal_items = portal.search()
        for existing_portal_item in existing_portal_items:
            existing_portal_ids.append(existing_portal_item['id'])
        

    item_dirs = os.listdir(os.path.join(usercontentpath,"items"))
    
    n = 1
    for item_dir in item_dirs:
        overwrite_id = None
        do_load_item = False
        foldername = None
        
        print "\n\tPublishing item {} ({}/{})...".format(item_dir, n, len(item_dirs))
        
        if id_mapping:
            overwrite_id = id_mapping.get(item_dir)['id']
            if overwrite_id:
                print "\t   -Item referenced in id mapping file. Checking if item exists in portal..."
                if overwrite_id in existing_portal_ids:
                    print "\t   -Item exists in portal. Will update item."
                else:
                    overwrite_id = None
                    print "*** WARNING: item referenced in id mapping file does NOT exist in portal. Will add new item instead of updating."
        
        
        # Determine if item should be loaded base on specified group parameters
        if not specified_groups:
            do_load_item = True
        else:
            os.chdir(os.path.join(usercontentpath,"items", item_dir))
            sharing_info = json.load(open('sharing.json'))
            item_groups = sharing_info.get('groups')
            for item_group in item_groups:
                grp_on_disk = grps_on_disk.get(item_group)
                if grp_on_disk:
                    for specified_group in specified_groups:
                        if specified_group == grp_on_disk:
                            do_load_item = True

            if not do_load_item:
                print "\t   -Skipping item. Item groups do not match user specified group criteria."       
        
        
        # Add/Update item
        if do_load_item:
            
            try:
                item, old_source_id = load_item(portal, os.path.join(usercontentpath,"items", item_dir), overwrite_id)
                newitems.append(item)
                old_source_ids.append(old_source_id)         
    
                # Reassign item to target owner and folder
                if os.path.exists(os.path.join(usercontentpath, "folders.json")):
                    os.chdir(usercontentpath)
                    foldersinfo = json.load(open('folders.json'))
                    foldername = publish_get_folder_name_for_item(item, foldersinfo)
                
                portal.reassign_item(item['id'], username, foldername)
                
            except Exception as err:
                print 'ERROR: Exception on adding/updating item: {}'.format(item_dir)
                
        n = n + 1

    return newitems, old_source_ids
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)