Пример #1
0
 def dump_database(self, db):
     """Dumps a database dict via mongoexport."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     colls = self.client[db["name"]].collection_names(
         include_system_collections=False)
     for collection in colls:
         f = os.path.join(dbpath, collection + ".json")
         cmd = [
             "mongoexport",
             "--db",
             db["name"],
             "--collection",
             collection,
             "--out",
             f,
         ]
         try:
             subprocess.check_call(cmd, stderr=subprocess.STDOUT)
         except subprocess.CalledProcessError as exc:
             print("Status : FAIL", exc.returncode, exc.output)
             raise exc
         to_add.append(os.path.join(db["path"], collection + ".json"))
     return to_add
Пример #2
0
 def load_database(self, db):
     """Loads a database via mongoimport.  Takes a database dict db."""
     dbpath = dbpathname(db, self.rc)
     for f in iglob(os.path.join(dbpath, '*.json')):
         base, ext = os.path.splitext(os.path.split(f)[-1])
         cmd = ['mongoimport', '--db',  db['name'], '--collection', base,
                '--file', f]
         subprocess.check_call(cmd)
Пример #3
0
 def load_database(self, db):
     """Loads a database via mongoimport.  Takes a database dict db."""
     dbpath = dbpathname(db, self.rc)
     for f in iglob(os.path.join(dbpath, '*.json')):
         base, ext = os.path.splitext(os.path.split(f)[-1])
         cmd = [
             'mongoimport', '--db', db['name'], '--collection', base,
             '--file', f
         ]
         subprocess.check_call(cmd)
Пример #4
0
 def dump_database(self, db):
     """Dumps a database dict via mongoexport."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     colls = self.client[db['name']].collection_names(
                                         include_system_collections=False)
     for collection in colls:
         f = os.path.join(dbpath, collection + '.json')
         cmd = ['mongoexport', '--db',  db['name'],
                '--collection', collection, '--out', f]
         subprocess.check_call(cmd)
         to_add.append(os.path.join(db['path'], collection + '.json'))
     return to_add
Пример #5
0
    def import_database(self, db: dict):
        """Import the database from filesystem to the mongo backend.

        Parameters
        ----------
        db : dict
            The dictionary of data base information, such as 'name'.
        """
        host = getattr(self.rc, 'host', None)
        uri = db.get('dst_url', None)
        dbpath = dbpathname(db, self.rc)
        dbname = db['name']
        import_jsons(dbpath, dbname, host=host, uri=uri)
        import_yamls(dbpath, dbname, host=host, uri=uri)
        return
Пример #6
0
 def load_database(self, db):
     """Loads a database via mongoimport.  Takes a database dict db."""
     dbpath = dbpathname(db, self.rc)
     for f in iglob(os.path.join(dbpath, "*.json")):
         base, ext = os.path.splitext(os.path.split(f)[-1])
         cmd = [
             "mongoimport",
             "--db",
             db["name"],
             "--collection",
             base,
             "--file",
             f,
         ]
         subprocess.check_call(cmd)
Пример #7
0
 def dump_database(self, db):
     """Dumps a database back to the filesystem."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     for collname, collection in self.dbs[db["name"]].items():
         print("dumping " + collname + "...", file=sys.stderr)
         filetype = self._collfiletypes.get(collname, "yaml")
         if filetype == "json":
             filename = self.dump_json(collection, collname, dbpath)
         elif filetype == "yaml":
             filename = self.dump_yaml(collection, collname, dbpath)
         else:
             raise ValueError("did not recognize file type for regolith")
         to_add.append(os.path.join(db["path"], filename))
     return to_add
Пример #8
0
 def dump_database(self, db):
     """Dumps a database back to the filesystem."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     for collname, collection in self.dbs[db['name']].items():
         print('dumping ' + collname + '...', file=sys.stderr)
         filetype = self._collfiletypes.get(collname, 'yaml')
         if filetype == 'json':
             filename = self.dump_json(collection, collname, dbpath)
         elif filetype == 'yaml':
             filename = self.dump_yaml(collection, collname, dbpath)
         else:
             raise ValueError('did not recognize file type for regolith')
         to_add.append(os.path.join(db['path'], filename))
     return to_add
Пример #9
0
 def dump_database(self, db):
     """Dumps a database dict via mongoexport."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     colls = self.client[db['name']].collection_names(
         include_system_collections=False)
     for collection in colls:
         f = os.path.join(dbpath, collection + '.json')
         cmd = [
             'mongoexport', '--db', db['name'], '--collection', collection,
             '--out', f
         ]
         subprocess.check_call(cmd)
         to_add.append(os.path.join(db['path'], collection + '.json'))
     return to_add
Пример #10
0
    def export_database(self, db: dict):
        """Exports the database from mongo backend to the filesystem.

        Parameters
        ----------
        db : dict
            The dictionary of data base information, such as 'name'.
        """
        host = getattr(self.rc, 'host', None)
        uri = db.get('dst_url', None)
        # Catch the easy/common regolith rc error of putting the db uri as localhost rather than host
        if uri == 'localhost':
            uri = None
            host = 'localhost'
        dbpath = os.path.abspath(dbpathname(db, self.rc))
        dbname = db['name']
        for collection in self.dbs[dbname].keys():
            export_json(collection, dbpath, dbname, host=host, uri=uri)
        return
Пример #11
0
    def import_database(self, db: dict):
        """Import the database from filesystem to the mongo backend.

        Parameters
        ----------
        db : dict
            The dictionary of data base information, such as 'name'.
        """
        host = getattr(self.rc, 'host', None)
        uri = db.get('dst_url', None)
        # Catch the easy/common regolith rc error of putting the db uri as localhost rather than host
        if uri == 'localhost':
            uri = None
            host = 'localhost'
        dbpath = dbpathname(db, self.rc)
        dbname = db['name']
        import_jsons(dbpath, dbname, host=host, uri=uri)
        import_yamls(dbpath, dbname, host=host, uri=uri)
        return
Пример #12
0
 def dump_database(self, db):
     """Dumps a database dict via mongoexport."""
     dbpath = dbpathname(db, self.rc)
     os.makedirs(dbpath, exist_ok=True)
     to_add = []
     colls = self.client[db["name"]].collection_names(
         include_system_collections=False)
     for collection in colls:
         f = os.path.join(dbpath, collection + ".json")
         cmd = [
             "mongoexport",
             "--db",
             db["name"],
             "--collection",
             collection,
             "--out",
             f,
         ]
         subprocess.check_call(cmd)
         to_add.append(os.path.join(db["path"], collection + ".json"))
     return to_add
Пример #13
0
 def load_database(self, db):
     """Loads a database."""
     dbpath = dbpathname(db, self.rc)
     self.load_json(db, dbpath)
     self.load_yaml(db, dbpath)