From 08254cd7c938d4140f5f328cde7d382322aa9297 Mon Sep 17 00:00:00 2001 From: Adam Cecile Date: Mon, 7 Oct 2019 15:29:02 +0200 Subject: [PATCH 1/2] Convert files to Python 3 with 2to3 --- mongodbtools/collection_stats.py | 22 +++++++++++----------- mongodbtools/index_stats.py | 30 +++++++++++++++--------------- mongodbtools/query/helpers.py | 2 +- mongodbtools/query/parser.py | 18 +++++++++--------- mongodbtools/redundant_indexes.py | 6 +++--- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/mongodbtools/collection_stats.py b/mongodbtools/collection_stats.py index fb54c3e..4df21ff 100755 --- a/mongodbtools/collection_stats.py +++ b/mongodbtools/collection_stats.py @@ -18,7 +18,7 @@ def compute_signature(index): return signature def get_collection_stats(database, collection): - print "Checking DB: %s" % collection.full_name + print(("Checking DB: %s" % collection.full_name)) return database.command("collstats", collection.name) def get_cli_options(): @@ -130,7 +130,7 @@ def main(options): x.align["Storage Size"] = "r" x.padding_width = 1 - print + print() for db in all_db_stats: db_stats = all_db_stats[db] @@ -145,19 +145,19 @@ def main(options): convert_bytes(stat.get("storageSize", 0)) ]) - print - print x.get_string(sortby="% Size") - print "Total Documents:", summary_stats["count"] - print "Total Data Size:", convert_bytes(summary_stats["size"]) - print "Total Index Size:", convert_bytes(summary_stats["indexSize"]) - print "Total Storage Size:", convert_bytes(summary_stats["storageSize"]) + print() + print((x.get_string(sortby="% Size"))) + print(("Total Documents:", summary_stats["count"])) + print(("Total Data Size:", convert_bytes(summary_stats["size"]))) + print(("Total Index Size:", convert_bytes(summary_stats["indexSize"]))) + print(("Total Storage Size:", convert_bytes(summary_stats["storageSize"]))) # this is only meaningful if we're running the script on localhost if options.host == "localhost": ram_headroom = psutil.virtual_memory().total - summary_stats["indexSize"] - print "RAM Headroom:", convert_bytes(ram_headroom) - print "RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent) - print "Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom) + print(("RAM Headroom:", convert_bytes(ram_headroom))) + print(("RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent))) + print(("Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom))) if __name__ == "__main__": options = get_cli_options() diff --git a/mongodbtools/index_stats.py b/mongodbtools/index_stats.py index c2b006b..b6f6d19 100755 --- a/mongodbtools/index_stats.py +++ b/mongodbtools/index_stats.py @@ -18,7 +18,7 @@ def compute_signature(index): return signature def get_collection_stats(database, collection): - print "Checking DB: %s" % collection.full_name + print(("Checking DB: %s" % collection.full_name)) return database.command("collstats", collection.name) # From http://www.5dollarwhitebox.org/drupal/node/84 @@ -124,7 +124,7 @@ def main(options): x.align["Index Size"] = "r" x.padding_width = 1 - print + print() index_size_mapping = {} for db in all_db_stats: @@ -141,11 +141,11 @@ def main(options): x.add_row(row) - print "Index Overview" - print x.get_string(sortby="Collection") + print("Index Overview") + print((x.get_string(sortby="Collection"))) - print - print "Top 5 Largest Indexes" + print() + print("Top 5 Largest Indexes") x = PrettyTable(["Collection", "Index","% Size", "Index Size"]) x.align["Collection"] = "l" x.align["Index"] = "l" @@ -153,22 +153,22 @@ def main(options): x.align["Index Size"] = "r" x.padding_width = 1 - top_five_indexes = sorted(index_size_mapping.keys(), reverse=True)[0:5] + top_five_indexes = sorted(list(index_size_mapping.keys()), reverse=True)[0:5] for size in top_five_indexes: x.add_row(index_size_mapping.get(size)) - print x - print + print(x) + print() - print "Total Documents:", summary_stats["count"] - print "Total Data Size:", convert_bytes(summary_stats["size"]) - print "Total Index Size:", convert_bytes(summary_stats["indexSize"]) + print(("Total Documents:", summary_stats["count"])) + print(("Total Data Size:", convert_bytes(summary_stats["size"]))) + print(("Total Index Size:", convert_bytes(summary_stats["indexSize"]))) # this is only meaningful if we're running the script on localhost if options.host == "localhost": ram_headroom = psutil.virtual_memory().total - summary_stats["indexSize"] - print "RAM Headroom:", convert_bytes(ram_headroom) - print "RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent) - print "Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom) + print(("RAM Headroom:", convert_bytes(ram_headroom))) + print(("RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent))) + print(("Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom))) if __name__ == "__main__": options = get_cli_options() diff --git a/mongodbtools/query/helpers.py b/mongodbtools/query/helpers.py index 2c246ad..d8838e5 100644 --- a/mongodbtools/query/helpers.py +++ b/mongodbtools/query/helpers.py @@ -65,4 +65,4 @@ def filter(iterator, field, value): embedded documents. """ - return itertools.ifilter(lambda x: _deep_get(x, field) == value, iterator) + return filter(lambda x: _deep_get(x, field) == value, iterator) diff --git a/mongodbtools/query/parser.py b/mongodbtools/query/parser.py index 86221d1..b350cc2 100644 --- a/mongodbtools/query/parser.py +++ b/mongodbtools/query/parser.py @@ -12,17 +12,17 @@ ZeroOrMore, restOfLine, Keyword def test( str ): - print str,"->" + print(str,"->") try: tokens = simpleSQL.parseString( str ) - print "tokens = ", tokens - print "tokens.columns =", tokens.columns - print "tokens.tables =", tokens.tables - print "tokens.where =", tokens.where - except ParseException, err: - print " "*err.loc + "^\n" + err.msg - print err - print + print("tokens = ", tokens) + print("tokens.columns =", tokens.columns) + print("tokens.tables =", tokens.tables) + print("tokens.where =", tokens.where) + except ParseException as err: + print(" "*err.loc + "^\n" + err.msg) + print(err) + print() # define SQL tokens diff --git a/mongodbtools/redundant_indexes.py b/mongodbtools/redundant_indexes.py index 7cf6525..9390461 100755 --- a/mongodbtools/redundant_indexes.py +++ b/mongodbtools/redundant_indexes.py @@ -68,7 +68,7 @@ def compute_signature(index): return signature def report_redundant_indexes(current_db): - print "Checking DB: %s" % current_db.name + print("Checking DB: %s" % current_db.name) indexes = current_db.system.indexes.find() index_map = {} for index in indexes: @@ -80,11 +80,11 @@ def report_redundant_indexes(current_db): if signature == other_sig: continue if other_sig.startswith(signature): - print "Index %s[%s] may be redundant with %s[%s]" % ( + print("Index %s[%s] may be redundant with %s[%s]" % ( index_map[signature]["ns"], index_map[signature]["name"], index_map[other_sig]["ns"], - index_map[other_sig]["name"]) + index_map[other_sig]["name"])) databases= [] if options.database: From 83289b409d89c241c645b88c7eae2b7f69e959f1 Mon Sep 17 00:00:00 2001 From: Adam Cecile Date: Mon, 7 Oct 2019 15:29:43 +0200 Subject: [PATCH 2/2] Port to Python3 and escape username/password in URI --- mongodbtools/collection_stats.py | 3 ++- mongodbtools/index_stats.py | 3 ++- mongodbtools/redundant_indexes.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/mongodbtools/collection_stats.py b/mongodbtools/collection_stats.py index 4df21ff..33bda68 100755 --- a/mongodbtools/collection_stats.py +++ b/mongodbtools/collection_stats.py @@ -10,6 +10,7 @@ from pymongo import MongoClient from pymongo import ReadPreference from optparse import OptionParser +from urllib.parse import quote_plus def compute_signature(index): signature = index["ns"] @@ -58,7 +59,7 @@ def get_cli_options(): def get_client(host, port, username, password): userPass = "" if username and password: - userPass = username + ":" + password + "@" + userPass = quote_plus(username) + ":" + quote_plus(password) + "@" mongoURI = "mongodb://" + userPass + host + ":" + str(port) client = MongoClient(mongoURI) diff --git a/mongodbtools/index_stats.py b/mongodbtools/index_stats.py index b6f6d19..ae06884 100755 --- a/mongodbtools/index_stats.py +++ b/mongodbtools/index_stats.py @@ -10,6 +10,7 @@ from pymongo import MongoClient from pymongo import ReadPreference from optparse import OptionParser +from urllib.parse import quote_plus def compute_signature(index): signature = index["ns"] @@ -78,7 +79,7 @@ def get_cli_options(): def get_client(host, port, username, password): userPass = "" if username and password: - userPass = username + ":" + password + "@" + userPass = quote_plus(username) + ":" + quote_plus(password) + "@" mongoURI = "mongodb://" + userPass + host + ":" + str(port) return MongoClient(mongoURI) diff --git a/mongodbtools/redundant_indexes.py b/mongodbtools/redundant_indexes.py index 9390461..61c74a2 100755 --- a/mongodbtools/redundant_indexes.py +++ b/mongodbtools/redundant_indexes.py @@ -9,6 +9,7 @@ from pymongo import MongoClient from pymongo import ReadPreference from optparse import OptionParser +from urllib.parse import quote_plus def get_cli_options(): @@ -48,7 +49,7 @@ def get_cli_options(): def get_client(host, port, username, password): userPass = "" if username and password: - userPass = username + ":" + password + "@" + userPass = quote_plus(username) + ":" + quote_plus(password) + "@" mongoURI = "mongodb://" + userPass + host + ":" + str(port) client = MongoClient(mongoURI)