-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymongofog.py
203 lines (176 loc) · 6.96 KB
/
pymongofog.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from bson import json_util
from faker import Faker
from providers.image_url import SafeImageUrl
import argparse
import bson
import os
import yaml
import pymongo
fake = Faker()
fake.add_provider(SafeImageUrl)
client = pymongo.MongoClient("localhost", 27017)
def flatten(d, sep='_', parent_key=''):
"""Flattens a nested map.
Adapted from https://stackoverflow.com/a/6027615/254190"""
items=dict()
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if v and isinstance(v, dict):
items = {**items, **flatten(v, sep, new_key)}
else:
items[new_key] = v
return items
def vacate_collection(db_name, collection_name):
"""Removes all records from a collection"""
coll = client[db_name][collection_name]
docs_count = coll.count_documents({})
print("-- Removing", docs_count, "documents...")
coll.delete_many({})
return
def str_to_generator(fk, s):
"""Converts an object - usually a string - into a generator.
'delete' is a special case where we simply return False. We expect the
caller to understand what this means.
We would like this to support lists and maps (i.e. recursion) at some point.
Patches welcome!
"""
if s == "delete":
return False
elif isinstance(s, list):
raise Exception("Lists -- especially lists of maps -- are not yet supported!")
else:
return getattr(fk, s)
def remove_missing_keys(a, b):
"""Remove keys from a that don't appear in b. Expects both maps to be flat"""
n = dict()
for k in b.keys():
if k in a:
n[k] = a[k]
return n
def get_filters(filters, db_name, collection_name):
return (filters.get(db_name) and filters.get(db_name).get(collection_name) or {})
def transform_values(*, db_name, collection_name, set_generator, unset_generator, filters={}):
"""Iterate through a collection (identified by db_name.collection_name, e.g. foo.bar),
updating each record by calling the provided 'set' and 'unset' generators.
This means a new, random 'set' (and 'unset' - although this doesn't really make sense)
for each record.
"""
coll = client[db_name][collection_name]
coll_filter = get_filters(filters, db_name, collection_name)
docs_count = coll.count_documents(coll_filter)
print("-- Updating", docs_count, "documents...")
if coll_filter:
print(" (filtering ", coll_filter, ")")
for result in coll.find(coll_filter):
tries = 10
while tries > 0:
# TODO values that don't exist on the record
# should be removed from the set.
new_set = remove_missing_keys(set_generator(), flatten(result, '.'))
try:
coll.update_one({ '_id': result['_id']},
{'$set' : new_set,
'$unset' : unset_generator()})
docs_count = docs_count - 1;
break
except pymongo.errors.DuplicateKeyError:
if --tries > 0:
continue
else:
print("Generator failed to produce a unique value after 10 tries. Aborting.")
exit(1)
def prepare_generators(m, d, p=[]):
"""Converts a nested map of string:string into a flat map of string:generator.
Nested keys are converted to flat using `.` notation (e.g. foo.bar) and
string values are converted to generators (see `str_to_generator`)
"""
for field_name, transformer_or_field in m.items():
if isinstance(transformer_or_field, dict):
x = prepare_generators(transformer_or_field, dict(), p+[field_name])
d = {**d, **x}
else:
generator = str_to_generator(fake, transformer_or_field)
d[".".join(p+[field_name])] = generator
return d
def create_set_generator(generator_cfg):
"""Converts a flat map of string:generators into a single generator function
which is designed for 'set' operation in a MongoDB update.
'False' generator values are discarded.
"""
def gen():
d = dict()
for field_name, transformer in generator_cfg.items():
if transformer != False:
d[field_name] = transformer()
return d
return gen
def create_unset_generator(generator_cfg):
"""Converts a flat map of string:generators into a single generator function
which is designed for 'unset' operation in a MongoDB update.
'False' generators are used to indicate that values should be 'unset'."""
def gen():
d = dict()
for field_name, transformer in generator_cfg.items():
if transformer == False:
d[field_name] = 1
return d
return gen
def apply_cfg(cfg):
"""Applies a fog config to a local MongoDB instance.
The structure of the config is as follows:
{'transform':
{'namespace':{'collection':{'field':'generator'}}}}
e.g.
{'transform':
{'my_db':{'users':{'email':'ascii_safe_email'}}}}
In this case, all records in 'my_db.users' will have a random email,
generated from `faker.ascii_safe_email` applied to the `email` field.
"""
# dbs
for db_name, db in cfg.get('transform').items():
print("db:", db_name )
# collections
for collection_name, collection in db.items():
print("- collection:", collection_name)
if collection == "delete":
vacate_collection(db_name, collection_name)
else:
generator_cfg = prepare_generators(collection, dict())
set_generator = create_set_generator(generator_cfg)
unset_generator = create_unset_generator(generator_cfg)
transform_values(db_name=db_name,
collection_name=collection_name,
set_generator=set_generator,
unset_generator=unset_generator,
filters=cfg.get('filters'))
return
def load_cfg(filename):
"""Safely load a yaml file"""
with open(filename, 'r') as stream:
try:
cfg = yaml.safe_load(stream)
return cfg
except yaml.YAMLError as exc:
print(exc)
return
def fog(fog_cfg_path):
"""Load a yaml file as a fog config and apply"""
cfg = load_cfg(fog_cfg_path)
apply_cfg(cfg)
return
if __name__ == '__main__':
# pylint: disable=invalid-name
parser = argparse.ArgumentParser()
parser.add_argument('--list', help='list mongodb tables', action='store_true',)
parser.add_argument('--test', help='test', action='store_true',)
parser.add_argument('--fog', help='config file')
args = parser.parse_args()
if args.fog:
print("Using fog config:", args.fog.strip())
fog(args.fog)
elif args.test:
print(fake.safe_image_url())
elif args.list:
print(client.list_database_names())
else:
print("No action specified")