Exemplo n.º 1
0
def _initialize_database(repository_url, table_name, data_dir, test_dir, export_dir):
    """Initialize database for tests.

    This function will create the database and load any test data that
    the suite may require.

    Parameters
    ----------
    repository_url: str
        URL for the repository, as used by SQLAlchemy engines
    table_name: str
        Name of the table that is to be used to store pycsw records
    data_dir: str
        Path to a directory that contains sample data records to be loaded
        into the database
    test_dir: str
        Directory where the database is to be created, in case of sqlite.
    export_dir: str
        Diretory where the exported records are to be saved, if any

    """

    print("Setting up {0!r} repository...".format(repository_url))
    if repository_url.startswith("postgresql"):
        extra_kwargs = {
            "create_sfsql_tables": True,
            "create_plpythonu_functions": False
        }
    else:
        extra_kwargs = {}
    admin.setup_db(database=repository_url, table=table_name, home=test_dir,
                   **extra_kwargs)
    if len(os.listdir(data_dir)) > 0:
        print("Loading database data...")
        loaded = admin.load_records(
            context=StaticContext(),
            database=repository_url,
            table=table_name,
            xml_dirpath=data_dir,
            recursive=True
        )
        admin.optimize_db(context=StaticContext(), database=repository_url, table=table_name)
        if export_dir is not None:
            # Attempt to export files
            exported = admin.export_records(
                context=StaticContext(),
                database=repository_url,
                table=table_name,
                xml_dirpath=export_dir
            )
            if len(loaded) != len(exported):
                raise ValueError(
                    "Loaded records (%s) is different from exported records (%s)" %
                    (len(loaded), len(exported))
                )
            # Remove the files that were exported since this was just a test
            for toremove in exported:
                os.remove(toremove)
Exemplo n.º 2
0
 def handle_db(self, args):
     database, table = self._get_db_settings()
     if args.db_command == "create":
         home = self.config.get('server', 'home')
         admin.setup_db(database, table, home)
     elif args.db_command == "optimize":
         admin.optimize_db(self.context, database, table)
     elif args.db_command == "rebuild":
         admin.rebuild_db_indexes(database, table)
     elif args.db_command == "clean":
         force = args.accept_changes
         if not force:
             msg = "This will delete all records! Continue [Y/n] "
             if raw_input(msg) == 'Y':
                 force = True
         if force:
             admin.delete_records(self.context, database, table)
Exemplo n.º 3
0
if COMMAND == 'setup_db':
    try:
        admin.setup_db(DATABASE, TABLE, HOME)
    except Exception as err:
        print err
        print 'ERROR: DB creation error.  Database tables already exist'
        print 'Delete tables or database to reinitialize'
elif COMMAND == 'load_records':
    admin.load_records(CONTEXT, DATABASE, TABLE, XML_DIRPATH, RECURSIVE, FORCE_CONFIRM)
elif COMMAND == 'export_records':
    admin.export_records(CONTEXT, DATABASE, TABLE, XML_DIRPATH)
elif COMMAND == 'rebuild_db_indexes':
    admin.rebuild_db_indexes(DATABASE, TABLE)
elif COMMAND == 'optimize_db':
    admin.optimize_db(CONTEXT, DATABASE, TABLE)
elif COMMAND == 'refresh_harvested_records':
    admin.refresh_harvested_records(CONTEXT, DATABASE, TABLE, URL)
elif COMMAND == 'gen_sitemap':
    admin.gen_sitemap(CONTEXT, DATABASE, TABLE, URL, OUTPUT_FILE)
elif COMMAND == 'post_xml':
    print admin.post_xml(CSW_URL, XML, TIMEOUT)
elif COMMAND == 'get_sysprof':
    print admin.get_sysprof()
elif COMMAND == 'validate_xml':
    admin.validate_xml(XML, XSD)
elif COMMAND == 'delete_records':
    if not FORCE_CONFIRM:
        if raw_input('This will delete all records! Continue? [Y/n] ') == 'Y':
            FORCE_CONFIRM = True
    if FORCE_CONFIRM:
Exemplo n.º 4
0
    def run(self, argv):

        if len(argv) == 0:
            grass.error('Nothing to do. Set args')
            return
        print argv
        try:
            OPTS, ARGS = getopt.getopt(argv, 'c:f:ho:p:ru:x:s:t:y')
        except getopt.GetoptError as err:
            grass.error('\nERROR: %s' % err)
            #print usage()

        for o, a in OPTS:
            if o == '-c':
                self.COMMAND = a
            if o == '-f':
                self.CFG = a
            if o == '-o':
                self.OUTPUT_FILE = a
            if o == '-p':
                self.XML_DIRPATH = a
            if o == '-r':
                self.RECURSIVE = True
            if o == '-u':
                self.CSW_URL = a
            if o == '-x':
                self.XML = a
            if o == '-t':
                self.TIMEOUT = int(a)
            if o == '-y':
                self.FORCE_CONFIRM = True

        if self.CFG is None and self.COMMAND not in ['post_xml']:
            print 'ERROR: -f <cfg> is a required argument'

        if self.COMMAND not in ['post_xml']:
            SCP = ConfigParser.SafeConfigParser()
            SCP.readfp(open(self.CFG))

            self.DATABASE = SCP.get('repository', 'database')
            self.URL = SCP.get('server', 'url')
            self.HOME = SCP.get('server', 'home')
            self.METADATA = dict(SCP.items('metadata:main'))
            try:
                self.TABLE = SCP.get('repository', 'table')
            except ConfigParser.NoOptionError:
                self.TABLE = 'records'

        if self.COMMAND == 'setup_db':
            try:
                admin.setup_db(self.DATABASE, self.TABLE, self.HOME)
            except Exception as err:
                print err
                print 'ERROR: DB creation error.  Database tables already exist'
                print 'Delete tables or database to reinitialize'

        elif self.COMMAND == 'load_records':
            admin.load_records(self.CONTEXT, self.DATABASE, self.TABLE,
                               self.XML_DIRPATH, self.RECURSIVE,
                               self.FORCE_CONFIRM)
        elif self.COMMAND == 'export_records':
            admin.export_records(self.CONTEXT, self.DATABASE, self.TABLE,
                                 self.XML_DIRPATH)
        elif self.COMMAND == 'rebuild_db_indexes':
            admin.rebuild_db_indexes(self.DATABASE, self.TABLE)
        elif self.COMMAND == 'optimize_db':
            admin.optimize_db(self.CONTEXT, self.DATABASE, self.TABLE)
        elif self.COMMAND == 'refresh_harvested_records':
            admin.refresh_harvested_records(self.CONTEXT, self.DATABASE,
                                            self.TABLE, self.URL)
        elif self.COMMAND == 'gen_sitemap':
            admin.gen_sitemap(self.CONTEXT, self.DATABASE, self.TABLE,
                              self.URL, self.OUTPUT_FILE)
        elif self.COMMAND == 'post_xml':
            grass.message(admin.post_xml(self.CSW_URL, self.XML, self.TIMEOUT))

        elif self.COMMAND == 'delete_records':
            admin.delete_records(self.CONTEXT, self.DATABASE, self.TABLE)
Exemplo n.º 5
0
if COMMAND == 'setup_db':
    try:
        admin.setup_db(DATABASE, TABLE, HOME)
    except Exception as err:
        print(err)
        print('ERROR: DB creation error.  Database tables already exist')
        print('Delete tables or database to reinitialize')
elif COMMAND == 'load_records':
    admin.load_records(CONTEXT, DATABASE, TABLE, XML_DIRPATH, RECURSIVE,
                       FORCE_CONFIRM)
elif COMMAND == 'export_records':
    admin.export_records(CONTEXT, DATABASE, TABLE, XML_DIRPATH)
elif COMMAND == 'rebuild_db_indexes':
    admin.rebuild_db_indexes(DATABASE, TABLE)
elif COMMAND == 'optimize_db':
    admin.optimize_db(CONTEXT, DATABASE, TABLE)
elif COMMAND == 'refresh_harvested_records':
    admin.refresh_harvested_records(CONTEXT, DATABASE, TABLE, URL)
elif COMMAND == 'gen_sitemap':
    admin.gen_sitemap(CONTEXT, DATABASE, TABLE, URL, OUTPUT_FILE)
elif COMMAND == 'post_xml':
    print(admin.post_xml(CSW_URL, XML, TIMEOUT))
elif COMMAND == 'get_sysprof':
    print(admin.get_sysprof())
elif COMMAND == 'validate_xml':
    admin.validate_xml(XML, XSD)
elif COMMAND == 'delete_records':
    if not FORCE_CONFIRM:
        if input('This will delete all records! Continue? [Y/n] ') == 'Y':
            FORCE_CONFIRM = True
    if FORCE_CONFIRM: