-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimport.py
More file actions
64 lines (50 loc) · 1.5 KB
/
import.py
File metadata and controls
64 lines (50 loc) · 1.5 KB
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
#!/usr/bin/env python
"""Upload an example project into database
`AVALON_PROJECTS`, `AVALON_MONGO` and `AVALON_DB` are needed env-var
example:
$ python import.py batman
"""
import os
import argparse
import pymongo
from bson import json_util
CD = os.path.dirname(os.path.abspath(__file__))
AVALON_DEBUG = bool(os.getenv("AVALON_DEBUG"))
AVALON_PROJECTS = os.getenv("AVALON_PROJECTS", os.path.join(CD, "projects"))
AVALON_MONGO = os.getenv("AVALON_MONGO", "")
AVALON_DB = os.getenv("AVALON_DB", "avalon")
parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument("project", help="name of project to import")
kwargs = parser.parse_args()
fname = os.path.join(AVALON_PROJECTS, kwargs.project, "db.json")
mongoURI = AVALON_MONGO
database = AVALON_DB
if '@' in AVALON_MONGO:
host = AVALON_MONGO.split('@')[-1]
else:
host = AVALON_MONGO.split('//')[-1]
result = []
try:
client = pymongo.MongoClient(mongoURI)
db = client[database]
proj = db[kwargs.project]
data = []
with open(fname) as data_file:
for line in data_file:
data.append(json_util.loads(line))
result = proj.insert_many(data).inserted_ids
except Exception as error:
if AVALON_DEBUG:
raise
else:
pass
if result:
print("success")
print(" project: %s" % kwargs.project)
print(" from: %s" % fname)
print(" to: %s" % host)
else:
print("failed")
# Avoid stating the obvious
if not AVALON_DEBUG:
print("Run with AVALON_DEBUG=True for details")