コード例 #1
0
ファイル: apps.py プロジェクト: Eric89GXL/api
    def post(self):
        """Create a new App."""
        # this handles receive and writing the file
        # but the the json validation and database is handled by util.
        apps_path = self.app.config['apps_path']
        if not apps_path:
            self.abort(503, 'POST api/apps unavailable. apps_path not defined')
        if self.public_request:  # TODO: how to handle auth during bootstrap?
            self.abort(403, 'must be logged in to upload apps')

        app_meta = None
        with tempfile.TemporaryDirectory(prefix='.tmp', dir=apps_path) as tempdir_path:
            hash_ = hashlib.sha1()
            app_temp = os.path.join(tempdir_path, 'temp')
            with open(app_temp, 'wb') as fd:
                for chunk in iter(lambda: self.request.body_file.read(2**20), ''):
                    hash_.update(chunk)
                    fd.write(chunk)
            if hash_.hexdigest() != self.request.headers['Content-MD5']:
                self.abort(400, 'Content-MD5 mismatch.')  # sha1
            if not tarfile.is_tarfile(app_temp):
                self.abort(415, 'Only tar files are accepted.')
            with tarfile.open(app_temp) as tf:
                for ti in tf:
                    if ti.name.endswith('description.json'):
                        app_meta = json.load(tf.extractfile(ti))
                        break
            if not app_meta:
                self.abort(415, 'application tar does not contain description.json')
            try:
                jsonschema.validate(app_meta, APP_SCHEMA)
            except (ValueError, jsonschema.ValidationError) as e:
                self.abort(400, str(e))
            util.insert_app(self.app.db, app_temp, apps_path, app_meta=app_meta)  # pass meta info, prevent re-reading
            log.debug('Recieved App: %s' % app_meta.get('_id'))
コード例 #2
0
ファイル: bootstrap.py プロジェクト: Eric89GXL/api
def appsinit(args):
    """Upload an app."""
    import tarfile
    db_client = connect_db(args.db_uri)
    db = db_client.get_default_database()
    app_tgz = args.app_tgz + '.tgz'
    if not os.path.exists(app_tgz):
        with tarfile.open(app_tgz, 'w:gz', compresslevel=6) as tf:
            tf.add(args.app_tgz, arcname=os.path.basename(args.app_tgz))
    util.insert_app(db, app_tgz, args.apps_path)
コード例 #3
0
def appsinit(args):
    """Upload an app."""
    import tarfile
    db_client = connect_db(args.db_uri)
    db = db_client.get_default_database()
    app_tgz = args.app_tgz + '.tgz'
    if not os.path.exists(app_tgz):
        with tarfile.open(app_tgz, 'w:gz', compresslevel=6) as tf:
            tf.add(args.app_tgz, arcname=os.path.basename(args.app_tgz))
    util.insert_app(db, app_tgz, args.apps_path)
コード例 #4
0
ファイル: apps.py プロジェクト: larsoner/api
    def post(self):
        """Create a new App."""
        # this handles receive and writing the file
        # but the the json validation and database is handled by util.
        apps_path = self.app.config['apps_path']
        if not apps_path:
            self.abort(503, 'POST api/apps unavailable. apps_path not defined')
        if self.public_request:  # TODO: how to handle auth during bootstrap?
            self.abort(403, 'must be logged in to upload apps')

        app_meta = None
        with tempfile.TemporaryDirectory(prefix='.tmp',
                                         dir=apps_path) as tempdir_path:
            hash_ = hashlib.sha1()
            app_temp = os.path.join(tempdir_path, 'temp')
            with open(app_temp, 'wb') as fd:
                for chunk in iter(lambda: self.request.body_file.read(2**20),
                                  ''):
                    hash_.update(chunk)
                    fd.write(chunk)
            if hash_.hexdigest() != self.request.headers['Content-MD5']:
                self.abort(400, 'Content-MD5 mismatch.')  # sha1
            if not tarfile.is_tarfile(app_temp):
                self.abort(415, 'Only tar files are accepted.')
            with tarfile.open(app_temp) as tf:
                for ti in tf:
                    if ti.name.endswith('description.json'):
                        app_meta = json.load(tf.extractfile(ti))
                        break
            if not app_meta:
                self.abort(
                    415, 'application tar does not contain description.json')
            try:
                jsonschema.validate(app_meta, APP_SCHEMA)
            except (ValueError, jsonschema.ValidationError) as e:
                self.abort(400, str(e))
            util.insert_app(
                self.app.db, app_temp, apps_path,
                app_meta=app_meta)  # pass meta info, prevent re-reading
            log.debug('Recieved App: %s' % app_meta.get('_id'))