-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddmodel.py
153 lines (138 loc) · 5.17 KB
/
addmodel.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
import os, json, shutil, sys
import os.path
os.environ['DJANGO_SETTINGS_MODULE']='infinitebrain.settings'
from modeldb.models import Model, ModelSpec, ModelRelation, Project, Citation
from django.contrib.auth.models import User
from django.utils import timezone
if len(sys.argv)>1: #and os.path.isfile(sys.argv[1]):
tempfile=sys.argv[1]
else:
print "no input given. nothing to do."
sys.exit()
MEDIA_PATH = '/project/infinitebrain/media/'
inbox='/project/infinitebrain/inbox/'
jsonfile=inbox + tempfile
lockfile=jsonfile+'.locked'
#print "jsonfile: " + jsonfile
if not os.path.isfile(jsonfile):
print "json specification not found. nothing to do."
sys.exit()
elif os.path.isfile(lockfile):
print "json specification locked. being processed elsewhere."
sys.exit()
else:
lock = open(lockfile, 'w+')
lock.close()
try:
json_data=open(jsonfile)
data = json.load(json_data)
json_data.close()
print "extracting model info on server..."
modelname = data['modelname']
username = data['username']
level = data['level']
notes = data['notes']
ispublished = data['ispublished']
projectname = data['projectname']
citationtitle = data['citationtitle']
citationstring = data['citationstring']
citationurl = data['citationurl']
citationabout = data['citationabout']
privacy = data['privacy']
if not privacy:
privacy = 'public'
sources = data['source']
if sources:
sources=str(sources).replace('[','').replace(']','').split(',')
# mat-file
tempmatfile = jsonfile.replace('.json','') + '.mat'
# d3 file
tempd3file = inbox + data['d3file']
# readme file
if (level=='mechanism'):
tempreadmefile = inbox + data['specfile']
else:
tempreadmefile = inbox + data['readmefile']
tags = data['tags']
tags = tags.replace(' ','').split(',')
# get user
u = User.objects.filter(username=username)[0]
# Project
if projectname:
p = Project.objects.filter(name=projectname)
if p:
p=p[0]
else:
print "creating new database record for project..."
p=Project(owner=u,name=projectname)
p.save()
print "creating new database record for model..."
m = Model(user=u,project=p,name=modelname,level=level,notes=notes,privacy=privacy,ispublished=(ispublished==1),date_added=timezone.now()) # ,d3file=d3file,readmefile=readmefile
else:
print "creating new database record for model..."
m = Model(user=u,name=modelname,level=level,notes=notes,privacy=privacy,ispublished=(ispublished==1),date_added=timezone.now()) # ,d3file=d3file,readmefile=readmefile
m.save()
m.tags.add(*tags)
m.save()
# Citation
if ispublished==1:
print "creating new database record for citation..."
c = Citation(model=m,title=citationtitle,citation=citationstring,url=citationurl,about=citationabout)
c.save()
# File handling
USER_MEDIA = 'user/' + username + '/models/'
if not os.path.isdir(MEDIA_PATH + USER_MEDIA):
os.makedirs(MEDIA_PATH + USER_MEDIA)
print "moving model to media archive..." #: " + MEDIA_PATH + USER_MEDIA
if (level=='mechanism'):
specfile = USER_MEDIA + 'model' + str(m.pk) + '_mech.txt'
mechfile = inbox + data['specfile']
if os.path.isfile(mechfile):
shutil.copy2(mechfile,MEDIA_PATH+specfile)
tempreadmefile = mechfile
else:
specfile = USER_MEDIA + 'model' + str(m.pk) + '_spec.json'
shutil.copy2(jsonfile,MEDIA_PATH+specfile)
os.remove(jsonfile)
if os.path.isfile(tempd3file):
m.d3file = USER_MEDIA + 'model' + str(m.pk) + '_d3.json'
shutil.copy2(tempd3file,MEDIA_PATH+str(m.d3file))
os.remove(tempd3file)
if os.path.isfile(tempreadmefile):
if (level=='mechanism'):
m.readmefile = specfile # USER_MEDIA + 'model' + str(m.pk) + '_mech.txt'
else:
m.readmefile = USER_MEDIA + 'model' + str(m.pk) + '_readme.txt'
shutil.copy2(tempreadmefile,MEDIA_PATH+str(m.readmefile))
os.remove(tempreadmefile)
m.save()
if (level=='mechanism'):
os.remove(mechfile)
# Specification Files
print "creating new database record for model specification..."
# json/text file
f=ModelSpec(model=m,file=specfile,date_added=timezone.now())
f.save()
# mat-file
if os.path.isfile(tempmatfile):
matfile = USER_MEDIA + 'model' + str(m.pk) + '_spec.mat'
shutil.copy2(tempmatfile,MEDIA_PATH+matfile)
os.remove(tempmatfile)
f2=ModelSpec(model=m,file=matfile,type='dsim-MATLAB',date_added=timezone.now())
f2.save()
# Model Relations
print "adding model relations based on parent_uids..."
try:
for source in sources:
src=Model.objects.get(pk=source.strip())
if src:
rel=ModelRelation(source=src,target=m)
rel.save()
print rel
except:
print "no model relations added."
print "success."
os.remove(lockfile)
except:
print "Unexpected error:", sys.exc_info()[0]
os.remove(lockfile)