<s> from lxml import etree <EOL> import urllib2 <EOL> import urlparse <EOL> from django . core import urlresolvers <EOL> from corehq . apps . app_manager . exceptions import AppEditingError <EOL> from corehq . apps . app_manager . xform import WrappedNode <EOL> from corehq . apps . users . util import cc_user_domain <EOL> class AutoSetVersions ( WrappedNode ) : <EOL> def auto_set_versions ( self , version ) : <EOL> if self . attrib . get ( '<STR_LIT:version>' ) == '<STR_LIT>' : <EOL> self_or_empty = [ self ] <EOL> else : <EOL> self_or_empty = [ ] <EOL> for node in self . findall ( '<STR_LIT>' ) + self_or_empty : <EOL> node . attrib [ '<STR_LIT:version>' ] = '<STR_LIT>' % version <EOL> class ProfileXML ( AutoSetVersions ) : <EOL> def set_property ( self , key , value ) : <EOL> node = self . find ( '<STR_LIT>' % key ) <EOL> if node . xml is None : <EOL> node = etree . Element ( '<STR_LIT>' ) <EOL> self . xml . insert ( <NUM_LIT:0> , node ) <EOL> node . attrib [ '<STR_LIT:key>' ] = key <EOL> node . attrib [ '<STR_LIT:value>' ] = value <EOL> def set_attribute ( self , key , value ) : <EOL> self . attrib [ key ] = value <EOL> def reset_suite_remote_url ( suite_node , url_base , profile_url , download_index_url ) : <EOL> suite_local_text = suite_node . findtext ( '<STR_LIT>' ) <EOL> suite_remote = suite_node . find ( '<STR_LIT>' ) <EOL> suite_name = strip_location ( profile_url , suite_local_text ) <EOL> suite_remote . xml . text = url_base + urlparse . urljoin ( download_index_url , <EOL> suite_name ) <EOL> def strip_location ( profile_url , location ) : <EOL> base = '<STR_LIT:/>' . join ( profile_url . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' <EOL> def strip_left ( prefix ) : <EOL> string = location <EOL> if string . startswith ( prefix ) : <EOL> return string [ len ( prefix ) : ] <EOL> return strip_left ( '<STR_LIT>' ) or strip_left ( base ) or strip_left ( '<STR_LIT>' ) or location <EOL> def make_remote_profile ( app ) : <EOL> try : <EOL> profile = urllib2 . urlopen ( app . profile_url ) . read ( ) <EOL> except Exception : <EOL> raise AppEditingError ( '<STR_LIT>' % app . profile_url ) <EOL> if app . manage_urls or app . build_langs : <EOL> profile_xml = ProfileXML ( profile ) <EOL> if app . manage_urls : <EOL> profile_xml . auto_set_versions ( app . version ) <EOL> profile_xml . set_attribute ( '<STR_LIT>' , app . profile_url ) <EOL> profile_xml . set_property ( "<STR_LIT>" , app . ota_restore_url ) <EOL> profile_xml . set_property ( "<STR_LIT>" , app . post_url ) <EOL> profile_xml . set_property ( "<STR_LIT>" , cc_user_domain ( app . domain ) ) <EOL> profile_xml . set_property ( '<STR_LIT>' , app . form_record_url ) <EOL> profile_xml . set_property ( '<STR_LIT>' , app . key_server_url ) <EOL> download_index_url = urlresolvers . reverse ( <EOL> '<STR_LIT>' , <EOL> args = [ app . domain , app . get_id ] , <EOL> ) <EOL> for suite_node in profile_xml . findall ( '<STR_LIT>' ) : <EOL> reset_suite_remote_url ( <EOL> suite_node = suite_node , <EOL> profile_url = app . profile_url , <EOL> url_base = app . url_base , <EOL> download_index_url = download_index_url <EOL> ) <EOL> if app . build_langs : <EOL> profile_xml . set_property ( "<STR_LIT>" , app . build_langs [ <NUM_LIT:0> ] ) <EOL> profile = profile_xml . render ( ) <EOL> return profile <EOL> def make_remote_suite ( app , suite_xml ) : <EOL> if app . manage_urls : <EOL> suite = AutoSetVersions ( suite_xml ) <EOL> suite . auto_set_versions ( app . version ) <EOL> return suite . render ( ) <EOL> else : <EOL> return suite_xml </s>
