forked from mapsme/cf_audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_features.py
executable file
·60 lines (48 loc) · 1.57 KB
/
update_features.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
#!/usr/bin/env python
import os
import sys
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, BASE_DIR)
PYTHON = 'python2.7'
VENV_DIR = os.path.join(BASE_DIR, 'venv', 'lib', PYTHON, 'site-packages')
if os.path.exists(VENV_DIR):
sys.path.insert(1, VENV_DIR)
import codecs
import datetime
import logging
import json
from www.db import Project, database
from www.util import update_features, update_features_cache
if len(sys.argv) < 3:
print "Usage: {} <project_id> <features.json> [<audit.json>]".format(sys.argv[0])
sys.exit(1)
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%H:%M:%S')
logging.info('Reading JSON files')
if sys.argv[2] == '-':
features = []
else:
with codecs.open(sys.argv[2], 'r', 'utf-8') as f:
features = json.load(f)['features']
audit = None
if len(sys.argv) > 3:
with codecs.open(sys.argv[3], 'r', 'utf-8') as f:
audit = json.load(f)
if not features and not audit:
logging.error("No features read")
sys.exit(2)
try:
project = Project.get(Project.name == sys.argv[1])
except Project.DoesNotExist:
logging.error("No such project: %s", sys.argv[1])
sys.exit(2)
logging.info('Updating features')
proj_audit = json.loads(project.audit or '{}')
if audit:
proj_audit.update(audit)
project.audit = json.dumps(proj_audit, ensure_ascii=False)
project.updated = datetime.datetime.utcnow().date()
with database.atomic():
update_features(project, features, proj_audit)
logging.info('Updating the feature cache')
update_features_cache(project)
project.save()