class OfflineEventCreator(object):

    def __init__(self, rh, conf, event_type=""):
        self._rh = rh
        self._conf = conf
        self.event = conf.as_event
        self._html = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._eventType = event_type
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}

    def create(self, static_site_id):
        config = Config.getInstance()
        self._fileHandler = ZIPFileHandler()

        # create the home page html
        self._create_home()

        # Create main and static folders
        self._mainPath = self._normalize_path(u'OfflineWebsite-{}'.format(self._conf.getTitle().decode('utf-8')))
        self._fileHandler.addDir(self._mainPath)
        self._staticPath = os.path.join(self._mainPath, "static")
        self._fileHandler.addDir(self._staticPath)
        # Add i18n js
        self._addFolderFromSrc(os.path.join(self._staticPath, 'js', 'indico', 'i18n'),
                               os.path.join(config.getHtdocsDir(), 'js', 'indico', 'i18n'))
        # Add system icons (not referenced in HTML/CSS)
        for icon in Config.getInstance().getSystemIcons().itervalues():
            self._addFileFromSrc(os.path.join(self._staticPath, 'images', icon),
                                 os.path.join(config.getHtdocsDir(), 'images', icon))
        # IE compat files (in conditional comments so BS doesn't see them)
        for path in ie_compatibility.urls():
            self._addFileFromSrc(os.path.join(self._staticPath, path.lstrip('/')),
                                 os.path.join(config.getHtdocsDir(), path.lstrip('/')))

        # Getting all materials, static files (css, images, js and vars.js.tpl)
        self._getAllMaterial()
        self._html = self._get_static_files(self._html)

        # Specific changes
        self._create_other_pages()

        # Retrieve files that were not available in the file system (e.e. js/css from plugins)
        self._get_failed_paths()
        self._failed_paths = set()

        # Retrieve files referenced in CSS files
        self._get_css_refs()

        # A custom event CSS might reference an uploaded image so we need to check for failed paths again
        self._get_failed_paths()

        # Create overview.html file (main page for the event)
        conferenceDisplayPath = os.path.join(self._mainPath, 'overview.html')
        self._fileHandler.addNewFile(conferenceDisplayPath, self._html)

        # Creating index.html file
        self._fileHandler.addNewFile('index.html',
                                     '<meta http-equiv="Refresh" content="0; url=%s">' % conferenceDisplayPath)

        self._fileHandler.close()
        return self._save_file(self._fileHandler.getPath(), static_site_id)

    def _get_static_files(self, html):
        config = Config.getInstance()
        soup = BeautifulSoup(html)
        images = set(_fix_url_path(x['src']) for x in soup.select('img[src]'))
        scripts = set(_fix_url_path(x['src']) for x in soup.select('script[src]'))
        styles = set(_fix_url_path(x['href']) for x in soup.select('link[rel="stylesheet"]'))
        for path in itertools.chain(images, scripts, styles):
            src_path = re.sub(r'#.*$', '', os.path.join(config.getHtdocsDir(), path))
            dst_path = os.path.join(self._staticPath, path)
            if path in styles:
                self._css_files.add(path)
            if not os.path.isfile(src_path):
                self._failed_paths.add(path)
            elif path not in styles:
                self._addFileFromSrc(dst_path, src_path)
        for image in soup.select('img[src]'):
            image['src'] = os.path.join('static', _fix_url_path(image['src']))
        for script in soup.select('script[src]'):
            script['src'] = os.path.join('static', _fix_url_path(script['src']))
        for style in soup.select('link[rel="stylesheet"]'):
            style['href'] = os.path.join('static', _fix_url_path(style['href']))
        return str(soup)

    def _get_failed_paths(self):
        """Downloads files that were not available in the fielystem via HTTP.
        This is the only clean way to deal with static files from plugins since otherwise
        we would have to emulate RHHtdocs.
        """
        cfg = Config.getInstance()
        # If we have the embedded webserver prefer its base url since the iptables hack does
        # not work for connections from the same machine
        base_url = cfg.getBaseURL()
        for path in self._failed_paths:
            dst_path = os.path.join(self._staticPath, path)
            if not self._fileHandler.hasFile(dst_path):
                response = requests.get(os.path.join(base_url, path), verify=False)
                self._downloaded_files[dst_path] = response.content
                self._fileHandler.addNewFile(dst_path, response.content)

    def _get_css_refs(self):
        """Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
        config = Config.getInstance()
        for path in self._css_files:
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if dst_path in self._downloaded_files and not os.path.exists(src_path):
                css = self._downloaded_files[dst_path]
            else:
                with open(src_path, 'rb') as f:
                    css = f.read()
            # Extract all paths inside url()
            urls = set(m.group('url') for m in RE_CSS_URL.finditer(css) if m.group('url')[0] != '#')
            for url in urls:
                orig_url = url
                url = _remove_qs(url)  # get rid of cache busters
                if url[0] == '/':
                    # make it relative and resolve '..' elements
                    url = os.path.normpath(url[1:])
                    # anything else is straightforward: the url is now relative to the htdocs folder
                    ref_src_path = os.path.join(config.getHtdocsDir(), url)
                    ref_dst_path = os.path.join(self._staticPath, url)
                    # the new url is relative to the css location
                    static_url = os.path.relpath(url, os.path.dirname(path))
                else:
                    # make the relative path absolute (note: it's most likely NOT relative to htdocs!)
                    css_abs_path = os.path.join(config.getHtdocsDir(), path)
                    # now we can combine the relative url with that path to get the proper paths of the resource
                    ref_src_path = os.path.normpath(os.path.join(os.path.dirname(css_abs_path), url))
                    ref_dst_path = os.path.normpath(os.path.join(self._staticPath, os.path.dirname(path), url))
                    static_url = os.path.relpath(ref_src_path, os.path.dirname(css_abs_path))
                if not os.path.isfile(ref_src_path):
                    htdocs_relative_path = os.path.relpath(ref_src_path, config.getHtdocsDir())
                    htdocs_relative_path = re.sub(r'#.*$', '', htdocs_relative_path)
                    self._failed_paths.add(htdocs_relative_path)
                else:
                    self._addFileFromSrc(ref_dst_path, ref_src_path)
                css = css.replace(orig_url, static_url)
            self._fileHandler.addNewFile(dst_path, css)

    def _create_home(self):
        # get default/selected view
        styleMgr = HelperMaKaCInfo.getMaKaCInfoInstance().getStyleManager()
        view = self._rh._target.getDefaultStyle()
        # if no default view was attributed, then get the configuration default
        if view == "" or not styleMgr.existsStyle(view) or view in styleMgr.getXSLStyles():
            view = styleMgr.getDefaultStyleForEventType(self._eventType)
        p = WPTPLStaticConferenceDisplay(self._rh, self._rh._target, view, self._eventType, self._rh._reqParams)
        self._html = p.display(**self._rh._getRequestParams())

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return secure_filename(remove_tags(path))

    def _getAllMaterial(self):
        self._addMaterialFrom(self._conf, "events/conference")
        for contrib in self._conf.getContributionList():
            self._addMaterialFrom(contrib, "agenda/%s-contribution" % contrib.getId())
            if contrib.getSubContributionList():
                for sc in contrib.getSubContributionList():
                    self._addMaterialFrom(sc, "agenda/%s-subcontribution" % sc.getId())
        for session in self._conf.getSessionList():
            self._addMaterialFrom(session, "agenda/%s-session" % session.getId())

    def _addMaterialFrom(self, target, categoryPath):
        for folder in AttachmentFolder.get_for_linked_object(target, preload_event=True):
            for attachment in folder.attachments:
                if attachment.type == AttachmentType.file:
                    dst_path = posixpath.join(self._mainPath, "files", categoryPath,
                                              "{}-{}".format(attachment.id, attachment.file.filename))
                    with attachment.file.get_local_path() as file_path:
                        self._addFileFromSrc(dst_path, file_path)

    def _addFileFromSrc(self, dstPath, srcPath):
        if not os.path.isfile(dstPath) and os.path.isfile(srcPath):
            if not self._fileHandler.hasFile(dstPath):
                newFile = open(srcPath, "rb")
                self._fileHandler.addNewFile(dstPath, newFile.read())
                newFile.close()

    def _addFolderFromSrc(self, dstPath, srcPath):
        for root, subfolders, files in os.walk(srcPath):
            for filename in files:
                src_filepath = os.path.join(root, filename)
                dst_dirpath = os.path.join(dstPath, root.strip(srcPath))
                dst_filepath = os.path.join(dst_dirpath, filename)
                self._fileHandler.addDir(dst_dirpath)
                if not self._fileHandler.hasFile(dst_filepath):
                    self._fileHandler.add(dst_filepath, src_filepath)

    def _save_file(self, srcPath, static_site_id):
        volume = HelperMaKaCInfo.getMaKaCInfoInstance().getArchivingVolume()
        path = os.path.join(Config.getInstance().getOfflineStore(), volume, 'offline', self._conf.getId())
        file_path = os.path.join(path, '{}.zip'.format(static_site_id))
        try:
            os.makedirs(path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        shutil.copyfile(srcPath, file_path)
        return file_path
Esempio n. 2
0
class OfflineEventCreator(object):
    def __init__(self, rh, conf, event_type=""):
        self._rh = rh
        self._conf = conf
        self._display_tz = timezoneUtils.DisplayTZ(self._rh._aw,
                                                   self._conf).getDisplayTZ()
        self.event = conf.as_event
        self._html = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._eventType = event_type
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}

    def create(self, static_site_id):
        config = Config.getInstance()
        self._fileHandler = ZIPFileHandler()

        # create the home page html
        self._create_home()

        # Create main and static folders
        self._mainPath = self._normalize_path(u'OfflineWebsite-{}'.format(
            self._conf.getTitle().decode('utf-8')))
        self._fileHandler.addDir(self._mainPath)
        self._staticPath = os.path.join(self._mainPath, "static")
        self._fileHandler.addDir(self._staticPath)
        # Add i18n js
        self._addFolderFromSrc(
            os.path.join(self._staticPath, 'js', 'indico', 'i18n'),
            os.path.join(config.getHtdocsDir(), 'js', 'indico', 'i18n'))
        # Add system icons (not referenced in HTML/CSS)
        for icon in Config.getInstance().getSystemIcons().itervalues():
            self._addFileFromSrc(
                os.path.join(self._staticPath, 'images', icon),
                os.path.join(config.getHtdocsDir(), 'images', icon))
        # IE compat files (in conditional comments so BS doesn't see them)
        for path in ie_compatibility.urls():
            self._addFileFromSrc(
                os.path.join(self._staticPath, path.lstrip('/')),
                os.path.join(config.getHtdocsDir(), path.lstrip('/')))
        # Mathjax plugins can't be discovered by parsing the HTML
        self._addFolderFromSrc(
            os.path.join(self._staticPath, 'js', 'lib', 'mathjax'),
            os.path.join(config.getHtdocsDir(), 'js', 'lib', 'mathjax'))

        # Getting all materials, static files (css, images, js and vars.js.tpl)
        self._getAllMaterial()
        self._html = self._get_static_files(self._html)

        # Specific changes
        self._create_other_pages()

        # Retrieve files that were not available in the file system (e.e. js/css from plugins)
        self._get_failed_paths()
        self._failed_paths = set()

        # Retrieve files referenced in CSS files
        self._get_css_refs()

        # A custom event CSS might reference an uploaded image so we need to check for failed paths again
        self._get_failed_paths()

        # Create overview.html file (main page for the event)
        conferenceDisplayPath = os.path.join(self._mainPath, 'overview.html')
        self._fileHandler.addNewFile(conferenceDisplayPath, self._html)

        # Creating index.html file
        self._fileHandler.addNewFile(
            'index.html', '<meta http-equiv="Refresh" content="0; url=%s">' %
            conferenceDisplayPath)

        self._fileHandler.close()
        return self._save_file(self._fileHandler.getPath(), static_site_id)

    def _get_static_files(self, html):
        config = Config.getInstance()
        soup = BeautifulSoup(html)
        images = set(_fix_url_path(x['src']) for x in soup.select('img[src]'))
        scripts = set(
            _fix_url_path(x['src']) for x in soup.select('script[src]'))
        styles = set(
            _fix_url_path(x['href'])
            for x in soup.select('link[rel="stylesheet"]'))
        for path in itertools.chain(images, scripts, styles):
            src_path = re.sub(r'#.*$', '',
                              os.path.join(config.getHtdocsDir(), path))
            dst_path = os.path.join(self._staticPath, path)
            if path in styles:
                self._css_files.add(path)
            if not os.path.isfile(src_path):
                self._failed_paths.add(path)
            elif path not in styles:
                self._addFileFromSrc(dst_path, src_path)
        for image in soup.select('img[src]'):
            image['src'] = os.path.join('static', _fix_url_path(image['src']))
        for script in soup.select('script[src]'):
            script['src'] = os.path.join('static',
                                         _fix_url_path(script['src']))
        for style in soup.select('link[rel="stylesheet"]'):
            style['href'] = os.path.join('static',
                                         _fix_url_path(style['href']))
        return str(soup)

    def _get_failed_paths(self):
        """Downloads files that were not available in the fielystem via HTTP.
        This is the only clean way to deal with static files from plugins since otherwise
        we would have to emulate RHHtdocs.
        """
        cfg = Config.getInstance()
        # If we have the embedded webserver prefer its base url since the iptables hack does
        # not work for connections from the same machine
        base_url = cfg.getBaseURL()
        for path in self._failed_paths:
            dst_path = os.path.join(self._staticPath, path)
            if not self._fileHandler.hasFile(dst_path):
                response = requests.get(os.path.join(base_url, path),
                                        verify=False)
                self._downloaded_files[dst_path] = response.content
                self._fileHandler.addNewFile(dst_path, response.content)

    def _get_css_refs(self):
        """Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
        config = Config.getInstance()
        for path in self._css_files:
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if dst_path in self._downloaded_files and not os.path.exists(
                    src_path):
                css = self._downloaded_files[dst_path]
            else:
                with open(src_path, 'rb') as f:
                    css = f.read()
            # Extract all paths inside url()
            urls = set(
                m.group('url') for m in RE_CSS_URL.finditer(css)
                if m.group('url')[0] != '#')
            for url in urls:
                orig_url = url
                url = _remove_qs(url)  # get rid of cache busters
                if url[0] == '/':
                    # make it relative and resolve '..' elements
                    url = os.path.normpath(url[1:])
                    # anything else is straightforward: the url is now relative to the htdocs folder
                    ref_src_path = os.path.join(config.getHtdocsDir(), url)
                    ref_dst_path = os.path.join(self._staticPath, url)
                    # the new url is relative to the css location
                    static_url = os.path.relpath(url, os.path.dirname(path))
                else:
                    # make the relative path absolute (note: it's most likely NOT relative to htdocs!)
                    css_abs_path = os.path.join(config.getHtdocsDir(), path)
                    # now we can combine the relative url with that path to get the proper paths of the resource
                    ref_src_path = os.path.normpath(
                        os.path.join(os.path.dirname(css_abs_path), url))
                    ref_dst_path = os.path.normpath(
                        os.path.join(self._staticPath, os.path.dirname(path),
                                     url))
                    static_url = os.path.relpath(ref_src_path,
                                                 os.path.dirname(css_abs_path))
                if not os.path.isfile(ref_src_path):
                    htdocs_relative_path = os.path.relpath(
                        ref_src_path, config.getHtdocsDir())
                    htdocs_relative_path = re.sub(r'#.*$', '',
                                                  htdocs_relative_path)
                    self._failed_paths.add(htdocs_relative_path)
                else:
                    self._addFileFromSrc(ref_dst_path, ref_src_path)
                css = css.replace(orig_url, static_url)
            self._fileHandler.addNewFile(dst_path, css)

    def _create_home(self):
        # get default/selected view
        view = self._rh._target.as_event.theme
        # if no default view was attributed, then get the configuration default
        if not view or view not in theme_settings.themes or theme_settings.themes[
                view].get('is_xml'):
            view = theme_settings.defaults[self._eventType]
        p = WPTPLStaticConferenceDisplay(self._rh, self._rh._target, view,
                                         self._eventType, self._rh._reqParams)
        self._html = p.display(**self._rh._getRequestParams())

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return secure_filename(remove_tags(path))

    def _getAllMaterial(self):
        self._addMaterialFrom(self.event, "events/conference")
        for contrib in self.event.contributions:
            self._addMaterialFrom(contrib,
                                  "agenda/%s-contribution" % contrib.id)
            for sc in contrib.subcontributions:
                self._addMaterialFrom(sc, "agenda/%s-subcontribution" % sc.id)
        for session in self.event.sessions:
            self._addMaterialFrom(session, "agenda/%s-session" % session.id)

    def _addMaterialFrom(self, target, categoryPath):
        for folder in AttachmentFolder.get_for_linked_object(
                target, preload_event=True):
            for attachment in folder.attachments:
                if attachment.type == AttachmentType.file:
                    dst_path = posixpath.join(
                        self._mainPath, "files", categoryPath,
                        "{}-{}".format(attachment.id,
                                       attachment.file.filename))
                    with attachment.file.get_local_path() as file_path:
                        self._addFileFromSrc(dst_path, file_path)

    def _addFileFromSrc(self, dstPath, srcPath):
        if not os.path.isfile(dstPath) and os.path.isfile(srcPath):
            if not self._fileHandler.hasFile(dstPath):
                newFile = open(srcPath, "rb")
                self._fileHandler.addNewFile(dstPath, newFile.read())
                newFile.close()

    def _addFolderFromSrc(self, dstPath, srcPath):
        for root, subfolders, files in os.walk(srcPath):
            for filename in files:
                src_filepath = os.path.join(root, filename)
                if root.startswith(srcPath):
                    dst_dirpath = os.path.join(dstPath,
                                               root[len(srcPath):].strip('/'))
                else:
                    dst_dirpath = dstPath
                dst_filepath = os.path.join(dst_dirpath, filename)
                self._fileHandler.addDir(dst_dirpath)
                if not self._fileHandler.hasFile(dst_filepath):
                    self._fileHandler.add(dst_filepath, src_filepath)

    def _save_file(self, srcPath, static_site_id):
        volume = HelperMaKaCInfo.getMaKaCInfoInstance().getArchivingVolume()
        path = os.path.join(Config.getInstance().getOfflineStore(), volume,
                            'offline', self._conf.getId())
        file_path = os.path.join(path, '{}.zip'.format(static_site_id))
        try:
            os.makedirs(path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        shutil.copyfile(srcPath, file_path)
        return file_path
class OfflineEventCreator(object):
    def __init__(self, rh, conf, event_type=""):
        self._rh = rh
        self._conf = conf
        self._html = ""
        self._outputFile = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._eventType = event_type
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}

    def create(self):
        config = Config.getInstance()
        self._fileHandler = ZIPFileHandler()

        # create the home page html
        self._create_home()

        # Create main and static folders
        self._mainPath = self._normalize_path(u'OfflineWebsite-{}'.format(
            self._conf.getTitle().decode('utf-8')))
        self._fileHandler.addDir(self._mainPath)
        self._staticPath = os.path.join(self._mainPath, "static")
        self._fileHandler.addDir(self._staticPath)
        # Add i18n js
        self._addFolderFromSrc(
            os.path.join(self._staticPath, 'js', 'indico', 'i18n'),
            os.path.join(config.getHtdocsDir(), 'js', 'indico', 'i18n'))
        # Add system icons (not referenced in HTML/CSS)
        for icon in Config.getInstance().getSystemIcons().itervalues():
            self._addFileFromSrc(
                os.path.join(self._staticPath, 'images', icon),
                os.path.join(config.getHtdocsDir(), 'images', icon))
        # IE compat files (in conditional comments so BS doesn't see them)
        for path in ie_compatibility.urls():
            self._addFileFromSrc(
                os.path.join(self._staticPath, path.lstrip('/')),
                os.path.join(config.getHtdocsDir(), path.lstrip('/')))

        # Getting all materials, static files (css, images, js and vars.js.tpl)
        self._getAllMaterial()
        self._html = self._get_static_files(self._html)

        # Specific changes
        self._create_other_pages()

        # Retrieve files that were not available in the file system (e.e. js/css from plugins)
        self._get_failed_paths()
        self._failed_paths = set()

        # Retrieve files referenced in CSS files
        self._get_css_refs()

        # A custom event CSS might reference an uploaded image so we need to check for failed paths again
        self._get_failed_paths()

        # Creating ConferenceDisplay.html file
        conferenceDisplayPath = os.path.join(
            self._mainPath, urlHandlers.UHConferenceDisplay.getStaticURL())
        self._fileHandler.addNewFile(conferenceDisplayPath, self._html)

        # Creating index.html file
        self._fileHandler.addNewFile(
            'index.html', '<meta http-equiv="Refresh" content="0; url=%s">' %
            conferenceDisplayPath)

        self._fileHandler.close()
        self._outputFile = self._generateZipFile(self._fileHandler.getPath())
        return self._outputFile

    def _get_static_files(self, html):
        config = Config.getInstance()
        soup = BeautifulSoup(html)
        scripts = set(
            _fix_url_path(x['src']) for x in soup.select('script[src]'))
        styles = set(
            _fix_url_path(x['href'])
            for x in soup.select('link[rel="stylesheet"]'))
        for path in itertools.chain(scripts, styles):
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if path in styles:
                self._css_files.add(path)
            if not os.path.isfile(src_path):
                self._failed_paths.add(path)
            elif path not in styles:
                self._addFileFromSrc(dst_path, src_path)
        for script in soup.select('script[src]'):
            script['src'] = os.path.join('static',
                                         _fix_url_path(script['src']))
        for style in soup.select('link[rel="stylesheet"]'):
            style['href'] = os.path.join('static',
                                         _fix_url_path(style['href']))
        return str(soup)

    def _get_failed_paths(self):
        """Downloads files that were not available in the fielystem via HTTP.
        This is the only clean way to deal with static files from plugins since otherwise
        we would have to emulate RHHtdocs.
        """
        cfg = Config.getInstance()
        # If we have the embedded webserver prefer its base url since the iptables hack does
        # not work for connections from the same machine
        base_url = cfg.getEmbeddedWebserverBaseURL() or cfg.getBaseURL()
        for path in self._failed_paths:
            dst_path = os.path.join(self._staticPath, path)
            if not self._fileHandler.hasFile(dst_path):
                response = requests.get(os.path.join(base_url, path),
                                        verify=False)
                self._downloaded_files[dst_path] = response.content
                self._fileHandler.addNewFile(dst_path, response.content)

    def _get_css_refs(self):
        """Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
        config = Config.getInstance()
        for path in self._css_files:
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if dst_path in self._downloaded_files and not os.path.exists(
                    src_path):
                css = self._downloaded_files[dst_path]
            else:
                with open(src_path, 'rb') as f:
                    css = f.read()
            # Extract all paths inside url()
            urls = set(
                m.group('url') for m in RE_CSS_URL.finditer(css)
                if m.group('url')[0] != '#')
            for url in urls:
                orig_url = url
                url = _remove_qs(url)  # get rid of cache busters
                if url[0] == '/':
                    # make it relative and resolve '..' elements
                    url = os.path.normpath(url[1:])
                    # anything else is straightforward: the url is now relative to the htdocs folder
                    ref_src_path = os.path.join(config.getHtdocsDir(), url)
                    ref_dst_path = os.path.join(self._staticPath, url)
                    # the new url is relative to the css location
                    static_url = os.path.relpath(url, os.path.dirname(path))
                else:
                    # make the relative path absolute (note: it's most likely NOT relative to htdocs!)
                    css_abs_path = os.path.join(config.getHtdocsDir(), path)
                    # now we can combine the relative url with that path to get the proper paths of the resource
                    ref_src_path = os.path.normpath(
                        os.path.join(os.path.dirname(css_abs_path), url))
                    ref_dst_path = os.path.normpath(
                        os.path.join(self._staticPath, os.path.dirname(path),
                                     url))
                    static_url = os.path.relpath(ref_src_path,
                                                 os.path.dirname(css_abs_path))
                if not os.path.isfile(ref_src_path):
                    htdocs_relative_path = os.path.relpath(
                        ref_src_path, config.getHtdocsDir())
                    self._failed_paths.add(htdocs_relative_path)
                else:
                    self._addFileFromSrc(ref_dst_path, ref_src_path)
                css = css.replace(orig_url, static_url)
            self._fileHandler.addNewFile(dst_path, css)

    def _create_home(self):
        # get default/selected view
        styleMgr = info.HelperMaKaCInfo.getMaKaCInfoInstance().getStyleManager(
        )
        view = displayMgr.ConfDisplayMgrRegistery().getDisplayMgr(
            self._rh._target).getDefaultStyle()
        # if no default view was attributed, then get the configuration default
        if view == "" or not styleMgr.existsStyle(
                view) or view in styleMgr.getXSLStyles():
            view = styleMgr.getDefaultStyleForEventType(self._eventType)
        p = WPTPLStaticConferenceDisplay(self._rh, self._rh._target, view,
                                         self._eventType, self._rh._reqParams)
        self._html = p.display(**self._rh._getRequestParams())

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return secure_filename(remove_tags(path))

    def _getAllMaterial(self):
        self._addMaterialFrom(self._conf, "events/conference")
        for contrib in self._conf.getContributionList():
            self._addMaterialFrom(contrib,
                                  "agenda/%s-contribution" % contrib.getId())
            if contrib.getSubContributionList():
                for sc in contrib.getSubContributionList():
                    self._addMaterialFrom(
                        sc, "agenda/%s-subcontribution" % sc.getId())
        for session in self._conf.getSessionList():
            self._addMaterialFrom(session,
                                  "agenda/%s-session" % session.getId())

    def _addMaterialFrom(self, target, categoryPath):
        if target.getAllMaterialList():
            for mat in target.getAllMaterialList():
                for res in mat.getResourceList():
                    if isinstance(res, conference.LocalFile):
                        dstPath = os.path.join(
                            self._mainPath, "files", categoryPath, mat.getId(),
                            res.getId() + "-" + res.getName())
                        self._addFileFromSrc(dstPath, res.getFilePath())

    def _addFileFromSrc(self, dstPath, srcPath):
        if not os.path.isfile(dstPath) and os.path.isfile(srcPath):
            if not self._fileHandler.hasFile(dstPath):
                newFile = open(srcPath, "rb")
                self._fileHandler.addNewFile(dstPath, newFile.read())
                newFile.close()

    def _addFolderFromSrc(self, dstPath, srcPath):
        for root, subfolders, files in os.walk(srcPath):
            for filename in files:
                src_filepath = os.path.join(root, filename)
                dst_dirpath = os.path.join(dstPath, root.strip(srcPath))
                dst_filepath = os.path.join(dst_dirpath, filename)
                self._fileHandler.addDir(dst_dirpath)
                if not self._fileHandler.hasFile(dst_filepath):
                    self._fileHandler.add(dst_filepath, src_filepath)

    def _generateZipFile(self, srcPath):
        repo = OfflineRepository.getRepositoryFromDB()
        filename = os.path.basename(srcPath) + ".zip"
        fd = LocalFile()
        fd.setFilePath(srcPath)
        fd.setFileName(filename)
        repo.storeFile(fd, self._conf.getId())
        return fd
Esempio n. 4
0
class OfflineEventCreator(object):
    def __init__(self, rh, conf, event_type=""):
        self._rh = rh
        self._conf = conf
        self._html = ""
        self._outputFile = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._eventType = event_type
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}

    def create(self):
        config = Config.getInstance()
        self._fileHandler = ZIPFileHandler()

        # create the home page html
        self._create_home()

        # Create main and static folders
        self._mainPath = "OfflineWebsite-%s" % self._normalize_path(self._conf.getTitle())
        self._fileHandler.addDir(self._mainPath)
        self._staticPath = os.path.join(self._mainPath, "static")
        self._fileHandler.addDir(self._staticPath)
        # Add i18n js
        self._addFolderFromSrc(
            os.path.join(self._staticPath, "js", "indico", "i18n"),
            os.path.join(config.getHtdocsDir(), "js", "indico", "i18n"),
        )
        # Add system icons (not referenced in HTML/CSS)
        for icon in Config.getInstance().getSystemIcons().itervalues():
            self._addFileFromSrc(
                os.path.join(self._staticPath, "images", icon), os.path.join(config.getHtdocsDir(), "images", icon)
            )
        # IE compat files (in conditional comments so BS doesn't see them)
        for path in ie_compatibility.urls():
            self._addFileFromSrc(
                os.path.join(self._staticPath, path.lstrip("/")), os.path.join(config.getHtdocsDir(), path.lstrip("/"))
            )

        # Getting all materials, static files (css, images, js and vars.js.tpl)
        self._getAllMaterial()
        self._html = self._get_static_files(self._html)

        # Specific changes
        self._create_other_pages()

        # Retrieve files that were not available in the file system (e.e. js/css from plugins)
        self._get_failed_paths()
        self._failed_paths = set()

        # Retrieve files referenced in CSS files
        self._get_css_refs()

        # A custom event CSS might reference an uploaded image so we need to check for failed paths again
        self._get_failed_paths()

        # Creating ConferenceDisplay.html file
        conferenceDisplayPath = os.path.join(self._mainPath, urlHandlers.UHConferenceDisplay.getStaticURL())
        self._fileHandler.addNewFile(conferenceDisplayPath, self._html)

        # Creating index.html file
        self._fileHandler.addNewFile(
            "index.html", '<meta http-equiv="Refresh" content="0; url=%s">' % conferenceDisplayPath
        )

        self._fileHandler.close()
        self._outputFile = self._generateZipFile(self._fileHandler.getPath())
        return self._outputFile

    def _get_static_files(self, html):
        config = Config.getInstance()
        soup = BeautifulSoup(html)
        scripts = set(_fix_url_path(x["src"]) for x in soup.select("script[src]"))
        styles = set(_fix_url_path(x["href"]) for x in soup.select('link[rel="stylesheet"]'))
        for path in itertools.chain(scripts, styles):
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if path in styles:
                self._css_files.add(path)
            if not os.path.isfile(src_path):
                self._failed_paths.add(path)
            elif path not in styles:
                self._addFileFromSrc(dst_path, src_path)
        for script in soup.select("script[src]"):
            script["src"] = os.path.join("static", _fix_url_path(script["src"]))
        for style in soup.select('link[rel="stylesheet"]'):
            style["href"] = os.path.join("static", _fix_url_path(style["href"]))
        return str(soup)

    def _get_failed_paths(self):
        """Downloads files that were not available in the fielystem via HTTP.
        This is the only clean way to deal with static files from plugins since otherwise
        we would have to emulate RHHtdocs.
        """
        cfg = Config.getInstance()
        # If we have the embedded webserver prefer its base url since the iptables hack does
        # not work for connections from the same machine
        base_url = cfg.getEmbeddedWebserverBaseURL() or cfg.getBaseURL()
        for path in self._failed_paths:
            dst_path = os.path.join(self._staticPath, path)
            if not self._fileHandler.hasFile(dst_path):
                response = requests.get(os.path.join(base_url, path), verify=False)
                self._downloaded_files[dst_path] = response.content
                self._fileHandler.addNewFile(dst_path, response.content)

    def _get_css_refs(self):
        """Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
        config = Config.getInstance()
        for path in self._css_files:
            src_path = os.path.join(config.getHtdocsDir(), path)
            dst_path = os.path.join(self._staticPath, path)
            if dst_path in self._downloaded_files and not os.path.exists(src_path):
                css = self._downloaded_files[dst_path]
            else:
                with open(src_path, "rb") as f:
                    css = f.read()
            # Extract all paths inside url()
            urls = set(m.group("url") for m in RE_CSS_URL.finditer(css) if m.group("url")[0] != "#")
            for url in urls:
                orig_url = url
                url = _remove_qs(url)  # get rid of cache busters
                if url[0] == "/":
                    # make it relative and resolve '..' elements
                    url = os.path.normpath(url[1:])
                    # anything else is straightforward: the url is now relative to the htdocs folder
                    ref_src_path = os.path.join(config.getHtdocsDir(), url)
                    ref_dst_path = os.path.join(self._staticPath, url)
                    # the new url is relative to the css location
                    static_url = os.path.relpath(url, os.path.dirname(path))
                else:
                    # make the relative path absolute (note: it's most likely NOT relative to htdocs!)
                    css_abs_path = os.path.join(config.getHtdocsDir(), path)
                    # now we can combine the relative url with that path to get the proper paths of the resource
                    ref_src_path = os.path.normpath(os.path.join(os.path.dirname(css_abs_path), url))
                    ref_dst_path = os.path.normpath(os.path.join(self._staticPath, os.path.dirname(path), url))
                    static_url = os.path.relpath(ref_src_path, os.path.dirname(css_abs_path))
                if not os.path.isfile(ref_src_path):
                    htdocs_relative_path = os.path.relpath(ref_src_path, config.getHtdocsDir())
                    self._failed_paths.add(htdocs_relative_path)
                else:
                    self._addFileFromSrc(ref_dst_path, ref_src_path)
                css = css.replace(orig_url, static_url)
            self._fileHandler.addNewFile(dst_path, css)

    def _create_home(self):
        # get default/selected view
        styleMgr = info.HelperMaKaCInfo.getMaKaCInfoInstance().getStyleManager()
        view = displayMgr.ConfDisplayMgrRegistery().getDisplayMgr(self._rh._target).getDefaultStyle()
        # if no default view was attributed, then get the configuration default
        if view == "" or not styleMgr.existsStyle(view) or view in styleMgr.getXSLStyles():
            view = styleMgr.getDefaultStyleForEventType(self._eventType)
        p = WPTPLStaticConferenceDisplay(self._rh, self._rh._target, view, self._eventType, self._rh._reqParams)
        self._html = p.display(**self._rh._getRequestParams())

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return path.translate(string.maketrans(' /:()*?<>|"', "___________"))

    def _getAllMaterial(self):
        self._addMaterialFrom(self._conf, "events/conference")
        for contrib in self._conf.getContributionList():
            self._addMaterialFrom(contrib, "agenda/%s-contribution" % contrib.getId())
            if contrib.getSubContributionList():
                for sc in contrib.getSubContributionList():
                    self._addMaterialFrom(sc, "agenda/%s-subcontribution" % sc.getId())
        for session in self._conf.getSessionList():
            self._addMaterialFrom(session, "agenda/%s-session" % session.getId())

    def _addMaterialFrom(self, target, categoryPath):
        if target.getAllMaterialList():
            for mat in target.getAllMaterialList():
                for res in mat.getResourceList():
                    if isinstance(res, conference.LocalFile):
                        dstPath = os.path.join(
                            self._mainPath, "files", categoryPath, mat.getId(), res.getId() + "-" + res.getName()
                        )
                        self._addFileFromSrc(dstPath, res.getFilePath())

    def _addFileFromSrc(self, dstPath, srcPath):
        if not os.path.isfile(dstPath) and os.path.isfile(srcPath):
            if not self._fileHandler.hasFile(dstPath):
                newFile = open(srcPath, "rb")
                self._fileHandler.addNewFile(dstPath, newFile.read())
                newFile.close()

    def _addFolderFromSrc(self, dstPath, srcPath):
        for root, subfolders, files in os.walk(srcPath):
            for filename in files:
                src_filepath = os.path.join(root, filename)
                dst_dirpath = os.path.join(dstPath, root.strip(srcPath))
                dst_filepath = os.path.join(dst_dirpath, filename)
                self._fileHandler.addDir(dst_dirpath)
                if not self._fileHandler.hasFile(dst_filepath):
                    self._fileHandler.add(dst_filepath, src_filepath)

    def _generateZipFile(self, srcPath):
        repo = OfflineRepository.getRepositoryFromDB()
        filename = os.path.basename(srcPath) + ".zip"
        fd = LocalFile()
        fd.setFilePath(srcPath)
        fd.setFileName(filename)
        repo.storeFile(fd, self._conf.getId())
        return fd