forked from briney/clonify-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunset_clonify.py
60 lines (44 loc) · 1.82 KB
/
unset_clonify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
# filename: unset_clonify.py
###########################################################################
#
# Copyright (c) 2014 Bryan Briney. All rights reserved.
#
# @version: 1.0.0
# @author: Bryan Briney
# @license: MIT (http://opensource.org/licenses/MIT)
#
###########################################################################
import argparse
from pymongo import MongoClient
parser = argparse.ArgumentParser("Removes Clonify lineage assignments from a MongoDB database.")
parser.add_argument('-i', '-ip', dest='mongo_ip', default='localhost',
help="The IP address of the MongoDB server. Default is 'localhost'.")
parser.add_argument('-p', '-port', dest='mongo_port', default=27017, type=int,
help="The MongoDB port. Default is 27017.")
parser.add_argument('-d' '-database', dest='database', required=True,
help="The database to query. Required.")
parser.add_argument('-c' '-collection', dest='collection', default=None,
help="The collection to query. If not provided, \
all collections in the database will be processed iteratively.")
args = parser.parse_args()
def get_collections():
if args.collection:
return [args.collection,]
conn = MongoClient(args.mongo_ip, args.mongo_port)
db = conn[args.database]
collections = db.collection_names(include_system_collections=False)
return sorted(collections)
def update_db(collection):
conn = MongoClient(args.mongo_ip, args.mongo_port)
db = conn[args.database]
coll = db[collection]
coll.update({'clonify': {'$exists': True}}, {'$unset': {'clonify': 1}}, multi=True)
def main():
collections = get_collections()
print '\n\n{0} collection(s) will be processed.\n\n'.format(len(collections))
for collection in collections:
print 'processing {0}...'.format(collection)
update_db(collection)
if __name__ == '__main__':
main()