Пример #1
0
def what_if_I_upgrade(db, extract_scripts):
    """
    :param db: a :class:`openquake.server.dbapi.Db` instance
    :param extract_scripts: scripts to extract
    """
    return upgrade_manager.what_if_I_upgrade(db.conn,
                                             extract_scripts=extract_scripts)
Пример #2
0
def what_if_I_upgrade(db, extract_scripts):
    """
    :param db: a :class:`openquake.server.dbapi.Db` instance
    :param extract_scripts: scripts to extract
    """
    return upgrade_manager.what_if_I_upgrade(
        db.conn, extract_scripts=extract_scripts)
Пример #3
0
    def test_script_lower_than_current_version(self):
        applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split())
        self.assertEqual(applied, '0001 0005'.split())
        self.assertEqual(count(conn, 'test_hazard_calculation'), 2)
        self.assertEqual(count(conn, 'test_lt_source_model'), 6)

        # a script 0004 can enter when the database is already at version 0005
        # (this is a convenient feature during development) but officially
        # this is not supported and what_if_I_upgrade must raise an exception
        with temp_script('0004-do-nothing.sql', 'SELECT 1'):
            applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split())
            self.assertEqual(applied, ['0004'])

        # check that the script 0004 is rejected by what_if_I_upgrade
        with temp_script('0004-do-nothing.sql', 'SELECT 1'):
            with self.assertRaises(VersionTooSmall):
                what_if_I_upgrade(conn, pkg, 'read_scripts')
Пример #4
0
    def test_script_lower_than_current_version(self):
        applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split())
        self.assertEqual(applied, '0001 0005'.split())
        self.assertEqual(count(conn, 'test.hazard_calculation'), 2)
        self.assertEqual(count(conn, 'test.lt_source_model'), 6)

        # a script 0004 can enter when the database is already at version 0005
        # (this is a convenient feature during development) but officially
        # this is not supported and what_if_I_upgrade must raise an exception
        with temp_script('0004-do-nothing.sql', 'SELECT 1'):
            applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split())
            self.assertEqual(applied, ['0004'])

        # check that the script 0004 is rejected by what_if_I_upgrade
        with temp_script('0004-do-nothing.sql', 'SELECT 1'):
            with self.assertRaises(VersionTooSmall):
                what_if_I_upgrade(conn, pkg, 'read_scripts')
Пример #5
0
 def check_message(self, html, expected):
     with mock.patch('urllib.request.urlopen') as urlopen:
         urlopen().read.return_value = html
         got = what_if_I_upgrade(conn, pkg)
         self.assertEqual(got, expected)
Пример #6
0
def what_if_I_upgrade(extract_scripts):
    db.connection.cursor()  # bind the connection
    conn = db.connection.connection
    return upgrade_manager.what_if_I_upgrade(
        conn, extract_scripts=extract_scripts)
Пример #7
0
def what_if_I_upgrade(extract_scripts):
    conn = db.connection.connection
    return upgrade_manager.what_if_I_upgrade(conn,
                                             extract_scripts=extract_scripts)
Пример #8
0
 def check_message(self, html, expected):
     if hasattr(urllib, 'urlopen'):  # Python 2
         with mock.patch('urllib.urlopen') as urlopen:
             urlopen().read.return_value = html
             got = what_if_I_upgrade(conn, pkg)
             self.assertEqual(got, expected)
Пример #9
0
def what_if_I_upgrade(extract_scripts):
    conn = db.connection.connection
    return upgrade_manager.what_if_I_upgrade(
        conn, extract_scripts=extract_scripts)
Пример #10
0
 def check_message(self, html, expected):
     with mock.patch('urllib.urlopen') as urlopen:
         urlopen().read.return_value = html
         got = what_if_I_upgrade(conn, pkg)
         self.assertEqual(got, expected)
Пример #11
0
def main():
    arg_parser = set_up_arg_parser()

    args = arg_parser.parse_args()

    exports = args.exports or 'xml,csv'

    if args.version:
        print __version__
        sys.exit(0)

    if args.run or args.run_hazard or args.run_risk:
        # the logging will be configured in engine.py
        pass
    else:
        # configure a basic logging
        logging.basicConfig(level=logging.INFO)

    if args.config_file:
        os.environ[utils.config.OQ_CONFIG_FILE_VAR] = \
            abspath(expanduser(args.config_file))
        utils.config.refresh()

    if args.no_distribute:
        os.environ[openquake.engine.NO_DISTRIBUTE_VAR] = '1'

    if args.upgrade_db:
        logs.set_level('info')
        msg = upgrade_manager.what_if_I_upgrade(
            conn, extract_scripts='read_scripts')
        print msg
        if msg.startswith('Your database is already updated'):
            pass
        elif args.yes or utils.confirm('Proceed? (y/n) '):
            upgrade_manager.upgrade_db(conn)
        sys.exit(0)

    if args.version_db:
        print upgrade_manager.version_db(conn)
        sys.exit(0)

    if args.what_if_I_upgrade:
        print upgrade_manager.what_if_I_upgrade(conn)
        sys.exit(0)

    # check if the db is outdated
    outdated = dbcmd('check_outdated')
    if outdated:
        sys.exit(outdated)

    # hazard or hazard+risk
    hc_id = args.hazard_calculation_id
    if hc_id and int(hc_id) < 0:
        # make it possible commands like `oq-engine --run job_risk.ini --hc -1`
        hc_id = dbcmd('get_hc_id', int(hc_id))
    if args.run:
        job_inis = map(expanduser, args.run.split(','))
        if len(job_inis) not in (1, 2):
            sys.exit('%s should be a .ini filename or a pair of filenames '
                     'separated by a comma' % args.run)
        for job_ini in job_inis:
            open(job_ini).read()  # raise an IOError if the file does not exist
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None

        if len(job_inis) == 2:
            # run hazard
            job_id = run_job(job_inis[0], args.log_level,
                             log_file, args.exports)
            # run risk
            run_job(job_inis[1], args.log_level, log_file,
                    args.exports, hazard_calculation_id=job_id)
        else:
            run_job(
                expanduser(args.run), args.log_level, log_file,
                args.exports, hazard_calculation_id=hc_id)
    # hazard
    elif args.list_hazard_calculations:
        dbcmd('list_calculations', 'hazard')
    elif args.run_hazard is not None:
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None
        run_job(expanduser(args.run_hazard), args.log_level,
                log_file, args.exports)
    elif args.delete_calculation is not None:
        dbcmd('delete_calculation', args.delete_calculation, args.yes)
    # risk
    elif args.list_risk_calculations:
        dbcmd('list_calculations', 'risk')
    elif args.run_risk is not None:
        if args.hazard_calculation_id is None:
            sys.exit(MISSING_HAZARD_MSG)
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None
        run_job(
            expanduser(args.run_risk),
            args.log_level, log_file, args.exports,
            hazard_calculation_id=hc_id)

    # export
    elif args.make_html_report:
        print 'Written', make_report(conn, args.make_html_report)
        sys.exit(0)

    elif args.list_outputs is not None:
        hc_id = dbcmd('get_hc_id', args.list_outputs)
        dbcmd('list_outputs', hc_id)
    elif args.show_view is not None:
        job_id, view_name = args.show_view
        print views.view(view_name, datastore.read(int(job_id)))
    elif args.show_log is not None:
        hc_id = dbcmd('get_hc_id', args.show_log[0])
        print dbcmd('get_log', hc_id)

    elif args.export_output is not None:
        output_id, target_dir = args.export_output
        dbcmd('export_output', int(output_id), expanduser(target_dir),
              exports)

    elif args.export_outputs is not None:
        job_id, target_dir = args.export_outputs
        hc_id = dbcmd('get_hc_id', job_id)
        dbcmd('export_outputs', hc_id, expanduser(target_dir), exports)

    elif args.delete_uncompleted_calculations:
        dbcmd('delete_uncompleted_calculations')
    else:
        arg_parser.print_usage()
Пример #12
0
def main():
    arg_parser = set_up_arg_parser()

    args = arg_parser.parse_args()

    exports = args.exports or 'xml,csv'

    if args.version:
        print __version__
        sys.exit(0)

    if args.run or args.run_hazard or args.run_risk:
        # the logging will be configured in engine.py
        pass
    else:
        # configure a basic logging
        logging.basicConfig(level=logging.INFO)

    if args.config_file:
        os.environ[config.OQ_CONFIG_FILE_VAR] = \
            abspath(expanduser(args.config_file))
        config.refresh()

    if args.no_distribute:
        os.environ['OQ_DISTRIBUTE'] = 'no'

    if args.upgrade_db:
        logs.set_level('info')
        msg = upgrade_manager.what_if_I_upgrade(
            conn, extract_scripts='read_scripts')
        print msg
        if msg.startswith('Your database is already updated'):
            pass
        elif args.yes or utils.confirm('Proceed? (y/n) '):
            upgrade_manager.upgrade_db(conn)
        sys.exit(0)

    if args.version_db:
        print upgrade_manager.version_db(conn)
        sys.exit(0)

    if args.what_if_I_upgrade:
        print upgrade_manager.what_if_I_upgrade(conn)
        sys.exit(0)

    # check if the db is outdated
    outdated = dbcmd('check_outdated')
    if outdated:
        sys.exit(outdated)

    # hazard or hazard+risk
    hc_id = args.hazard_calculation_id
    if hc_id:
        hc_id = int(hc_id)
        if hc_id < 0:
            # make it possible to run `oq-engine --run job_risk.ini --hc -1`
            hc_id = dbcmd('get_hc_id', hc_id)
    if args.run:
        job_inis = map(expanduser, args.run.split(','))
        if len(job_inis) not in (1, 2):
            sys.exit('%s should be a .ini filename or a pair of filenames '
                     'separated by a comma' % args.run)
        for job_ini in job_inis:
            open(job_ini).read()  # raise an IOError if the file does not exist
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None

        if len(job_inis) == 2:
            # run hazard
            job_id = run_job(job_inis[0], args.log_level,
                             log_file, args.exports)
            # run risk
            run_job(job_inis[1], args.log_level, log_file,
                    args.exports, hazard_calculation_id=job_id)
        else:
            run_job(
                expanduser(args.run), args.log_level, log_file,
                args.exports, hazard_calculation_id=hc_id)
    # hazard
    elif args.list_hazard_calculations:
        dbcmd('list_calculations', 'hazard')
    elif args.run_hazard is not None:
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None
        run_job(expanduser(args.run_hazard), args.log_level,
                log_file, args.exports)
    elif args.delete_calculation is not None:
        dbcmd('delete_calculation', args.delete_calculation, args.yes)
    # risk
    elif args.list_risk_calculations:
        dbcmd('list_calculations', 'risk')
    elif args.run_risk is not None:
        if args.hazard_calculation_id is None:
            sys.exit(MISSING_HAZARD_MSG)
        log_file = expanduser(args.log_file) \
            if args.log_file is not None else None
        run_job(
            expanduser(args.run_risk),
            args.log_level, log_file, args.exports,
            hazard_calculation_id=hc_id)

    # export
    elif args.make_html_report:
        print 'Written', make_report(conn, args.make_html_report)
        sys.exit(0)

    elif args.list_outputs is not None:
        hc_id = dbcmd('get_hc_id', args.list_outputs)
        dbcmd('list_outputs', hc_id)
    elif args.show_view is not None:
        job_id, view_name = args.show_view
        print views.view(view_name, datastore.read(int(job_id)))
    elif args.show_log is not None:
        hc_id = dbcmd('get_hc_id', args.show_log[0])
        print dbcmd('get_log', hc_id)

    elif args.export_output is not None:
        output_id, target_dir = args.export_output
        dbcmd('export_output', int(output_id), expanduser(target_dir),
              exports)

    elif args.export_outputs is not None:
        job_id, target_dir = args.export_outputs
        hc_id = dbcmd('get_hc_id', job_id)
        dbcmd('export_outputs', hc_id, expanduser(target_dir), exports)

    elif args.delete_uncompleted_calculations:
        dbcmd('delete_uncompleted_calculations')
    else:
        arg_parser.print_usage()