def get_web_configs(self, webconfig_path_list, shell, webservice_ext_filter):
        fs = file_system.createFileSystem(shell)
        configs = []
        for webconfig_path in webconfig_path_list:
            try:
                webconfig_path = webconfig_path.find("%") != -1 and shell_interpreter.dereference_string(shell,
                                                                                                         webconfig_path) or webconfig_path
                default_configs = map(
                    lambda obj: obj.find("%") != -1 and shell_interpreter.dereference_string(shell, obj) or obj,
                    self.DEFAULT_WEB_CONFIGURATION_LOCATIONS)
                if not webconfig_path in default_configs and fs.exists(webconfig_path):
                    file_attr = (file_topology.BASE_FILE_ATTRIBUTES +
                                 [file_topology.FileAttrs.CONTENT, file_topology.FileAttrs.LAST_MODIFICATION_TIME])
                    logger.debug("getting config file:", webconfig_path)
                    resource_path = ''
                    match = re.match('(.*)\\\\.*', webconfig_path)
                    if match:
                        resource_path = match.group(1)
                    logger.debug("getting config file path:", resource_path)
                    files = fs.getFiles(resource_path, filters = [file_system.ExtensionsFilter(webservice_ext_filter)],
                                           fileAttrs = [file_topology.FileAttrs.NAME,
                                                        file_topology.FileAttrs.PATH])

                    for webservicefile in files:
                        logger.debug("getting webservice file:", webservicefile.path)
                        file = fs.getFile(webservicefile.path, file_attr)
                        if file:
                            content = file.content
                            config_file = iis.ConfigFile(webservicefile.path, content, file.lastModificationTime(),)
                            configs.append(config_file)

                    webconfig = fs.getFile(webconfig_path, file_attr)
                    if webconfig:
                        content = webconfig.content
                        content = content.strip()
                        xmlContentStartIndex = content.find('<?xml')
                        if xmlContentStartIndex != -1:
                            content = content[xmlContentStartIndex:]

                        # Lazy intilization of old code to prevent cyclic dependencies
                        from NTCMD_IIS import WebConfig

                        content = WebConfig.replacePasswords(content)

                        db_datasources = self.get_db_datasources(content)

                        config_file = iis.ConfigFile(webconfig_path, content, webconfig.lastModificationTime(),
                                                     db_datasources)
                        configs.append(config_file)
            except:
                logger.warn("Unable to discover %s" % webconfig_path)
                logger.debugException("")
                logger.reportWarning("Unable to discover some of config files")
        return configs
    def parse_site(self, result, app_pools, app_of_sites):
        root = ET.fromstring(result)
        site_elements = root.findall('SITE')
        sites = []
        for element in site_elements:
            site_name = element.get('SITE.NAME')
            bindings_attr = element.get('bindings')
            state = iis.get_apppool_state(element.get('state'))
            bindings = self.getBindings(bindings_attr)
            apps = app_of_sites.get(site_name)
            site_path = None
            app_pool_of_site = None
            virtual_dirs_of_site = None
            web_applications = []
            for app in apps:
                if app.path == '/':
                    app_pool_of_site = app_pools.get(app.app_pool_name)
                    site_path = app.physical_path
                    if site_path and site_path.find("%") != -1:
                        site_path = shell_interpreter.dereference_string(
                            self.shell, site_path)
                    virtual_dirs_of_site = app.virtual_dirs
                else:
                    web_applications.append(app)

            config_files = self.get_web_configs(site_path, self.shell, [])
            site = Site(site_name, bindings, app_pool_of_site, state,
                        site_path, config_files, web_applications,
                        virtual_dirs_of_site, [])
            sites.append(site)
        return sites
    def discover_websites(self, apppools_map_object, executor, shell, host_ips=None, webservice_ext_filter=[]):
        sites_info = WebSitesCmd() | executor
        sites = []
        for site_info in sites_info:
            site_name = site_info.get("name")
            site_apppool = site_info.get("applicationPool")
            site_state = iis.get_apppool_state(site_info.get("state"))
            site_path = site_info.get("physicalPath")
            if site_name:
                try:
                    bindings = WebSiteBindingCmd(site_name) | executor
                    parse_func = partial(parse_bindings, host_ips=host_ips)
                    bindings = map(parse_func, bindings)
                    site_configs = []
                    try:
                        site_configs = self.discover_webconfigs(site_name, executor, shell, webservice_ext_filter)
                    except Exception, ex:
                        logger.debug("cannot find config file for site: ", site_name)

                    applications, app_web_services = self.discover_applications(
                        site_name, executor, shell, webservice_ext_filter
                    )

                    dirs, dir_web_services = self.discover_virtual_dirs(
                        site_name, executor, shell, webservice_ext_filter
                    )

                    web_services = []
                    if app_web_services:
                        web_services.extend(app_web_services)
                    if dir_web_services:
                        web_services.extend(dir_web_services)

                    if site_path and site_path.find("%") != -1:
                        site_path = shell_interpreter.dereference_string(shell, site_path)

                    sites.append(
                        iis.Site(
                            site_name,
                            bindings,
                            apppools_map_object[site_apppool],
                            site_state,
                            site_path,
                            site_configs,
                            applications,
                            dirs,
                            web_services,
                        )
                    )
                except Exception, ex:
                    logger.debugException(str(ex))
                    logger.warnException(str(ex))
    def parse_site(self, result, app_pools, app_of_sites):
        root = ET.fromstring(result)
        site_elements = root.findall("SITE")
        sites = []
        for element in site_elements:
            site_name = element.get("SITE.NAME")
            bindings_attr = element.get("bindings")
            state = iis.get_apppool_state(element.get("state"))
            bindings = self.getBindings(bindings_attr)
            apps = app_of_sites.get(site_name)
            site_path = None
            app_pool_of_site = None
            virtual_dirs_of_site = None
            web_applications = []
            for app in apps:
                if app.path == "/":
                    app_pool_of_site = app_pools.get(app.app_pool_name)
                    site_path = app.physical_path
                    if site_path and site_path.find("%") != -1:
                        site_path = shell_interpreter.dereference_string(self.shell, site_path)
                    virtual_dirs_of_site = app.virtual_dirs
                else:
                    web_applications.append(app)

            config_files = self.get_web_configs(site_path, self.shell, [])
            site = Site(
                site_name,
                bindings,
                app_pool_of_site,
                state,
                site_path,
                config_files,
                web_applications,
                virtual_dirs_of_site,
                [],
            )
            sites.append(site)
        return sites
    def discover_websites(self, apppools_map_object, executor, shell, host_ips=None, webservice_ext_filter=[]):
        sites_info = WebSitesCmd() | executor
        sites = []
        for site_info in sites_info:
            site_name = site_info.get("name")
            site_apppool = site_info.get("applicationPool")
            site_state = iis.get_apppool_state(site_info.get("state"))
            site_path = site_info.get("physicalPath")
            if site_name:
                try:
                    bindings = WebSiteBindingCmd(site_name) | executor
                    parse_func = partial(parse_bindings, host_ips=host_ips)
                    bindings = map(parse_func, bindings)
                    site_configs =[]
                    try:
                        site_configs = self.discover_webconfigs(site_name, executor, shell, webservice_ext_filter)
                    except Exception, ex:
                        logger.debug("cannot find config file for site: ", site_name)
                    
                    applications, app_web_services = self.discover_applications(site_name, executor, shell, webservice_ext_filter)

                    dirs, dir_web_services = self.discover_virtual_dirs(site_name, executor, shell, webservice_ext_filter)

                    web_services = []
                    if app_web_services:
                        web_services.extend(app_web_services)
                    if dir_web_services:
                        web_services.extend(dir_web_services)

                    if site_path and site_path.find("%") != -1:
                        site_path = shell_interpreter.dereference_string(shell, site_path)

                    sites.append(iis.Site(site_name, bindings, apppools_map_object[site_apppool], site_state,
                                          site_path, site_configs, applications, dirs, web_services))
                except Exception, ex:
                    logger.debugException(str(ex))
                    logger.warnException(str(ex))
    def get_web_configs(self, webconfig_path_list, shell, webservice_ext_filter):
        fs = file_system.createFileSystem(shell)
        configs = []
        for webconfig_path in webconfig_path_list:
            try:
                webconfig_path = (
                    webconfig_path.find("%") != -1
                    and shell_interpreter.dereference_string(shell, webconfig_path)
                    or webconfig_path
                )
                default_configs = map(
                    lambda obj: obj.find("%") != -1 and shell_interpreter.dereference_string(shell, obj) or obj,
                    self.DEFAULT_WEB_CONFIGURATION_LOCATIONS,
                )
                if not webconfig_path in default_configs and fs.exists(webconfig_path):
                    file_attr = file_topology.BASE_FILE_ATTRIBUTES + [
                        file_topology.FileAttrs.CONTENT,
                        file_topology.FileAttrs.LAST_MODIFICATION_TIME,
                    ]
                    logger.debug("getting config file:", webconfig_path)
                    resource_path = ""
                    match = re.match("(.*)\\\\.*", webconfig_path)
                    if match:
                        resource_path = match.group(1)
                    logger.debug("getting config file path:", resource_path)
                    files = fs.getFiles(
                        resource_path,
                        filters=[file_system.ExtensionsFilter(webservice_ext_filter)],
                        fileAttrs=[file_topology.FileAttrs.NAME, file_topology.FileAttrs.PATH],
                    )

                    for webservicefile in files:
                        logger.debug("getting webservice file:", webservicefile.path)
                        file = fs.getFile(webservicefile.path, file_attr)
                        if file:
                            content = file.content
                            config_file = iis.ConfigFile(webservicefile.path, content, file.lastModificationTime())
                            configs.append(config_file)

                    webconfig = fs.getFile(webconfig_path, file_attr)
                    if webconfig:
                        content = webconfig.content
                        content = content.strip()
                        xmlContentStartIndex = content.find("<?xml")
                        if xmlContentStartIndex != -1:
                            content = content[xmlContentStartIndex:]

                        # Lazy intilization of old code to prevent cyclic dependencies
                        from NTCMD_IIS import WebConfig

                        content = WebConfig.replacePasswords(content)

                        db_datasources = self.get_db_datasources(content)

                        config_file = iis.ConfigFile(
                            webconfig_path, content, webconfig.lastModificationTime(), db_datasources
                        )
                        configs.append(config_file)
            except:
                logger.warn("Unable to discover %s" % webconfig_path)
                logger.debugException("")
                logger.reportWarning("Unable to discover some of config files")
        return configs