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.event.title))
        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))
        # 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._fileHandler.getPath()
Esempio n. 2
0
    def create(self):
        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.event.title))
        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(self._htdocs_dir, 'js', 'indico', 'i18n'))
        # Mathjax plugins can't be discovered by parsing the HTML
        self._addFolderFromSrc(os.path.join(self._staticPath, 'js', 'lib', 'mathjax'),
                               os.path.join(self._htdocs_dir, '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._fileHandler.getPath()
Esempio n. 3
0
class OfflineEventCreator(object):
    def __init__(self, rh, event):
        self._rh = rh
        self.event = event
        self._display_tz = self.event.display_tzinfo.zone
        self._html = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}
        self._htdocs_dir = os.path.join(get_root_path('indico'), 'htdocs')

    def create(self):
        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.event.title))
        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(self._htdocs_dir, 'js', 'indico', 'i18n'))
        # Mathjax plugins can't be discovered by parsing the HTML
        self._addFolderFromSrc(os.path.join(self._staticPath, 'js', 'lib', 'mathjax'),
                               os.path.join(self._htdocs_dir, '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._fileHandler.getPath()

    def _static_url_to_path(self, url):
        match = re.match(r'^static/assets/(core|plugin-(?P<plugin>[^/]+)|theme-(?P<theme>[^/]+))/(?P<path>.+)$', url)
        if match is not None:
            path = os.path.join(config.ASSETS_DIR, get_asset_path(**match.groupdict()))
        else:
            path = os.path.join(self._htdocs_dir, url)
        return re.sub(r'#.*$', '', path)

    def _get_static_files(self, html):
        soup = BeautifulSoup(html, 'lxml')
        images = set(_fix_url_path(x['src']) for x in soup.select('img[src]'))
        images |= set(_fix_url_path(x['href']) for x in soup.select('link[rel="shortcut icon"]'))
        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 = self._static_url_to_path(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.
        """
        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(config.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"""
        for path in self._css_files:
            src_path = self._static_url_to_path(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(self._htdocs_dir, 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(self._htdocs_dir, 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, self._htdocs_dir)
                    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):
        self._html = WPStaticSimpleEventDisplay(self._rh, self.event, self.event.theme).display()

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return secure_filename(strip_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)
Esempio n. 4
0
class OfflineEventCreator(object):
    def __init__(self, rh, event):
        self._rh = rh
        self.event = event
        self._display_tz = self.event.display_tzinfo.zone
        self._html = ""
        self._fileHandler = None
        self._mainPath = ""
        self._staticPath = ""
        self._failed_paths = set()
        self._css_files = set()
        self._downloaded_files = {}
        self._htdocs_dir = os.path.join(get_root_path('indico'), 'htdocs')

    def create(self):
        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.event.title))
        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(self._htdocs_dir, 'js', 'indico', 'i18n'))
        # Mathjax plugins can't be discovered by parsing the HTML
        self._addFolderFromSrc(
            os.path.join(self._staticPath, 'js', 'lib', 'mathjax'),
            os.path.join(self._htdocs_dir, '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._fileHandler.getPath()

    def _static_url_to_path(self, url):
        match = re.match(
            r'^static/assets/(core|plugin-(?P<plugin>[^/]+)|theme-(?P<theme>[^/]+))/(?P<path>.+)$',
            url)
        if match is not None:
            path = os.path.join(config.ASSETS_DIR,
                                get_asset_path(**match.groupdict()))
        else:
            path = os.path.join(self._htdocs_dir, url)
        return re.sub(r'#.*$', '', path)

    def _get_static_files(self, html):
        soup = BeautifulSoup(html, 'lxml')
        images = set(_fix_url_path(x['src']) for x in soup.select('img[src]'))
        images |= set(
            _fix_url_path(x['href'])
            for x in soup.select('link[rel="shortcut icon"]'))
        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 = self._static_url_to_path(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.
        """
        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(config.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"""
        for path in self._css_files:
            src_path = self._static_url_to_path(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(self._htdocs_dir, 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(self._htdocs_dir, 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, self._htdocs_dir)
                    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):
        self._html = WPStaticSimpleEventDisplay(self._rh, self.event,
                                                self.event.theme).display()

    def _create_other_pages(self):
        pass

    def _normalize_path(self, path):
        return secure_filename(strip_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)