dbf.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import mpp.api
  20. import os.path
  21. import logging
  22. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable):
  23. def declare_configuration(self, parser):
  24. parser.add_option("--db-file", "--dbf", default='./metrixpp.db',
  25. help="Primary database file to write (by the collector) and post-process (by other tools) [default: %default]")
  26. parser.add_option("--db-file-prev", "--dbfp", default=None,
  27. help="Database file with data collected for the past/previous revision."
  28. " If it is set for the collector tool to perform an incremental/iterative collection,"
  29. " it may reduce the processing time significantly."
  30. " Post-processing tools use it in order to recognise/evaluate change trends. [default: %default].")
  31. self.parser = parser
  32. def configure(self, options):
  33. self.dbfile = options.__dict__['db_file']
  34. self.dbfile_prev = options.__dict__['db_file_prev']
  35. if self.dbfile_prev != None and os.path.exists(self.dbfile_prev) == False:
  36. self.parser.error("File does not exist:" + self.dbfile_prev)
  37. def initialize(self):
  38. # TODO refactor and remove self.get_plugin_loader() != None
  39. if self.get_plugin_loader() != None and self.get_plugin_loader().get_action() == 'collect':
  40. if os.path.exists(self.dbfile):
  41. logging.warn("Removing existing file: " + self.dbfile)
  42. # TODO can reuse existing db file to speed up the processing?
  43. # TODO add option to choose to remove or to overwrite?
  44. try:
  45. os.unlink(self.dbfile)
  46. except:
  47. logging.warn("Failure in removing file: " + self.dbfile)
  48. self.loader = mpp.api.Loader()
  49. created = self.loader.create_database(self.dbfile, previous_db = self.dbfile_prev)
  50. if created == False:
  51. self.parser.error("Failure in creating file: " + self.dbfile)
  52. else:
  53. self.loader = mpp.api.Loader()
  54. if self.loader.open_database(self.dbfile) == False:
  55. self.parser.error("Can not open file: " + self.dbfile)
  56. self.loader_prev = mpp.api.Loader()
  57. if self.dbfile_prev != None:
  58. if self.loader_prev.open_database(self.dbfile_prev) == False:
  59. self.parser.error("Can not open file: " + self.dbfile_prev)
  60. self._warn_on_metadata()
  61. def _warn_on_metadata(self):
  62. for each in self.loader.iterate_properties():
  63. prev = self.loader_prev.get_property(each.name)
  64. if prev != each.value:
  65. logging.warn("Data files have been created by different versions of the tool or with different settings.")
  66. logging.warn(" - identification of some change trends can be not reliable")
  67. logging.warn(" - use 'info' action to view more details")
  68. def get_dbfile_path(self):
  69. return self.dbfile
  70. def get_dbfile_prev_path(self):
  71. return self.dbfile_prev
  72. def get_loader(self):
  73. return self.loader
  74. def get_loader_prev(self, none_if_empty=False):
  75. if none_if_empty == True and self.dbfile_prev == None:
  76. return None
  77. return self.loader_prev