<s> import os , sys <EOL> supportFilesPath = os . path . join ( <EOL> os . path . dirname ( <EOL> os . path . dirname ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) ) ) , "<STR_LIT>" ) <EOL> sys . path . append ( supportFilesPath ) <EOL> import portalpy <EOL> import json <EOL> import urlparse <EOL> import types <EOL> import shutil <EOL> from datetime import datetime , timedelta <EOL> from portalpy import Portal , parse_hostname , portal_time , WebMap , normalize_url , unpack , TEXT_BASED_ITEM_TYPES <EOL> from portalpy . provision import load_items , load_item <EOL> from Utilities import findInFile , editFiles <EOL> import logging <EOL> import copy <EOL> from Utilities import findFilePath <EOL> logging . basicConfig ( ) <EOL> source_hostname = "<STR_LIT>" <EOL> new_hostname = "<STR_LIT>" <EOL> new_port = None <EOL> hostname_map = { } <EOL> DEBUG = False <EOL> titleBreak = "<STR_LIT>" <EOL> sectionBreak = "<STR_LIT>" <EOL> sectionBreak2 = "<STR_LIT>" <EOL> portal_processing = "<STR_LIT:POST>" <EOL> portalLogFolder = "<STR_LIT>" <EOL> users = { } <EOL> originGroupToDestGroups = { } <EOL> destGroupID_GroupName = { } <EOL> origIDToNewID = { } <EOL> def main ( ) : <EOL> scriptName = sys . argv [ <NUM_LIT:0> ] <EOL> specified_users = None <EOL> specified_groups = None <EOL> id_mapping_file = None <EOL> if len ( sys . argv ) < <NUM_LIT:5> : <EOL> print '<STR_LIT:\n>' + scriptName + '<STR_LIT>' + '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' + source_hostname + '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> target_portal_address = sys . argv [ <NUM_LIT:1> ] <EOL> adminuser = sys . argv [ <NUM_LIT:2> ] <EOL> adminpassword = sys . argv [ <NUM_LIT:3> ] <EOL> contentpath = sys . argv [ <NUM_LIT:4> ] <EOL> if len ( sys . argv ) > <NUM_LIT:5> : <EOL> specified_users = sys . argv [ <NUM_LIT:5> ] <EOL> if len ( sys . argv ) > <NUM_LIT:6> : <EOL> specified_groups = sys . argv [ <NUM_LIT:6> ] <EOL> specified_groups = [ group . strip ( ) for group in specified_groups . split ( '<STR_LIT:|>' ) ] <EOL> if len ( sys . argv ) > <NUM_LIT:7> : <EOL> id_mapping_file = sys . argv [ <NUM_LIT:7> ] <EOL> if len ( sys . argv ) > <NUM_LIT:8> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" + str ( target_portal_address ) <EOL> print "<STR_LIT>" + str ( adminuser ) <EOL> print "<STR_LIT>" + str ( adminpassword ) <EOL> print "<STR_LIT>" + str ( contentpath ) <EOL> if specified_users : <EOL> print "<STR_LIT>" + str ( specified_users ) <EOL> if specified_groups : <EOL> print "<STR_LIT>" + str ( specified_groups ) <EOL> if id_mapping_file : <EOL> print "<STR_LIT>" + str ( id_mapping_file ) <EOL> if not val_arg_content_path ( contentpath ) : <EOL> print "<STR_LIT>" + contentpath + "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> users = val_arg_users ( specified_users , contentpath ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" + str ( users . keys ( ) ) <EOL> if id_mapping_file : <EOL> if not os . path . exists ( id_mapping_file ) : <EOL> print "<STR_LIT>" + id_mapping_file + "<STR_LIT>" <EOL> new_hostname = parse_hostname ( target_portal_address ) <EOL> hostname_map = { source_hostname : new_hostname } <EOL> publish_portal ( target_portal_address , contentpath , adminuser , adminpassword , users , hostname_map , specified_groups , id_mapping_file ) <EOL> os . chdir ( contentpath ) <EOL> def print_script_header ( portal , portal_processing , users , specified_groups ) : <EOL> print titleBreak <EOL> print "<STR_LIT>" + portal_processing <EOL> print titleBreak <EOL> print "<STR_LIT>" + portal_processing <EOL> print "<STR_LIT>" + portal . url <EOL> print "<STR_LIT>" + portal . logged_in_user ( ) [ '<STR_LIT:username>' ] + "<STR_LIT>" + portal . logged_in_user ( ) [ '<STR_LIT>' ] + "<STR_LIT:)>" <EOL> print "<STR_LIT>" + str ( users . keys ( ) ) <EOL> print "<STR_LIT>" <EOL> print str ( specified_groups ) <EOL> def publish_portal ( portaladdress , contentpath , adminuser , adminpassword , users , hostname_map , specified_groups , id_mapping_file ) : <EOL> os . chdir ( contentpath ) <EOL> portal_properties = json . load ( open ( "<STR_LIT>" ) ) <EOL> portaladmin = Portal ( portaladdress , adminuser , adminpassword ) <EOL> print_script_header ( portaladmin , portal_processing , users , specified_groups ) <EOL> portalLogPath = os . path . join ( contentpath , portalLogFolder , get_servername_from_url ( portaladmin . url ) ) <EOL> makeFolder ( portalLogPath ) <EOL> grps_on_disk = get_groups_on_disk ( contentpath ) <EOL> if specified_groups : <EOL> invalid_specified_groups = [ ] <EOL> for specified_group in specified_groups : <EOL> found = False <EOL> for grp in grps_on_disk . itervalues ( ) : <EOL> if specified_group == grp : <EOL> found = True <EOL> if not found : <EOL> invalid_specified_groups . append ( specified_group ) <EOL> if len ( invalid_specified_groups ) > <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> print invalid_specified_groups <EOL> return <EOL> print titleBreak <EOL> print '<STR_LIT>' <EOL> if portaladmin . properties ( ) [ '<STR_LIT:id>' ] == '<STR_LIT>' : <EOL> for username , userinfo in users . iteritems ( ) : <EOL> create_user ( portaladmin , contentpath , userinfo ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT:\n>' + titleBreak <EOL> print '<STR_LIT>' <EOL> for username , userinfo in users . iteritems ( ) : <EOL> print '<STR_LIT>' + userinfo [ '<STR_LIT>' ] <EOL> create_user_folders ( portaladmin , contentpath , userinfo ) <EOL> print "<STR_LIT:\n>" + titleBreak <EOL> print "<STR_LIT>" <EOL> oldGrpID_newGrpID = { } <EOL> for username , userinfo in users . iteritems ( ) : <EOL> newGroups = publish_user_groups ( portaladmin , contentpath , userinfo , users ) <EOL> for key , val in newGroups . iteritems ( ) : <EOL> oldGrpID_newGrpID [ key ] = { '<STR_LIT:id>' : val } <EOL> print "<STR_LIT:\n>" + titleBreak <EOL> print "<STR_LIT>" <EOL> print titleBreak <EOL> for username , userinfo in users . iteritems ( ) : <EOL> username = userinfo [ '<STR_LIT>' ] <EOL> password = userinfo [ '<STR_LIT>' ] <EOL> userfoldername = userinfo [ '<STR_LIT:username>' ] <EOL> usercontentpath = os . path . join ( contentpath , userfoldername ) <EOL> newItems , origItemSourceIDs = publish_user_items ( portaladmin , username , usercontentpath , source_hostname , <EOL> new_hostname , new_port , specified_groups , id_mapping_file , grps_on_disk ) <EOL> dump_newItem_info ( newItems , origItemSourceIDs , os . path . join ( portalLogPath , username ) ) <EOL> userItemsPath = os . path . join ( contentpath , userfoldername , "<STR_LIT>" ) <EOL> share_items ( portaladmin , userItemsPath , newItems , origItemSourceIDs , originGroupToDestGroups ) <EOL> os . chdir ( portalLogPath ) <EOL> json . dump ( origIDToNewID , open ( '<STR_LIT>' , '<STR_LIT:w>' ) ) <EOL> json . dump ( oldGrpID_newGrpID , open ( '<STR_LIT>' , '<STR_LIT:w>' ) ) <EOL> print "<STR_LIT:\n>" + titleBreak <EOL> print "<STR_LIT>" <EOL> print titleBreak + "<STR_LIT:\n>" <EOL> origIDToNewID . update ( oldGrpID_newGrpID ) <EOL> update_post_publish ( portaladmin , hostname_map , origIDToNewID ) <EOL> print "<STR_LIT>" <EOL> def get_groups_on_disk ( contentpath ) : <EOL> groups_on_disk_info = { } <EOL> group_json_files = findFilePath ( contentpath , '<STR_LIT>' , returnFirst = False ) <EOL> for group_json_file in group_json_files : <EOL> os . chdir ( os . path . dirname ( group_json_file ) ) <EOL> group_json = json . load ( open ( '<STR_LIT>' ) ) <EOL> groups_on_disk_info [ group_json [ '<STR_LIT:id>' ] ] = '<STR_LIT>' . format ( group_json [ '<STR_LIT>' ] , group_json [ '<STR_LIT:title>' ] ) <EOL> return groups_on_disk_info <EOL> def dump_newItem_info ( newItems , origItemSourceIDs , userLogPath ) : <EOL> oldID_newItem_JsonFile = "<STR_LIT>" <EOL> oldID_newItem_JsonFilePath = os . path . join ( userLogPath , oldID_newItem_JsonFile ) <EOL> oldID_newID_JsonFile = "<STR_LIT>" <EOL> oldID_newID_JsonFilePath = os . path . join ( userLogPath , oldID_newID_JsonFile ) <EOL> makeFolder ( userLogPath ) <EOL> os . chdir ( userLogPath ) <EOL> oldID_newItem = [ ] <EOL> oldID_newID = { } <EOL> for x in range ( <NUM_LIT:0> , len ( origItemSourceIDs ) ) : <EOL> d = { } <EOL> d_new_info = { } <EOL> newID = newItems [ x ] [ "<STR_LIT:id>" ] <EOL> oldID = origItemSourceIDs [ x ] <EOL> itemType = newItems [ x ] [ "<STR_LIT:type>" ] <EOL> itemOwner = newItems [ x ] [ "<STR_LIT>" ] <EOL> d [ "<STR_LIT>" ] = oldID <EOL> d [ "<STR_LIT>" ] = newItems [ x ] <EOL> oldID_newItem . append ( d ) <EOL> d_new_info = { "<STR_LIT:id>" : newID , "<STR_LIT:type>" : itemType , "<STR_LIT>" : itemOwner } <EOL> oldID_newID [ oldID ] = dict ( d_new_info ) <EOL> origIDToNewID [ oldID ] = dict ( d_new_info ) <EOL> json . dump ( oldID_newItem , open ( oldID_newItem_JsonFile , '<STR_LIT:w>' ) ) <EOL> json . dump ( oldID_newID , open ( oldID_newID_JsonFile , '<STR_LIT:w>' ) ) <EOL> return oldID_newID_JsonFilePath , oldID_newItem_JsonFilePath <EOL> def share_items ( portal , userItemsPath , newItems , origItemSourceIDs , originGroupToDestGroups ) : <EOL> print "<STR_LIT>" <EOL> for x in range ( <NUM_LIT:0> , len ( origItemSourceIDs ) ) : <EOL> newItem = newItems [ x ] <EOL> newID = newItem [ "<STR_LIT:id>" ] <EOL> origID = origItemSourceIDs [ x ] <EOL> itemIdfolder = os . path . join ( userItemsPath , str ( origID ) ) <EOL> if not os . path . exists ( itemIdfolder ) : <EOL> continue <EOL> os . chdir ( itemIdfolder ) <EOL> sharing = json . load ( open ( '<STR_LIT>' ) ) <EOL> new_group_ids = [ ] <EOL> new_group_names = [ ] <EOL> everyone = False <EOL> org = False <EOL> if sharing : <EOL> old_group_ids = sharing [ '<STR_LIT>' ] <EOL> for g in old_group_ids : <EOL> if g in originGroupToDestGroups . keys ( ) : <EOL> new_group_id = originGroupToDestGroups [ g ] <EOL> new_group_ids . append ( new_group_id ) <EOL> new_group_names = destGroupID_GroupName [ new_group_id ] <EOL> if len ( new_group_ids ) == <NUM_LIT:0> : <EOL> new_group_ids = None <EOL> print "<STR_LIT>" + str ( newItem . get ( "<STR_LIT:title>" ) ) + "<STR_LIT:[>" + str ( newItem . get ( "<STR_LIT:type>" ) ) + "<STR_LIT>" + sharing [ '<STR_LIT>' ] + "<STR_LIT:U+002CU+0020>" + str ( new_group_names ) + "<STR_LIT>" <EOL> if sharing [ '<STR_LIT>' ] . lower ( ) == "<STR_LIT>" : <EOL> everyone = True <EOL> org = True <EOL> elif sharing [ '<STR_LIT>' ] . lower ( ) == "<STR_LIT>" : <EOL> everyone = False <EOL> org = True <EOL> resp = portal . share_item ( newID , new_group_ids , org , everyone ) <EOL> else : <EOL> pass <EOL> del sharing <EOL> def has_user ( portaladmin , username ) : <EOL> '''<STR_LIT>''' <EOL> exists = False <EOL> portal_usernames = [ ] <EOL> portal_users = portaladmin . org_users ( ) <EOL> for portal_user in portal_users : <EOL> portal_usernames . append ( portal_user [ '<STR_LIT:username>' ] ) <EOL> if username in portal_usernames : <EOL> exists = True <EOL> return exists <EOL> def create_user ( portaladmin , contentpath , userinfo ) : <EOL> username = userinfo [ '<STR_LIT>' ] <EOL> password = userinfo [ '<STR_LIT>' ] <EOL> userfoldername = userinfo [ '<STR_LIT:username>' ] <EOL> user_exists = has_user ( portaladmin , username ) <EOL> if not user_exists : <EOL> print '<STR_LIT>' . format ( username ) <EOL> userfolder = os . path . join ( contentpath , userfoldername ) <EOL> os . chdir ( userfolder ) <EOL> userproperties = json . load ( open ( "<STR_LIT>" ) ) <EOL> fullname = userproperties [ "<STR_LIT>" ] <EOL> role = userproperties [ "<STR_LIT>" ] <EOL> email = userproperties [ "<STR_LIT:email>" ] <EOL> if not email : <EOL> email = username + '<STR_LIT>' <EOL> portaladmin . signup ( username , password , fullname , email ) <EOL> portaladmin . update_user_role ( username , role ) <EOL> else : <EOL> print '<STR_LIT>' . format ( username ) <EOL> def has_folder ( portaladmin , username , foldername ) : <EOL> '''<STR_LIT>''' <EOL> exists = False <EOL> portal_folders = [ ] <EOL> for portal_folder in portaladmin . folders ( username ) : <EOL> portal_folders . append ( portal_folder [ '<STR_LIT:title>' ] ) <EOL> if foldername in portal_folders : <EOL> exists = True <EOL> return exists <EOL> def create_user_folders ( portaladmin , contentpath , userinfo ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( os . path . join ( contentpath , userinfo [ '<STR_LIT:username>' ] , '<STR_LIT>' ) ) : <EOL> print '<STR_LIT>' <EOL> else : <EOL> os . chdir ( os . path . join ( contentpath , userinfo [ '<STR_LIT:username>' ] ) ) <EOL> folderinfo = json . load ( open ( '<STR_LIT>' ) ) <EOL> for folder in folderinfo : <EOL> foldername = folder [ <NUM_LIT:1> ] <EOL> if not has_folder ( portaladmin , userinfo [ '<STR_LIT>' ] , foldername ) : <EOL> print '<STR_LIT>' . format ( foldername ) <EOL> portaladmin . create_folder ( userinfo [ '<STR_LIT>' ] , foldername ) <EOL> else : <EOL> print '<STR_LIT>' . format ( foldername ) <EOL> def publish_user_items ( portal , username , usercontentpath , old_hostname , new_hostname , new_port , specified_groups , id_mapping_file , grps_on_disk ) : <EOL> '''<STR_LIT>''' <EOL> newitems , old_source_ids = [ ] , [ ] <EOL> existing_portal_ids = [ ] <EOL> print "<STR_LIT:\n>" + sectionBreak <EOL> print "<STR_LIT>" + username + "<STR_LIT>" <EOL> id_mapping = None <EOL> if id_mapping_file : <EOL> filefolder = os . path . dirname ( id_mapping_file ) <EOL> filename = os . path . basename ( id_mapping_file ) <EOL> os . chdir ( filefolder ) <EOL> id_mapping = json . load ( open ( filename ) ) <EOL> existing_portal_items = portal . search ( ) <EOL> for existing_portal_item in existing_portal_items : <EOL> existing_portal_ids . append ( existing_portal_item [ '<STR_LIT:id>' ] ) <EOL> item_dirs = os . listdir ( os . path . join ( usercontentpath , "<STR_LIT>" ) ) <EOL> n = <NUM_LIT:1> <EOL> for item_dir in item_dirs : <EOL> overwrite_id = None <EOL> do_load_item = False <EOL> foldername = None <EOL> print "<STR_LIT>" . format ( item_dir , n , len ( item_dirs ) ) <EOL> if id_mapping : <EOL> overwrite_id = id_mapping . get ( item_dir ) [ '<STR_LIT:id>' ] <EOL> if overwrite_id : <EOL> print "<STR_LIT>" <EOL> if overwrite_id in existing_portal_ids : <EOL> print "<STR_LIT>" <EOL> else : <EOL> overwrite_id = None <EOL> print "<STR_LIT>" <EOL> if not specified_groups : <EOL> do_load_item = True <EOL> else : <EOL> os . chdir ( os . path . join ( usercontentpath , "<STR_LIT>" , item_dir ) ) <EOL> sharing_info = json . load ( open ( '<STR_LIT>' ) ) <EOL> item_groups = sharing_info . get ( '<STR_LIT>' ) <EOL> for item_group in item_groups : <EOL> grp_on_disk = grps_on_disk . get ( item_group ) <EOL> if grp_on_disk : <EOL> for specified_group in specified_groups : <EOL> if specified_group == grp_on_disk : <EOL> do_load_item = True <EOL> if not do_load_item : <EOL> print "<STR_LIT>" <EOL> if do_load_item : <EOL> try : <EOL> item , old_source_id = load_item ( portal , os . path . join ( usercontentpath , "<STR_LIT>" , item_dir ) , overwrite_id ) <EOL> newitems . append ( item ) <EOL> old_source_ids . append ( old_source_id ) <EOL> if os . path . exists ( os . path . join ( usercontentpath , "<STR_LIT>" ) ) : <EOL> os . chdir ( usercontentpath ) <EOL> foldersinfo = json . load ( open ( '<STR_LIT>' ) ) <EOL> foldername = publish_get_folder_name_for_item ( item , foldersinfo ) <EOL> portal . reassign_item ( item [ '<STR_LIT:id>' ] , username , foldername ) <EOL> except Exception as err : <EOL> print '<STR_LIT>' . format ( item_dir ) <EOL> n = n + <NUM_LIT:1> <EOL> return newitems , old_source_ids <EOL> def publish_user_groups ( portal , contentpath , userinfo , users ) : <EOL> username = userinfo [ '<STR_LIT>' ] <EOL> password = userinfo [ '<STR_LIT>' ] <EOL> userfoldername = userinfo [ '<STR_LIT:username>' ] <EOL> groupfolder = os . path . join ( contentpath , userfoldername , '<STR_LIT>' ) <EOL> groupfolders = os . listdir ( groupfolder ) <EOL> numTotalGroupFolders = len ( groupfolders ) <EOL> if numTotalGroupFolders == <NUM_LIT:0> : <EOL> print "<STR_LIT>" . format ( username ) <EOL> else : <EOL> print "<STR_LIT>" . format ( str ( numTotalGroupFolders ) , username ) <EOL> groupcount = <NUM_LIT:0> <EOL> groupsprocessed = <NUM_LIT:0> <EOL> groupsFailed = [ "<STR_LIT:None>" ] <EOL> for folder in groupfolders : <EOL> groupcount = groupcount + <NUM_LIT:1> <EOL> oldGroupID = folder <EOL> groupidfolder = os . path . join ( groupfolder , folder ) <EOL> os . chdir ( groupidfolder ) <EOL> group = json . load ( open ( "<STR_LIT>" ) ) <EOL> group_users = json . load ( open ( "<STR_LIT>" ) ) <EOL> groupId = getGroupID ( portal , username , str ( group [ '<STR_LIT:title>' ] ) ) <EOL> if not groupId : <EOL> print "<STR_LIT>" + str ( group [ '<STR_LIT:title>' ] ) + "<STR_LIT:'>" <EOL> groupId = portal . create_group ( group , group [ '<STR_LIT>' ] ) <EOL> portal . reassign_group ( groupId , username ) <EOL> else : <EOL> print "<STR_LIT>" + str ( group [ '<STR_LIT:title>' ] ) + "<STR_LIT>" <EOL> destGroupID_GroupName [ groupId ] = str ( group [ '<STR_LIT:title>' ] ) <EOL> print "<STR_LIT>" <EOL> if groupId : <EOL> originGroupToDestGroups [ oldGroupID ] = groupId <EOL> groups = [ ] <EOL> groups . append ( groupId ) <EOL> users_to_invite = [ ] <EOL> portal_users = get_target_users ( users ) <EOL> for u in map_group_users ( group_users [ "<STR_LIT>" ] , users ) : <EOL> u = str ( u ) <EOL> if u in portal_users : <EOL> users_to_invite . append ( u ) <EOL> if len ( users_to_invite ) != <NUM_LIT:0> : <EOL> print "<STR_LIT>" + str ( users_to_invite ) <EOL> results = portal . add_group_users ( users_to_invite , groups ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> groupsprocessed = groupsprocessed + <NUM_LIT:1> <EOL> else : <EOL> groupsFailed . append ( groupID ) <EOL> os . chdir ( groupfolder ) <EOL> return originGroupToDestGroups <EOL> def get_target_users ( users ) : <EOL> target_users = [ ] <EOL> for user , userinfo in users . iteritems ( ) : <EOL> target_users . append ( userinfo [ '<STR_LIT>' ] ) <EOL> return target_users <EOL> def map_group_users ( group_users , users ) : <EOL> target_group_users = [ ] <EOL> for group_user in group_users : <EOL> if group_user in users . keys ( ) : <EOL> target_group_users . append ( users [ group_user ] [ '<STR_LIT>' ] ) <EOL> return target_group_users <EOL> def publish_portal_resources ( portaladmin , portal_properties ) : <EOL> portalID = portal_properties [ '<STR_LIT:id>' ] <EOL> resourcesJSON = json . load ( open ( os . path . join ( contentpath , '<STR_LIT>' ) ) ) <EOL> resourcesList = resourcesJSON [ '<STR_LIT>' ] <EOL> print "<STR_LIT>" <EOL> for rsc in resourcesList : <EOL> key = rsc [ '<STR_LIT:key>' ] <EOL> if key != '<STR_LIT>' : <EOL> datafile = os . path . join ( contentpath , key ) <EOL> resp = portaladmin . add_resource ( portalID , key , datafile , None ) <EOL> def publish_portal_properties ( portaladmin , portal_properties , oldHostname , newHostname ) : <EOL> portalID = portal_properties [ '<STR_LIT:id>' ] <EOL> resp = portaladmin . publish_portal_properties ( portalID , portal_properties , oldHostname , newHostname ) <EOL> if resp [ '<STR_LIT:success>' ] == True : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> def get_servername_from_url ( url ) : <EOL> '''<STR_LIT>''' <EOL> p = urlparse . urlparse ( url ) <EOL> return p . hostname <EOL> def publish_get_folder_name_for_item ( item , folders ) : <EOL> '''<STR_LIT>''' <EOL> folderName = None <EOL> ownerFolderID = item . get ( '<STR_LIT>' ) <EOL> if ownerFolderID is not None and ownerFolderID < > '<STR_LIT:null>' : <EOL> for folder in folders : <EOL> folderID = folder [ <NUM_LIT:0> ] <EOL> if folderID == ownerFolderID : <EOL> folderName = folder [ <NUM_LIT:1> ] <EOL> return folderName <EOL> def update_post_publish ( portal , hostname_map , id_map ) : <EOL> '''<STR_LIT>''' <EOL> items = portal . search ( ) <EOL> for item in items : <EOL> print_item_info ( item ) <EOL> update_item_properties ( portal , item , hostname_map , id_map ) <EOL> update_item_data ( portal , item , hostname_map , id_map ) <EOL> def update_item_properties ( portal , item , hostname_map , id_map ) : <EOL> '''<STR_LIT>''' <EOL> jsonPropsToUpdate = [ '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' ] <EOL> for jsonProp in jsonPropsToUpdate : <EOL> is_updated = False <EOL> propertyValue = item . get ( jsonProp ) <EOL> if propertyValue : <EOL> for host in hostname_map : <EOL> search_str_list = [ host , host . lower ( ) , host . upper ( ) ] <EOL> for search_str in search_str_list : <EOL> if propertyValue . find ( search_str ) > - <NUM_LIT:1> : <EOL> propertyValue = propertyValue . replace ( search_str , hostname_map [ host ] ) <EOL> is_updated = True <EOL> for item_id in id_map : <EOL> search_str_list = [ item_id , item_id . lower ( ) , item_id . upper ( ) ] <EOL> for search_str in search_str_list : <EOL> if propertyValue . find ( search_str ) > - <NUM_LIT:1> : <EOL> propertyValue = propertyValue . replace ( search_str , id_map [ item_id ] [ "<STR_LIT:id>" ] ) <EOL> is_updated = True <EOL> if is_updated : <EOL> portal . update_item ( item [ '<STR_LIT:id>' ] , { jsonProp : propertyValue } ) <EOL> def update_item_data ( portal , item , hostname_map , id_map ) : <EOL> '''<STR_LIT>''' <EOL> if item [ '<STR_LIT:type>' ] in TEXT_BASED_ITEM_TYPES : <EOL> try : <EOL> itemdata = portal . item_data ( item [ '<STR_LIT:id>' ] ) <EOL> except Exception as err : <EOL> print ( '<STR_LIT>' . format ( str ( item . get ( '<STR_LIT:title>' ) ) ) ) <EOL> itemdata = None <EOL> if itemdata : <EOL> is_updated = False <EOL> for host in hostname_map : <EOL> search_str_list = [ host , host . lower ( ) , host . upper ( ) ] <EOL> for search_str in search_str_list : <EOL> if itemdata . find ( search_str ) > - <NUM_LIT:1> : <EOL> itemdata = itemdata . replace ( search_str , hostname_map [ host ] ) <EOL> is_updated = True <EOL> for item_id in id_map : <EOL> search_str_list = [ item_id , item_id . lower ( ) , item_id . upper ( ) ] <EOL> for search_str in search_str_list : <EOL> if itemdata . find ( search_str ) > - <NUM_LIT:1> : <EOL> itemdata = itemdata . replace ( search_str , id_map [ item_id ] [ "<STR_LIT:id>" ] ) <EOL> is_updated = True <EOL> if is_updated : <EOL> portal . update_item ( item [ '<STR_LIT:id>' ] , { '<STR_LIT:text>' : itemdata } ) <EOL> if item [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> update_csv_item ( portal , item , hostname_map ) <EOL> def update_csv_item ( portal , item , hostname_map ) : <EOL> '''<STR_LIT>''' <EOL> filePath = portal . item_datad ( item [ '<STR_LIT:id>' ] ) <EOL> is_updated = False <EOL> for host in hostname_map : <EOL> search_str_list = [ host , host . lower ( ) , host . upper ( ) ] <EOL> for search_str in search_str_list : <EOL> if findInFile ( filePath , search_str ) : <EOL> editFiles ( [ filePath ] , search_str , hostname_map [ host ] ) <EOL> is_updated = True <EOL> if is_updated : <EOL> portal . update_item ( item [ '<STR_LIT:id>' ] , None , filePath ) <EOL> if os . path . exists ( filePath ) : <EOL> os . remove ( filePath ) <EOL> def print_item_info ( item ) : <EOL> itemID = item . get ( '<STR_LIT:id>' ) <EOL> itemTitle = item . get ( '<STR_LIT:title>' ) <EOL> itemOwner = item . get ( '<STR_LIT>' ) <EOL> itemType = item . get ( '<STR_LIT:type>' ) <EOL> print "<STR_LIT>" + str ( itemID ) + "<STR_LIT>" + str ( itemTitle ) + "<STR_LIT>" + str ( itemType ) + "<STR_LIT>" + str ( itemOwner ) + "<STR_LIT>" <EOL> def makeFolder ( folderPath ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( folderPath ) : <EOL> os . makedirs ( folderPath ) <EOL> def val_arg_content_path ( contentpath ) : <EOL> '''<STR_LIT>''' <EOL> is_valid = False <EOL> if os . path . exists ( contentpath ) : <EOL> if os . path . isdir ( contentpath ) : <EOL> is_valid = True <EOL> return is_valid <EOL> def read_userfile ( contentpath ) : <EOL> all_users = { } <EOL> userfile = open ( os . path . join ( contentpath , '<STR_LIT>' ) , '<STR_LIT:r>' ) <EOL> lineOne = True <EOL> for line in userfile : <EOL> if lineOne : <EOL> lineOne = False <EOL> continue <EOL> username , target_username , target_password = line . rstrip ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> all_users [ username ] = { '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : target_username , <EOL> '<STR_LIT>' : target_password } <EOL> userfile . close ( ) <EOL> return all_users <EOL> def val_arg_users ( specified_users , contentpath ) : <EOL> values_to_use = { } <EOL> exclude_users_list = [ ] <EOL> include_users_list = [ ] <EOL> if not specified_users : <EOL> specified_users = "<STR_LIT>" <EOL> else : <EOL> specified_users = specified_users . strip ( ) <EOL> all_users = read_userfile ( contentpath ) <EOL> if len ( specified_users ) == <NUM_LIT:0> : <EOL> return all_users <EOL> elif specified_users == "<STR_LIT:#>" : <EOL> return all_users <EOL> elif specified_users . find ( "<STR_LIT:->" ) == <NUM_LIT:0> : <EOL> values_to_use = all_users . copy ( ) <EOL> exclude_users = specified_users . replace ( "<STR_LIT:->" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> exclude_users_list = [ element . lower ( ) . strip ( ) for element in exclude_users . split ( "<STR_LIT:U+002C>" ) ] <EOL> for exclude_user in exclude_users_list : <EOL> for user in all_users : <EOL> if exclude_user == user . lower ( ) : <EOL> del values_to_use [ user ] <EOL> return values_to_use <EOL> else : <EOL> include_users_list = [ element . lower ( ) . strip ( ) for element in specified_users . split ( "<STR_LIT:U+002C>" ) ] <EOL> for include_user in include_users_list : <EOL> for user in all_users : <EOL> if include_user == user . lower ( ) : <EOL> values_to_use [ user ] = all_users [ user ] <EOL> return values_to_use <EOL> def share_templates ( portaladdress , username , password ) : <EOL> portal = Portal ( portaladdress , username , password ) <EOL> print '<STR_LIT>' <EOL> results = share_default_webapp_templates ( portal , portal . logged_in_user ( ) [ '<STR_LIT:username>' ] , '<STR_LIT>' ) <EOL> if results [ '<STR_LIT:success>' ] : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' . format ( results [ '<STR_LIT:message>' ] ) <EOL> print '<STR_LIT>' <EOL> results = share_default_gallery_templates ( portal , portal . logged_in_user ( ) [ '<STR_LIT:username>' ] , '<STR_LIT>' ) <EOL> if results [ '<STR_LIT:success>' ] : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' . format ( results [ '<STR_LIT:message>' ] ) <EOL> def share_default_gallery_templates ( portal , groupOwner , groupTitle ) : <EOL> func_results = { '<STR_LIT:success>' : True } <EOL> templateType = '<STR_LIT>' <EOL> results = share_default_templates ( portal , templateType , groupOwner , groupTitle ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> func_results [ '<STR_LIT:success>' ] = results [ '<STR_LIT:success>' ] <EOL> func_results [ '<STR_LIT:message>' ] = results [ '<STR_LIT:message>' ] <EOL> return func_results <EOL> def share_default_webapp_templates ( portal , groupOwner , groupTitle ) : <EOL> func_results = { '<STR_LIT:success>' : True } <EOL> templateType = '<STR_LIT>' <EOL> results = share_default_templates ( portal , templateType , groupOwner , groupTitle ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> func_results [ '<STR_LIT:success>' ] = results [ '<STR_LIT:success>' ] <EOL> func_results [ '<STR_LIT:message>' ] = results [ '<STR_LIT:message>' ] <EOL> return func_results <EOL> def share_default_templates ( portal , templateType , groupOwner , groupTitle ) : <EOL> func_results = { '<STR_LIT:success>' : True } <EOL> printStatement = '<STR_LIT>' <EOL> if templateType == '<STR_LIT>' : <EOL> templateTypeStr = '<STR_LIT>' <EOL> print printStatement . format ( templateTypeStr ) <EOL> templateIDs = unpack ( portal . webmap_templates ( [ '<STR_LIT:id>' ] ) ) <EOL> elif templateType == '<STR_LIT>' : <EOL> templateTypeStr = '<STR_LIT>' <EOL> print printStatement . format ( templateTypeStr ) <EOL> templateIDs = unpack ( portal . gallery_templates ( [ '<STR_LIT:id>' ] ) ) <EOL> else : <EOL> func_results [ '<STR_LIT:success>' ] = False <EOL> func_results [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( templateType ) <EOL> return func_results <EOL> if not templateIDs : <EOL> func_results [ '<STR_LIT:success>' ] = False <EOL> func_results [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( templateTypeStr . capitalize ( ) ) <EOL> return func_results <EOL> groupID = getGroupID ( portal , groupOwner , groupTitle ) <EOL> if not groupID : <EOL> func_results [ '<STR_LIT:success>' ] = False <EOL> func_results [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( groupOwner , groupTitle ) <EOL> return func_results <EOL> print '<STR_LIT>' . format ( groupTitle , groupOwner ) <EOL> results = share_template_items ( portal , templateIDs , [ groupID ] ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> func_results [ '<STR_LIT:success>' ] = False <EOL> print results [ '<STR_LIT:message>' ] <EOL> return func_results <EOL> def getGroupID ( portal , owner , title ) : <EOL> groupID = None <EOL> groups = portal . user ( owner ) [ '<STR_LIT>' ] <EOL> if groups : <EOL> for group in groups : <EOL> if group [ '<STR_LIT:title>' ] == title : <EOL> groupID = group [ '<STR_LIT:id>' ] <EOL> return groupID <EOL> def share_template_items ( portal , item_ids , group_ids ) : <EOL> func_results = { '<STR_LIT:success>' : True } <EOL> errList = [ ] <EOL> for item_id in item_ids : <EOL> results = portal . share_item ( item_id , group_ids ) <EOL> if len ( results [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> errList . append ( results ) <EOL> if len ( errList ) > <NUM_LIT:0> : <EOL> func_results [ '<STR_LIT:success>' ] = False <EOL> func_results [ '<STR_LIT:message>' ] = errList <EOL> return func_results <EOL> def tags_exist ( find_tags , tags_to_search ) : <EOL> '''<STR_LIT>''' <EOL> found = False <EOL> DEBUG = False <EOL> ops_type_flags = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> find_tags_mod = [ element . lower ( ) . strip ( ) . encode ( "<STR_LIT:ascii>" ) for element in list ( find_tags ) ] <EOL> tags_to_search_mod = [ element . lower ( ) . strip ( ) . encode ( "<STR_LIT:ascii>" ) for element in list ( tags_to_search ) ] <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" + str ( find_tags_mod ) <EOL> print "<STR_LIT>" + str ( tags_to_search_mod ) <EOL> print "<STR_LIT>" + str ( ops_type_flags ) <EOL> for search in tags_to_search_mod : <EOL> search = search . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" + search <EOL> if search . find ( "<STR_LIT::>" ) > - <NUM_LIT:1> : <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> element1 = search . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] <EOL> element2 = search . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> if DEBUG : <EOL> print "<STR_LIT>" + element1 <EOL> print "<STR_LIT>" + element2 <EOL> if element1 in ops_type_flags : <EOL> if element2 in find_tags_mod : <EOL> found = True <EOL> if DEBUG : <EOL> print "<STR_LIT>" + str ( found ) <EOL> return found <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
