util: Addressed requested changes

Change-Id: I202bb591960b76f74c3fbb95867905b968c3517d
This commit is contained in:
Harshil Patel
2024-01-10 21:59:21 -08:00
parent 3f2a72641b
commit 77d6442c1a
3 changed files with 12 additions and 161 deletions

View File

@@ -3,46 +3,49 @@
This utility contains various scripts that are helpful when maintaining the gem5 resources MongoDB database. 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 The scripts in this directory use external libraries. Please install the required libraries mentioned in the
`requirements.txt` by running the following command: `requirements.txt` by running the following command:
``` ```
pip3 install -r requirements.txt 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. We can use the following command to add a JSON file to MongoDB collection:
To run this script you use the following command:
``` ```
python3 ./add-json-to-mongo.py --uri <uri> --db_name <db_name> --collection_name <collection_name> --json_file <json_file> mongoimport --db <dbName> --collection <collectionName> --file <fileName.json> --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 <uri> --db_name <db_name> --collection_name <collection_name> mongodump --uri <uri>
``` ```
## create-new-collection.py ## 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. 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: To run this script you use the following command:
``` ```
python3 ./create-new-collection.py --uri <uri> --db_name <db_name> --collection_name <collection_name> --new_collection_name <new_collection_name> python3 ./create-new-collection.py --uri <uri> --db_name <db_name> --collection_name <collection_name> --new_collection_name <new_collection_name>
``` ```
## update-gem5-versions.py ## update-gem5-versions.py
This script grabs all resources categorically from a specified collection in a MongoDB 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 database and adds a new gem5 version to the gem5_versions field of each
resource. resource.
To run this script you use the following command: To run this script you use the following command:
``` ```
python3 ./update-gem5-versions.py --uri <uri> --db <db_name> --collection <collection_name> --version <version> --category <category> --outfile <outfile> python3 ./update-gem5-versions.py --uri <uri> --db <db_name> --collection <collection_name> --version <version> --category <category> --outfile <outfile>
``` ```
## helper.py ## helper.py
This script contains helper functions for the scripts in this directory. This script contains helper functions for the scripts in this directory.

View File

@@ -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 <uri> --db_name <db_name> /
--collection_name <collection_name> --json_file <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)

View File

@@ -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 <uri> --db_name <db_name> --collection_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")