Exemple #1
0
        def __init__(self, cms):
            self.__cms = cms
            self.__dispatcher = Dispatcher()

            # Set the default location for file-based sessions
            sconf = session.config
            using_file_sessions = (sconf.get("session.type") == "file")
            using_dbm_sessions = (sconf.get("session.type") == "dbm")
            lock_dir_missing = (not using_file_sessions
                                and not sconf.get("session.lock_dir"))
            data_dir_missing = (
                (using_file_sessions or
                 (using_dbm_sessions and not sconf.get("session.dbm_dir")))
                and not sconf.get("session.data_dir"))

            if lock_dir_missing or data_dir_missing:

                session_path = app.path("sessions")
                if not os.path.exists(session_path):
                    os.mkdir(session_path)

                if lock_dir_missing:
                    sconf["session.lock_dir"] = session_path

                if data_dir_missing:
                    sconf["session.data_dir"] = session_path

            if not sconf.get("session.secret"):

                session_key_path = app.path(".session_key")
                if os.path.exists(session_key_path):
                    with open(session_key_path, "r") as session_key_file:
                        session_key = session_key_file.readline()
                else:
                    with open(session_key_path, "w") as session_key_file:
                        session_key = sha("".join(
                            choice(ascii_letters)
                            for i in range(10))).hexdigest()
                        session_key_file.write(session_key)

                sconf["session.secret"] = session_key

            # Create the folders for uploaded files
            upload_path = app.path("upload")
            if not os.path.exists(upload_path):
                os.mkdir(upload_path)

            temp_path = app.path("upload", "temp")
            if not os.path.exists(temp_path):
                os.mkdir(temp_path)

            async_uploader.temp_folder = temp_path
Exemple #2
0
def setup_package():

    if not get_selenium_enabled():
        return

    # Create a temporary folder to hold site files
    _site_temp_path = mkdtemp()
    app.root = _site_temp_path

    # Set up a temporary database
    datastore.storage = FileStorage(app.path("testdb.fs"))

    # Initialize site content before testing
    site_initializer = SiteInitializer()
    site_initializer.initialize(admin_email, admin_password, site_languages)
    datastore.commit()

    # Configure the site's webserver
    hostname, port = get_selenium_site_address()
    cherrypy.config.update({
        "log.screen": False,
        "server.socket_host": hostname,
        "server.socket_port": port,
        "engine.autoreload.on": False
    })

    # Configure the application
    cms = CMSController()
    cms.closing_item_requires_confirmation = False

    # Launch the site's webserver on another thread
    cms.run(block=False)
Exemple #3
0
def request_encoding(file, encoder):

    if isinstance(encoder, basestring):
        encoder = AudioEncoder.require_instance(identifier=encoder)

    dest = get_audio_cache_dest(file, encoder)

    # The file hasn't been generated yet
    if not os.path.exists(dest):

        request = None
        request_key = (file.id, encoder.identifier)

        with _cache_lock:
            request = _cache_ongoing_requests.get(request_key)

            # Another thread is already requesting this file encoding: wait
            # for it to finish
            if request is not None:
                new_request = False
            # No other thread is generating the requested file/encoding
            # combination at present: generate it
            else:
                request = Event()
                _cache_ongoing_requests[request_key] = request
                new_request = True

                # Make sure the cache folder for the indicated file exists
                folder = app.path("audio-cache", str(file.id))

                if not os.path.exists(folder):
                    os.mkdir(folder)

        if new_request:
            try:
                encoder.encode(file, dest)
                with _cache_lock:
                    _create_symlink(file, dest)
                    request.set()
            finally:
                try:
                    del _cache_ongoing_requests[request_key]
                except KeyError:
                    pass
        else:
            request.wait()

    return dest
    def _export(self, context, status_tracker=None):
        StaticSiteDestination._export(self,
                                      context,
                                      status_tracker=status_tracker)

        # Close the zip file before export it to the CMS
        context["zip_file"].close()
        upload_path = app.path("upload")

        with changeset_context(get_current_user()):
            file = File.from_path(context["temp_file"],
                                  upload_path,
                                  languages=[get_language()])
            file.title = "export-%s" % date.today().strftime("%Y%m%d")
            file.file_name = file.title + ".zip"
            file.insert()
            context.update(file=file)
Exemple #5
0
def _create_symlink(file, dest):
    """Create a symbolic link for static publication of an encoded file."""

    if not hasattr(os, "symlink"):
        return

    # Only create static publication links for public content
    anonymous = User.require_instance(qname="woost.anonymous_user")
    if not anonymous.has_permission(ReadPermission, target=file):
        return

    static_folder = app.path("static", "audio", str(file.id))
    static_link = os.path.join(static_folder, os.path.basename(dest))

    if not os.path.lexists(static_link):

        # Make sure the containing folder exists
        if not os.path.exists(static_folder):
            os.mkdir(static_folder)

        os.symlink(dest, static_link)
Exemple #6
0
def clear_image_cache(item=None, factory=None):

    if not app.root:
        return

    if debug:
        from cocktail.styled import styled
        print styled("Clearing image cache", "red"),

        if item:
            print styled("Item:", "light_gray"),
            print styled(item, "red", style="bold"),

        if factory:
            print styled("Factory:", "light_gray"),
            print styled(factory, "red", style="bold"),

        print

    # Remove the full cache
    if item is None and factory is None:
        _remove_dir_contents(app.path("image-cache"))
        _remove_dir_contents(app.path("static", "images"))

    # Selective drop: per item and/or factory
    else:
        paths = []

        if item is not None:
            paths.append(app.path("image-cache", str(item.id)))
            paths.append(app.path("static", "images", str(item.id)))
        else:
            for base in (app.path("image-cache"), app.path("static",
                                                           "images")):
                for item in os.listdir(base):
                    path = os.path.join(base, item)
                    if os.path.isdir(path):
                        paths.append(path)

        if factory is None:
            pattern = None
        else:
            pattern = factory + ".*"

        for path in paths:
            _remove_dir_contents(path, pattern)
    def submit(self):

        self.imported_files = []

        file = self.form_data["upload"]["file"]
        zip_file = ZipFile(file)

        upload_path = app.path("upload")
        temp_dir = mkdtemp()

        try:
            with changeset_context(get_current_user()):
                for file_path in zip_file.namelist():

                    file_name = os.path.basename(file_path)

                    # Skip directories
                    if not file_name:
                        continue

                    source = zip_file.open(file_path)
                    temp_file = os.path.join(temp_dir, file_name)
                    dest = open(temp_file, "wb")
                    copyfileobj(source, dest)
                    source.close()
                    dest.close()

                    try:
                        imported_file = File.from_path(temp_file, upload_path)
                        imported_file.insert()
                        self.imported_files.append(imported_file)
                    finally:
                        os.remove(temp_file)

            datastore.commit()
        finally:
            os.rmdir(temp_dir)
Exemple #8
0
def clear_audio_cache(item=None, encoder_id=None):

    if debug:
        from cocktail.styled import styled
        print styled("Clearing audio cache", "red"),

        if item:
            print styled("Item:", "light_gray"),
            print styled(item, "red", style="bold"),

        if encoder_id:
            print styled("Encoder:", "light_gray"),
            print styled(encoder_id, "red", style="bold"),

        print

    # Remove the full cache
    if item is None and encoder_id is None:
        _remove_dir_contents(app.path("audio-cache"))
        _remove_dir_contents(app.path("static", "audio"))

    # Selective drop: per item and/or encoder
    else:
        paths = []

        if item is not None:
            paths.append(app.path("audio-cache", str(item.id)))
            paths.append(app.path("static", "audio", str(item.id)))
        else:
            for base in (app.path("audio-cache"), app.path("static", "audio")):
                for item in os.listdir(base):
                    path = os.path.join(base, item)
                    if os.path.isdir(path):
                        paths.append(path)

        if encoder_id is None:
            pattern = None
        else:
            pattern = encoder_id + ".*"

        for path in paths:
            _remove_dir_contents(path, pattern)
Exemple #9
0
def get_audio_cache_dest(file, encoder):
    return app.path("audio-cache", str(file.id),
                    encoder.identifier + encoder.extension)
Exemple #10
0
 def file_path(self):
     return app.path("upload", str(self.id))
Exemple #11
0
def get_client():
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        app.path("ga-client-secrets.json"), SCOPES)
    return build("analyticsreporting", "v4", credentials=credentials)
Exemple #12
0
 def snapshot_path(self):
     return app.path("snapshots", str(self.id))
 def execute_in_subprocess(self):
     script = app.path("scripts", "staticpub.py")
     return subprocess.Popen([
         os.path.join(sys.prefix, "bin", "python"), script, "export",
         f"export:{self.id}"
     ])
Exemple #14
0
 def temp_file_path(self):
     return app.path(
         "upload", "temp",
         "%s-%s-%s" % (self.item.id, session.id, self.stack.to_param()))
Exemple #15
0
def require_rendering(item, factory=None, format=None, parameters=None):

    ext = None

    if factory is None:
        factory = ImageFactory.require_instance(identifier="default")

    if format is None:
        if not isinstance(item, type):
            ext = getattr(item, "file_extension", None)

            if ext is not None:
                format = formats_by_extension.get(ext.lstrip(".").lower())
                if format is None:
                    ext = None

    elif format not in mime_types_by_format:
        raise BadRenderingRequest("Invalid image format: %s" % format)

    if format is None:
        format = default_format

    if ext is None:
        ext = extensions_by_format[format]

    identifier = factory.identifier or "factory%d" % factory.id
    file_name = "%s.%s" % (identifier, ext)
    item_id = item.full_name if isinstance(item, type) else str(item.id)

    # If the image hasn't been generated yet, do so and store it in the
    # application's image cache
    image_cache_file = app.path("image-cache", item_id, file_name)

    if not os.path.exists(image_cache_file):

        # Generate the file
        image = factory.render(item)

        if not image:
            raise RenderError("Can't render %s" % item)

        # Store the generated image in the image cache
        try:
            os.mkdir(app.path("image-cache", item_id))
        except OSError:
            pass

        if isinstance(image, basestring):
            try:
                os.remove(image_cache_file)
            except OSError:
                pass

            if hasattr(os, "symlink"):
                os.symlink(image, image_cache_file)
            else:
                copy(image, image_cache_file)
        else:
            if format == 'JPEG' and image.mode not in ('RGB', 'RGBA'):
                image = image.convert('RGBA')
            elif format == 'PNG' and image.mode == "CMYK":
                image = image.convert('RGBA')
            image.save(image_cache_file, format)

    # If the image is accessible to anonymous users, create a link in the
    # application's static content folder (further requests will be served
    # by the web server, no questions asked).
    if hasattr(os, "symlink"):
        static_publication_link = \
            app.path("static", "images", item_id, file_name)

        if not os.path.lexists(static_publication_link):
            anonymous = User.require_instance(qname="woost.anonymous_user")

            if anonymous.has_permission(RenderPermission,
                                        target=item,
                                        image_factory=factory):
                try:
                    os.mkdir(app.path("static", "images", item_id))
                except OSError:
                    pass
                os.symlink(image_cache_file, static_publication_link)

    return image_cache_file
Exemple #16
0
def create_export_script():
    script_path = app.path("scripts", "staticpub.py")
    with open(script_path, "w") as script_file:
        script_file.write(EXPORT_SCRIPT_TEMPLATE % {"package": app.package})
Exemple #17
0
def get_links(file):    
    return [
        app.path("static", app.url_resolver.get_path(file, language))
        for language in (file.translations or [None])
    ]
 def create_exporter(self) -> Exporter:
     folder = self.zip_folder or app.path("x-staticpub-zip-files")
     os.makedirs(folder, exist_ok=True)
     filename = os.path.join(folder, f"{self.export.id}.zip")
     self.export.zip_path = filename
     return super().create_exporter(filename=filename)