PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/importOldDatabase.py

https://github.com/shamelmerchant/RMG-database
Python | 224 lines | 211 code | 5 blank | 8 comment | 2 complexity | 9270b75f5214aba0679d7992ef37e81d MD5 | raw file
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. This script imports an RMG-Java database from the output directory and saves
  5. it in the input directory.
  6. """
  7. import math
  8. import os.path
  9. import sys
  10. import time
  11. import subprocess
  12. import os
  13. from rmgpy.kinetics import KineticsData
  14. from rmgpy.data.kinetics import KineticsDatabase, KineticsGroups
  15. from rmgpy.data.rmg import RMGDatabase
  16. ################################################################################
  17. def getUsername():
  18. """
  19. Figure out what the current username is in the form "Richard West <rwest@mit.edu>"
  20. It should be in the format "Richard West <rwest@mit.edu>". We do this by
  21. interrogating git, if possible
  22. """
  23. name = ''
  24. email = ''
  25. try:
  26. p=subprocess.Popen('git config --get user.name'.split(),
  27. stdout=subprocess.PIPE)
  28. name = p.stdout.read()
  29. except OSError:
  30. pass
  31. if name:
  32. name = name.strip()
  33. else:
  34. raise Exception("Couldn't find user.name from git. Please add user info to git or fill user info manually.")
  35. try:
  36. p=subprocess.Popen('git config --get user.email'.split(),
  37. stdout=subprocess.PIPE)
  38. email = p.stdout.read()
  39. except OSError:
  40. pass
  41. if email:
  42. email = email.strip()
  43. else:
  44. raise Exception("Couldn't find user.email from git. Please add email to git or fill email info manually.")
  45. return '{0} <{1}>'.format(name,email)
  46. from cStringIO import StringIO
  47. def compare(entry1,entry2,saver):
  48. """Compare entries entry1 and entry2 according to the saveEntry function saver"""
  49. history1, history2 = entry1.history, entry2.history
  50. entry1.history = []
  51. entry2.history = []
  52. s1 = StringIO()
  53. saver(s1,entry1)
  54. s2 = StringIO()
  55. saver(s2,entry2)
  56. equal = False
  57. if s1.getvalue() == s2.getvalue():
  58. equal = True
  59. s1.close()
  60. s2.close()
  61. entry1.history, entry2.history = history1, history2
  62. return equal
  63. def setHistory(database, current_database, user):
  64. """
  65. For each entry in each component of the database, add a note to the history
  66. indicating the import event and its time, if it's different than what's in the current_database
  67. """
  68. # Add history item to each entry in each database
  69. event = [time.asctime(),user,'action','{0} updated this entry by importing the old RMG database.'.format(user)]
  70. event_new = [time.asctime(),user,'action','{0} added this entry by importing the old RMG database.'.format(user)]
  71. for depository in database.thermo.depository.values():
  72. for label, entry in depository.entries.iteritems():
  73. raise NotImplementedError("Wasn't expecting depositories in the old database, so haven't implemented change detection")
  74. entry.history.append(event)
  75. for key, library in database.thermo.libraries.iteritems():
  76. try:
  77. old_library = current_database.thermo.libraries[key].entries
  78. except KeyError:
  79. old_library = {}
  80. for label, entry in library.entries.iteritems():
  81. try:
  82. old_entry = old_library[label]
  83. except KeyError:
  84. entry.history.append(event_new)
  85. continue # next in for loop
  86. entry.history.extend(old_entry.history)
  87. if compare(old_entry, entry, library.saveEntry):
  88. print key, label, "has not changed."
  89. else:
  90. entry.history.append(event)
  91. for key, groups in database.thermo.groups.iteritems():
  92. try:
  93. old_groups = current_database.thermo.groups[key].entries
  94. except KeyError:
  95. old_groups = {}
  96. for label, entry in groups.entries.iteritems():
  97. try:
  98. old_entry = old_groups[label]
  99. except KeyError:
  100. entry.history.append(event_new)
  101. continue # next in for loop
  102. entry.history.extend(old_entry.history)
  103. if compare(old_entry, entry, groups.saveEntry):
  104. print key, label, "has not changed."
  105. else:
  106. entry.history.append(event)
  107. for key, family in database.kinetics.families.iteritems():
  108. try:
  109. old_groups = current_database.kinetics.families[key].groups.entries
  110. except KeyError:
  111. old_groups = {}
  112. for label, entry in family.groups.entries.iteritems():
  113. try:
  114. old_entry = old_groups[label]
  115. except KeyError:
  116. entry.history.append(event_new)
  117. continue # next in for loop
  118. entry.history.extend(old_entry.history)
  119. if compare(old_entry, entry, family.rules.saveEntry): # family.groups has no .saveEntry?
  120. print key, label, "has not changed."
  121. else:
  122. entry.history.append(event)
  123. try:
  124. old_rules = current_database.kinetics.families[key].rules.entries
  125. except KeyError:
  126. old_rules = {}
  127. for label, entry in family.rules.entries.iteritems():
  128. try:
  129. old_entry = old_rules[label]
  130. except KeyError:
  131. for item in entry:
  132. item.history.append(event_new)
  133. continue # next in for loop
  134. for item in entry:
  135. for old_item in old_entry:
  136. if old_item.index == item.index:
  137. print "Found what's meant to be the same thing"
  138. item.history.extend(old_item.history)
  139. if compare(old_item, item, family.rules.saveEntry):
  140. print key, label, "has not changed."
  141. else:
  142. item.history.append(event)
  143. break
  144. else:
  145. "Couldn't find corresponding item with same index."
  146. item.history.append(event_new)
  147. for depository in family.depositories.values():
  148. for label, entry in depository.entries.iteritems():
  149. raise NotImplementedError("Wasn't expecting depositories in the old database, so haven't implemented change detection")
  150. entry.history.append(event)
  151. for key, library in database.kinetics.libraries.iteritems():
  152. try:
  153. old_library = current_database.kinetics.libraries[key].entries
  154. except KeyError:
  155. old_library = {}
  156. for label, entry in library.entries.iteritems():
  157. #print "What is the label?",label
  158. # is this useful for looking things up?
  159. try:
  160. old_entry = old_library[label]
  161. except KeyError:
  162. entry.history.append(event_new)
  163. continue # next in for loop
  164. entry.history.extend(old_entry.history)
  165. if compare(old_entry, entry, library.saveEntry):
  166. print key, label, "has not changed."
  167. else:
  168. entry.history.append(event)
  169. groups = database.statmech.groups
  170. for label, entry in groups.entries.iteritems():
  171. entry.history.append(event)
  172. for label, entry in database.forbiddenStructures.entries.iteritems():
  173. entry.history.append(event)
  174. ################################################################################
  175. if __name__ == '__main__':
  176. #figure out the username
  177. user = getUsername()
  178. # Comment out the line above and uncomment the one below, filling in user information manually if
  179. # it cannot be obtained from git or you do not want to extract user info from git.
  180. # user = "John Doe <john@email.com>"
  181. # Set the import and export paths
  182. oldPath = 'output/RMG_database'
  183. newPath = 'input'
  184. print 'Loading old RMG-Java database...'
  185. database = RMGDatabase()
  186. database.loadOld(oldPath)
  187. print 'Loading the current database to look for changes...'
  188. current_database = RMGDatabase()
  189. current_database.load(newPath)
  190. print 'Setting history of all entries in database...'
  191. setHistory(database, current_database, user=user)
  192. print 'Saving the new RMG-Py database...'
  193. database.save(newPath)
  194. print "Done!"