示例#1
0
    def get(self, id_):
        '''
        Get a file identified by ``id_``.

        :status 200: ok
        :status 401: authentication required
        :status 404: no file with that ID
        '''

        file_ = g.db.query(File).filter(File.id == id_).first()
        data_dir = get_path('data')
        cache_timeout = 0 if g.debug else None

        if file_ is None:
            raise NotFound('No file exists with id={}'.format(id_))

        if file_.mime == 'application/zip':
            return send_from_directory(
                data_dir,
                file_.relpath(),
                mimetype=file_.mime,
                as_attachment=True,
                attachment_filename=file_.name,
                cache_timeout=cache_timeout
            )
        else:
            return send_from_directory(
                data_dir,
                file_.relpath(),
                mimetype=file_.mime,
                cache_timeout=cache_timeout
            )
示例#2
0
    def _agnostic_bootstrap(self, config):
        ''' Bootstrap the Agnostic migrations system. '''

        env = {
            'AGNOSTIC_TYPE': 'postgres',
            'AGNOSTIC_HOST': config.get('database', 'host'),
            'AGNOSTIC_USER': config.get('database', 'super_username'),
            'AGNOSTIC_PASSWORD': config.get('database', 'super_password'),
            'AGNOSTIC_DATABASE': config.get('database', 'database'),
            'AGNOSTIC_MIGRATIONS_DIR': get_path('migrations'),
            'LANG': os.environ['LANG'],  # http://click.pocoo.org/4/python3/
            'PATH': os.environ['PATH'],
        }

        process = subprocess.Popen(['agnostic', 'bootstrap'],
                                   stdout=subprocess.DEVNULL,
                                   stderr=subprocess.PIPE,
                                   env=env)
        process.wait()

        if process.returncode != 0:
            args = (process.returncode, process.stderr.read().decode('ascii'))
            self._logger.error('External process `agnostic bootstrap` failed ' \
                               'with error code (%d):\n%s' % args)
            sys.exit(1)
示例#3
0
    def _agnostic_bootstrap(self, config):
        ''' Bootstrap the Agnostic migrations system. '''

        env = {
            'AGNOSTIC_TYPE': 'postgres',
            'AGNOSTIC_HOST': config.get('database', 'host'),
            'AGNOSTIC_USER': config.get('database', 'super_username'),
            'AGNOSTIC_PASSWORD': config.get('database', 'super_password'),
            'AGNOSTIC_SCHEMA': config.get('database', 'database'),
            'AGNOSTIC_MIGRATIONS_DIR': get_path('migrations'),
            'LANG': os.environ['LANG'], # http://click.pocoo.org/4/python3/
            'PATH': os.environ['PATH'],
        }

        process = subprocess.Popen(
            ['agnostic', 'bootstrap'],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.PIPE,
            env=env
        )
        process.wait()

        if process.returncode != 0:
            args = (process.returncode, process.stderr.read().decode('ascii'))
            self._logger.error('External process `agnostic bootstrap` failed ' \
                               'with error code (%d):\n%s' % args)
            sys.exit(1)
示例#4
0
 def _delete_data(self):
     ''' Delete files stored in the data directory. '''
     data_dir = get_path("data")
     for file_object in os.listdir(data_dir):
         file_object_path = os.path.join(data_dir, file_object)
         if os.path.isfile(file_object_path):
             os.unlink(file_object_path)
         else:
             shutil.rmtree(file_object_path)
示例#5
0
    def _create_fixture_images(self, config):
        '''
        Create the generic error image.

        Since this script will often run as root, it modifies the owner of the
        new file to match the owner of the data directory.
        '''

        session = app.database.get_session(self._db)

        image_name = 'hgprofiler_error.png'
        data_stat = os.stat(get_path('data'))
        img_path = os.path.join(get_path('static'), 'img', image_name)
        with open(img_path, 'rb') as img:
            img_data = img.read()
            image_file = File(name=image_name,
                              mime='image/png',
                              content=img_data)
            image_file.chown(data_stat.st_uid, data_stat.st_gid)
            session.add(image_file)

        session.commit()
示例#6
0
    def get(self, id_):
        '''
        Get a file identified by ``id_``.

        :status 200: ok
        :status 401: authentication required
        :status 404: no file with that ID
        '''

        file_ = g.db.query(File).filter(File.id == id_).first()
        data_dir = get_path('data')
        cache_timeout = 0 if g.debug else None

        if file_ is None:
            raise NotFound('No file exists with id={}'.format(id_))

        # Restrict access to files according to their access_type and owner.
        # access_type can be Private ('p') or shared ('s').
        if file_.access_type == 'private' and file_.user_id != g.user.id:
            raise Unauthorized('You are not authorized to view this file.')

        if file_.mime == 'application/zip':
            return send_from_directory(
                data_dir,
                file_.relpath(),
                mimetype=file_.mime,
                as_attachment=True,
                attachment_filename=file_.name,
                cache_timeout=cache_timeout
            )
        else:
            return send_from_directory(
                data_dir,
                file_.relpath(),
                mimetype=file_.mime,
                cache_timeout=cache_timeout
            )
示例#7
0
    def _run(self, args, config):
        ''' Main entry point. '''

        if args.debug_db:
            # Configure database logging.
            log_level = getattr(logging, args.verbosity.upper())

            db_logger = logging.getLogger('sqlalchemy.engine')
            db_logger.setLevel(log_level)
            db_logger.addHandler(self._log_handler)

        # Connect to database.
        database_config = dict(config.items('database'))
        self._db = app.database.get_engine(database_config, super_user=True)

        # Run build commands.
        if args.action in ('build', 'drop'):
            self._logger.info('Dropping database tables.')
            self._drop_all()

            # Delete Solr indexes whenever we drop database
            self._logger.info('Deleting Solr index.')
            solr_index_cli_path = get_path('bin/index.py')

            process = subprocess.Popen(
                ['python3', solr_index_cli_path, 'delete-all'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.PIPE,
            )

            process.wait()

            if process.returncode != 0:
                args = (process.returncode,
                        process.stderr.read().decode('ascii'))
                self._logger.error('External process `Delete Solr index` ' \
                                'failed with error code (%d):\n%s' % args)
                sys.exit(1)

        if args.action == 'build':
            self._logger.info('Running Agnostic\'s bootstrap.')
            self._agnostic_bootstrap(config)

            self._logger.info('Creating database tables.')
            Base.metadata.create_all(self._db)

            self._logger.info('Creating fixture data.')
            self._create_fixtures(config)

        if args.action == 'build' and args.sample_data:
            self._logger.info('Creating sample data.')
            self._create_samples(config)

            # Create Solr indexes when we build database with sample data
            self._logger.info('Creating Solr index.')
            solr_index_cli_path = get_path('bin/index.py')

            process = subprocess.Popen(
                ['python3', solr_index_cli_path, 'add-all'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.PIPE,
            )
            process.wait()

            if process.returncode != 0:
                args = (process.returncode,
                        process.stderr.read().decode('ascii'))
                self._logger.error('External process `Create Solr index` ' \
                                'failed with error code (%d):\n%s' % args)
                sys.exit(1)
示例#8
0
    def _run(self, args, config):
        ''' Main entry point. '''

        if args.debug_db:
            # Configure database logging.
            log_level = getattr(logging, args.verbosity.upper())

            db_logger = logging.getLogger('sqlalchemy.engine')
            db_logger.setLevel(log_level)
            db_logger.addHandler(self._log_handler)

        # Connect to database.
        database_config = dict(config.items('database'))
        self._db = app.database.get_engine(database_config, super_user=True)

        # Run build commands.
        if args.action in ('build', 'drop'):
            self._logger.info('Dropping database tables.')
            self._drop_all()

            # Delete Solr indexes whenever we drop database
            self._logger.info('Deleting Solr index.')
            solr_index_cli_path = get_path('bin/index.py')

            process = subprocess.Popen(
                ['python3', solr_index_cli_path, 'delete-all'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.PIPE,
            )

            process.wait()

            if process.returncode != 0:
                args = (process.returncode, process.stderr.read().decode('ascii'))
                self._logger.error('External process `Delete Solr index` ' \
                                'failed with error code (%d):\n%s' % args)
                sys.exit(1)

        if args.action == 'build':
            self._logger.info('Running Agnostic\'s bootstrap.')
            self._agnostic_bootstrap(config)

            self._logger.info('Creating database tables.')
            Base.metadata.create_all(self._db)

            self._logger.info('Creating fixture data.')
            self._create_fixtures(config)

        if args.action == 'build' and args.sample_data:
            self._logger.info('Creating sample data.')
            self._create_samples(config)

            # Create Solr indexes when we build database with sample data
            self._logger.info('Creating Solr index.')
            solr_index_cli_path = get_path('bin/index.py')

            process = subprocess.Popen(
                ['python3', solr_index_cli_path, 'add-all'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.PIPE,
            )
            process.wait()

            if process.returncode != 0:
                args = (process.returncode, process.stderr.read().decode('ascii'))
                self._logger.error('External process `Create Solr index` ' \
                                'failed with error code (%d):\n%s' % args)
                sys.exit(1)