示例#1
0
 def test_get_format(self):
     fl = FileStorage(filename='filename.wav')
     self.assertEqual(get_format(fl),'.wav')
     fl.filename = 'sound.mp3-.wav.ogg'
     self.assertEqual(get_format(fl),'.ogg')
     fl.filename = 'name.AVR'
     self.assertEqual(get_format(fl),'.avr')
     fl.filename = ''
     self.assertIsNone(get_format(fl))
     fl.filename = None
     self.assertIsNone(get_format(fl))
     fl.filename = {}
     self.assertIsNone(get_format(fl))
示例#2
0
 def create_zipfile_storage(
     opened_file: BufferedReader, filename: str,
 ) -> FileStorage:
     spooled = SpooledTemporaryFile()
     spooled.write(opened_file.read())
     zip_file_storage = FileStorage(spooled)
     zip_file_storage.filename = filename
     opened_file.seek(0)
     return zip_file_storage
示例#3
0
def save_file(file: FileStorage):
    """
    Save a file to the file system using the Flask-Uploads config.
    :param file: the file to save to the file system
    :return: the UploadFile object representing the file that was saved
    """
    orig_filename, file_extension = os.path.splitext(file.filename)

    # Give file a new randomly generated filename
    file.filename = _generate_file_name(file_extension=file_extension)
    saved_filename = upload_set_photos.save(file)
    url = upload_set_photos.url(saved_filename)
    upload_file = UploadFile(filename=saved_filename, url=url)
    return upload_file
示例#4
0
    def _save_file_on_disk(cls, f: FileStorage) -> str:
        """
        Save file to temp directory and compute its hash at the same stream

        Args:
            f: FileStorage - file to be stored
        Returns:
            str - computed hash
        """

        f.filename = secure_filename(f.filename)
        hash_instance = HASHING_METHOD()
        hash_instance.update(f.filename.encode('utf-8'))
        temp_path = os.path.join(cls.TEMP, f.filename)
        with open(temp_path, "wb", buffering=READING_FILE_BUF_SIZE, closefd=True) as out_file:

            while True:
                data = f.stream.read(READING_FILE_BUF_SIZE)
                if not data:
                    break
                hash_instance.update(data)
                out_file.write(data)

        return hash_instance.hexdigest()
示例#5
0
 def create_zipfile_storage(self):
     spooled = SpooledTemporaryFile()
     spooled.write(self.zipfile_content)
     zip_file_storage = FileStorage(spooled)
     zip_file_storage.filename = DOWNLOAD_FILE.rpartition(os.path.sep)[-1]
     return zip_file_storage
示例#6
0
 def create_zipfile_storage(self):
     spooled = SpooledTemporaryFile()
     spooled.write(self.zipfile_file.read())
     zip_file_storage = FileStorage(spooled)
     zip_file_storage.filename = self.ZIP_NAME
     return zip_file_storage
示例#7
0
def build_module_locally(
    local_build_nsvs=None,
    yaml_file=None,
    srpms=None,
    stream=None,
    skiptests=False,
    default_streams=None,
    offline=False,
    platform_repofiles=None,
    platform_id=None,
    log_debug=False,
):
    """ Performs local module build using Mock
    """
    # if debug is not specified, set log level of console to INFO
    if not log_debug:
        for handler in logging.getLogger().handlers:
            if isinstance(handler, logging.StreamHandler):
                handler.setLevel(logging.INFO)

    if "SERVER_NAME" not in app.config or not app.config["SERVER_NAME"]:
        app.config["SERVER_NAME"] = "localhost"

        if conf.resolver == "db":
            raise ValueError(
                "Please set RESOLVER to 'mbs' in your configuration for local builds."
            )

    conf.set_item("system", "mock")
    conf.set_item("base_module_repofiles", platform_repofiles)

    # Use our own local SQLite3 database.
    confdir = os.path.abspath(os.getcwd())
    dbdir = \
        os.path.abspath(os.path.join(confdir, "..")) if confdir.endswith("conf") else confdir
    dbpath = "/{0}".format(os.path.join(dbdir, ".mbs_local_build.db"))
    dburi = "sqlite://" + dbpath
    app.config["SQLALCHEMY_DATABASE_URI"] = dburi
    conf.set_item("sqlalchemy_database_uri", dburi)
    if os.path.exists(dbpath):
        os.remove(dbpath)

    db.create_all()
    # Reconfigure the backend database session registry to use the new the database location
    db_session.remove()
    db_session.configure(bind=db.session.bind)

    params = {
        "local_build": True,
        "default_streams": dict(ns.split(":") for ns in default_streams)
    }
    if srpms:
        params["srpms"] = srpms

    username = getpass.getuser()
    if not yaml_file or not yaml_file.endswith(".yaml"):
        raise IOError("Provided modulemd file is not a yaml file.")

    yaml_file_path = os.path.abspath(yaml_file)

    if offline:
        import_builds_from_local_dnf_repos(platform_id)
    load_local_builds(local_build_nsvs)

    with open(yaml_file_path) as fd:
        filename = os.path.basename(yaml_file)
        handle = FileStorage(fd)
        handle.filename = filename
        try:
            module_builds = submit_module_build_from_yaml(db_session,
                                                          username,
                                                          handle,
                                                          params,
                                                          stream=str(stream),
                                                          skiptests=skiptests)
        except StreamAmbigous as e:
            logging.error(str(e))
            logging.error(
                "Use '-s module_name:module_stream' to choose the stream")
            return

        module_build_ids = [build.id for build in module_builds]

    module_build_service.scheduler.local.main(module_build_ids)

    has_failed_module = db_session.query(models.ModuleBuild).filter(
        models.ModuleBuild.id.in_(module_build_ids),
        models.ModuleBuild.state == models.BUILD_STATES["failed"],
    ).count() > 0

    if has_failed_module:
        raise RuntimeError("Module build failed")
示例#8
0
 def get_file_stream(cls, filename):
     file: FileStorage = None
     file_stream = open(filename, 'rb')
     file = FileStorage(file_stream)
     file.filename = Path(file.filename).parts[-1]
     return file
示例#9
0
def get_file_storage(fp, filename):
    filestorage =  FileStorage(fp)
    filestorage.filename = filename
    return filestorage
 def saveImage(self, folder: str, file: FileStorage) -> None:
     file.filename = file.filename.lower()
     file.save(os.path.join(folder, file.filename))