<s> from __future__ import unicode_literals <EOL> from __future__ import print_function <EOL> from __future__ import absolute_import <EOL> import arcpy <EOL> import glob <EOL> import os <EOL> import shutil <EOL> import sys <EOL> try : <EOL> import ctypes <EOL> from ctypes import wintypes <EOL> kdll = ctypes . windll . LoadLibrary ( "<STR_LIT>" ) <EOL> except ImportError : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" <EOL> arcpy . AddError ( msg ) <EOL> sys . exit ( ) <EOL> from . bootstrap_r import execute_r <EOL> from . github_release import save_url , release_info <EOL> from . rpath import ( <EOL> r_library_path , <EOL> r_pkg_path , <EOL> r_pkg_version , <EOL> r_version_info , <EOL> arcmap_exists , <EOL> arcmap_install_path , <EOL> fnf_exception , <EOL> handle_fnf , <EOL> ) <EOL> from . utils import mkdtemp , set_env_tmpdir <EOL> from . fs import getvolumeinfo , hardlinks_supported , junctions_supported <EOL> try : <EOL> import winreg <EOL> except ImportError : <EOL> import _winreg as winreg <EOL> PACKAGE_NAME = '<STR_LIT>' <EOL> PACKAGE_VERSION = r_pkg_version ( ) <EOL> def bridge_running ( product ) : <EOL> """<STR_LIT>""" <EOL> running = False <EOL> if product == '<STR_LIT>' : <EOL> proxy_name = "<STR_LIT>" <EOL> else : <EOL> proxy_name = "<STR_LIT>" <EOL> kdll . GetModuleHandleW . restype = wintypes . HMODULE <EOL> kdll . GetModuleHandleW . argtypes = [ wintypes . LPCWSTR ] <EOL> dll_handle = kdll . GetModuleHandleW ( proxy_name ) <EOL> if dll_handle is not None : <EOL> running = True <EOL> return running <EOL> def arcgis_platform ( ) : <EOL> """<STR_LIT>""" <EOL> info = arcpy . GetInstallInfo ( ) <EOL> install_dir = info [ '<STR_LIT>' ] <EOL> arc_version = info [ '<STR_LIT>' ] <EOL> if info [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> product = '<STR_LIT>' <EOL> else : <EOL> product = '<STR_LIT>' <EOL> return ( install_dir , arc_version , product ) <EOL> def validate_environment ( ) : <EOL> """<STR_LIT>""" <EOL> ( install_dir , arc_version , product ) = arcgis_platform ( ) <EOL> no_hook_versions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if arc_version in no_hook_versions and product is not '<STR_LIT>' : <EOL> arcpy . AddError ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if arc_version in ( '<STR_LIT:1.0>' , '<STR_LIT>' ) and product == '<STR_LIT>' : <EOL> arcpy . AddError ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if bridge_running ( product ) : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" <EOL> arcpy . AddError ( msg ) <EOL> sys . exit ( ) <EOL> def create_registry_entry ( product , arc_version ) : <EOL> """<STR_LIT>""" <EOL> root_key = winreg . HKEY_CURRENT_USER <EOL> if product == '<STR_LIT>' : <EOL> product_name = "<STR_LIT>" <EOL> else : <EOL> product_name = "<STR_LIT>" . format ( arc_version ) <EOL> reg_path = "<STR_LIT>" . format ( product_name ) <EOL> package_key = '<STR_LIT>' <EOL> link_key = None <EOL> try : <EOL> full_access = ( winreg . KEY_WOW64_64KEY + winreg . KEY_ALL_ACCESS ) <EOL> link_key = winreg . OpenKey ( root_key , reg_path , <NUM_LIT:0> , full_access ) <EOL> except fnf_exception as error : <EOL> handle_fnf ( error ) <EOL> if link_key : <EOL> try : <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> binding_path = "<STR_LIT>" . format ( r_library_path , "<STR_LIT>" ) <EOL> winreg . SetValueEx ( link_key , package_key , <NUM_LIT:0> , <EOL> winreg . REG_SZ , binding_path ) <EOL> except fnf_exception as error : <EOL> handle_fnf ( error ) <EOL> def install_package ( overwrite = False , r_library_path = r_library_path ) : <EOL> """<STR_LIT>""" <EOL> if overwrite is True : <EOL> overwrite = True <EOL> else : <EOL> overwrite = False <EOL> if not overwrite : <EOL> if PACKAGE_VERSION : <EOL> msg = "<STR_LIT>" <EOL> arcpy . AddError ( msg ) <EOL> sys . exit ( ) <EOL> ( install_dir , arc_version , product ) = arcgis_platform ( ) <EOL> arcmap_needs_link = False <EOL> validate_environment ( ) <EOL> if product == '<STR_LIT>' and arcmap_exists ( "<STR_LIT>" ) : <EOL> arcmap_needs_link = True <EOL> msg_base = "<STR_LIT>" <EOL> if arcmap_install_path is not None : <EOL> msg = "<STR_LIT>" . format ( msg_base ) <EOL> arcpy . AddMessage ( msg ) <EOL> else : <EOL> msg = "<STR_LIT>" . format ( msg_base ) + "<STR_LIT>" <EOL> arcpy . AddWarning ( msg ) <EOL> if arc_version == '<STR_LIT>' and product == '<STR_LIT>' or arcmap_needs_link : <EOL> r_integration_dir = os . path . join ( arcmap_install_path , "<STR_LIT>" ) <EOL> if not os . path . exists ( r_integration_dir ) : <EOL> try : <EOL> write_test = os . path . join ( install_dir , '<STR_LIT>' ) <EOL> with open ( write_test , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT:test>' ) <EOL> os . remove ( write_test ) <EOL> os . makedirs ( r_integration_dir ) <EOL> except IOError : <EOL> arcpy . AddError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( product ) ) <EOL> sys . exit ( ) <EOL> orig_tmpdir = os . getenv ( "<STR_LIT>" ) <EOL> if not orig_tmpdir : <EOL> set_env_tmpdir ( ) <EOL> download_url = release_info ( ) [ <NUM_LIT:0> ] <EOL> if download_url is None : <EOL> arcpy . AddWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> base_path = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , '<STR_LIT:..>' ) <EOL> zip_glob = glob . glob ( os . path . join ( base_path , "<STR_LIT>" ) ) <EOL> if zip_glob and os . path . exists ( zip_glob [ <NUM_LIT:0> ] ) : <EOL> zip_path = zip_glob [ <NUM_LIT:0> ] <EOL> local_install = True <EOL> else : <EOL> local_install = False <EOL> with mkdtemp ( ) as temp_dir : <EOL> zip_name = os . path . basename ( download_url ) <EOL> package_path = os . path . join ( temp_dir , zip_name ) <EOL> if local_install : <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> shutil . copyfile ( zip_path , package_path ) <EOL> else : <EOL> save_url ( download_url , package_path ) <EOL> if os . path . exists ( package_path ) : <EOL> execute_r ( '<STR_LIT>' , '<STR_LIT>' , package_path ) <EOL> else : <EOL> arcpy . AddError ( "<STR_LIT>" . format ( package_path ) ) <EOL> set_env_tmpdir ( orig_tmpdir ) <EOL> if ( arc_version == '<STR_LIT>' and product == '<STR_LIT>' ) or ( arc_version in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> and product == '<STR_LIT>' ) : <EOL> ( r_major , r_minor , r_patchlevel ) = r_version_info . split ( "<STR_LIT:.>" ) <EOL> if len ( r_patchlevel ) > <NUM_LIT:1> and '<STR_LIT>' in r_library_path : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" <EOL> arcpy . AddError ( msg ) <EOL> sys . exit ( ) <EOL> if arc_version == '<STR_LIT>' and product == '<STR_LIT>' or arcmap_needs_link : <EOL> link_dir = os . path . join ( r_integration_dir , PACKAGE_NAME ) <EOL> if os . path . exists ( link_dir ) : <EOL> if junctions_supported ( link_dir ) or hardlinks_supported ( link_dir ) : <EOL> os . rmdir ( link_dir ) <EOL> else : <EOL> shutil . rmtree ( link_dir ) <EOL> r_package_path = r_pkg_path ( ) <EOL> if r_package_path : <EOL> arcpy . AddMessage ( "<STR_LIT>" . format ( r_package_path ) ) <EOL> else : <EOL> arcpy . AddError ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> detect_msg = "<STR_LIT>" <EOL> if junctions_supported ( link_dir ) or hardlinks_supported ( link_dir ) : <EOL> arcpy . AddMessage ( "<STR_LIT>" . format ( detect_msg ) ) <EOL> kdll . CreateSymbolicLinkW ( link_dir , r_package_path , <NUM_LIT:1> ) <EOL> else : <EOL> vol_info = getvolumeinfo ( link_dir ) <EOL> arcpy . AddMessage ( "<STR_LIT>" . format ( <EOL> detect_msg , vol_info [ <NUM_LIT:0> ] ) ) <EOL> shutil . copytree ( r_package_path , link_dir ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> overwrite = sys . argv [ <NUM_LIT:1> ] <EOL> else : <EOL> overwrite = None <EOL> print ( "<STR_LIT>" . format ( r_library_path ) ) <EOL> install_package ( overwrite = overwrite , r_library_path = r_library_path ) </s>
