forked from jwallen/RMG-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importOldDatabase.py
113 lines (93 loc) · 3.53 KB
/
importOldDatabase.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
#!/usr/bin/env python
# encoding: utf-8
"""
This script imports an RMG-Java database from the output directory and saves
it in the input directory.
"""
import math
import os.path
import sys
import time
import subprocess
import os
from rmgpy.data.rmg import RMGDatabase
from rmgpy.kinetics import KineticsData
from rmgpy.data.kinetics import KineticsDatabase, KineticsGroups
################################################################################
def getUsername():
"""
Figure out what the current username is in the form "Richard West <[email protected]>"
It should be in the format "Richard West <[email protected]>". We do this by
interrogating git, if possible
"""
name = ''
email = ''
try:
p=subprocess.Popen('git config --get user.name'.split(),
stdout=subprocess.PIPE)
name = p.stdout.read()
except OSError:
pass
if name:
name = name.strip()
else:
print "Couldn't find user.name from git."
name = os.getlogin()
try:
p=subprocess.Popen('git config --get user.email'.split(),
stdout=subprocess.PIPE)
email = p.stdout.read()
except OSError:
pass
if email:
email = email.strip()
else:
print "Couldn't find user.email from git."
email = os.getlogin() + "@" + os.uname()[1]
return '{0} <{1}>'.format(name,email)
def setHistory(database, user):
"""
For each entry in each component of the database, add a note to the history
indicating the import event and its time.
"""
# Add history item to each entry in each database
event = [time.asctime(),user,'action','{0} imported this entry from the old RMG database.'.format(user)]
for depository in database.thermo.depository.values():
for label, entry in depository.entries.iteritems():
entry.history.append(event)
for library in database.thermo.libraries.values():
for label, entry in library.entries.iteritems():
entry.history.append(event)
for groups in database.thermo.groups.values():
for label, entry in groups.entries.iteritems():
entry.history.append(event)
for family in database.kinetics.families.values():
for label, entry in family.groups.entries.iteritems():
entry.history.append(event)
for label, entry in family.rules.entries.iteritems():
entry.history.append(event)
for depository in family.depositories.values():
for label, entry in depository.entries.iteritems():
entry.history.append(event)
for library in database.kinetics.libraries.values():
for label, entry in library.entries.iteritems():
entry.history.append(event)
groups = database.statmech.groups
for label, entry in groups.entries.iteritems():
entry.history.append(event)
for label, entry in database.forbiddenStructures.entries.iteritems():
entry.history.append(event)
################################################################################
if __name__ == '__main__':
#figure out the username
user = getUsername()
# Set the import and export paths
oldPath = 'output/RMG_database'
newPath = 'input'
print 'Loading old RMG-Java database...'
database = RMGDatabase()
database.loadOld(oldPath)
print 'Setting history of all entries in database...'
setHistory(database, user=user)
print 'Saving the new RMG-Py database...'
database.save(newPath)