diff --git a/util/helper_scripts_for_mongodb/README.md b/util/helper_scripts_for_mongodb/README.md index 00b7e8ae50..e252d6a427 100644 --- a/util/helper_scripts_for_mongodb/README.md +++ b/util/helper_scripts_for_mongodb/README.md @@ -3,46 +3,49 @@ This utility contains various scripts that are helpful when maintaining the gem5 resources MongoDB database. The scripts in this directory use external libraries. Please install the required libraries mentioned in the `requirements.txt` by running the following command: + ``` pip3 install -r requirements.txt ``` -## add-json-to-mongo.py +## Adding JSON file to MongoDB -This script adds a list of resources from a JSON file to a specified collection in a MongoDB database. The JSON file should be in the format of a list of dictionaries, where each dictionary represents a resource. - -To run this script you use the following command: +We can use the following command to add a JSON file to MongoDB collection: ``` - python3 ./add-json-to-mongo.py --uri --db_name --collection_name --json_file +mongoimport --db --collection --file --jsonArray ``` -## backup-db.py +## backup MongoDB -This script grabs all documents from a specified collection in a MongoDB database and saves them to a JSON file. +We can mongodump to take a back of a mongoDB databse. We can use the following command: -To run this script you use the following command: ``` - python3 ./backup-db.py --uri --db_name --collection_name +mongodump --uri ``` ## create-new-collection.py + This script grabs all documents from a specified collection in a MongoDB database and creates a new collection with the same documents. To run this script you use the following command: + ``` python3 ./create-new-collection.py --uri --db_name --collection_name --new_collection_name ``` ## update-gem5-versions.py + This script grabs all resources categorically from a specified collection in a MongoDB database and adds a new gem5 version to the gem5_versions field of each resource. To run this script you use the following command: + ``` python3 ./update-gem5-versions.py --uri --db --collection --version --category --outfile ``` ## helper.py + This script contains helper functions for the scripts in this directory. diff --git a/util/helper_scripts_for_mongodb/add-json-to-mongo.py b/util/helper_scripts_for_mongodb/add-json-to-mongo.py deleted file mode 100644 index 687834e40d..0000000000 --- a/util/helper_scripts_for_mongodb/add-json-to-mongo.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2023 The Regents of the University of California -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer; -# redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution; -# neither the name of the copyright holders nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -""" - This script adds a list of resources from a JSON file to a specified - collection in a MongoDB database. The JSON file should be in the format - of a list of dictionaries, where each dictionary represents a resource. - - To run this script you use the following command: - python3 ./add-json-to-mongo.py --uri --db_name / - --collection_name --json_file -""" - -import argparse -import json - -from helper import get_database - -parser = argparse.ArgumentParser( - description="Add a list of resources from a " - "JSON file to a specified collection in a " - "MongoDB database" -) -parser.add_argument( - "--uri", help="URI of the database", type=str, required=True -) -parser.add_argument( - "--db_name", help="Name of the database", type=str, default="gem5-vision" -) -parser.add_argument( - "--collection_name", - help="Name of the collection", - type=str, - default="resources", -) -parser.add_argument( - "--json_file", help="Name of the json file", type=str, required=True -) - -if __name__ == "__main__": - args = parser.parse_args() - - uri = args.uri - db_name = args.db_name - collection_name = args.collection_name - json_file = args.json_file - - # get resources from json file - with open(json_file) as f: - resources = json.load(f) - - collection = get_database(uri, db_name, collection_name) - - # insert resources into collection - collection.insert_many(resources) diff --git a/util/helper_scripts_for_mongodb/backup-db.py b/util/helper_scripts_for_mongodb/backup-db.py deleted file mode 100644 index 198731b167..0000000000 --- a/util/helper_scripts_for_mongodb/backup-db.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2023 The Regents of the University of California -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer; -# redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution; -# neither the name of the copyright holders nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -""" - This script grabs all documents from a specified collection in a MongoDB - database and saves them to a JSON file. - - To run this script you use the following command: - python3 ./backup-db.py --uri --db_name --collection_name -""" - -import argparse -from datetime import date - -from helper import ( - get_database, - save_to_json, -) - -parser = argparse.ArgumentParser( - description="Get all documents from a " - "specified collection in a MongoDB database " - "and save them to a JSON file" -) -parser.add_argument( - "--uri", help="URI of the database", type=str, required=True -) -parser.add_argument( - "--db_name", help="Name of the database", type=str, default="gem5-vision" -) -parser.add_argument( - "--collection_name", - help="Name of the collection", - type=str, - default="resources", -) -args = parser.parse_args() - -uri = args.uri -db_name = args.db_name -collection_name = args.collection_name - -collection = get_database(uri, db_name, collection_name) - -# get all documents from resources collection -resources = collection.find({}) - -# copy all documents from resources collection to resources_backup json file -save_to_json(resources, f"resources_backup_{date.today()}.json")