dbf.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if self.get_action() == 'collect':
  25. dbfile_help = "Path to a database file to create and write [default: %default]."
  26. dbfile_prev_help = ("Path to database file with data collected for the past/previous code revision."
  27. " If it is set, the tool will do an incremental/iterative collection."
  28. " It may reduce the time of processing significantly [default: %default].")
  29. else:
  30. dbfile_help = "Path to a database file to read and process [default: %default]."
  31. dbfile_prev_help = ("Path to database file with data collected for the past/previous code revision."
  32. " It is used to identify and evaluate/analyze change trends. [default: %default].")
  33. parser.add_option("--db-file", "--dbf", default='./metrixpp.db',
  34. help=dbfile_help)
  35. parser.add_option("--db-file-prev", "--dbfp", default=None,
  36. help=dbfile_prev_help)
  37. self.parser = parser
  38. def configure(self, options):
  39. self.dbfile = options.__dict__['db_file']
  40. self.dbfile_prev = options.__dict__['db_file_prev']
  41. if self.dbfile_prev != None and os.path.exists(self.dbfile_prev) == False:
  42. self.parser.error("option --db-file-prev: File '{0}' does not exist".format(self.dbfile_prev))
  43. def initialize(self):
  44. if self.get_action() in ['collect', 'test']:
  45. if os.path.exists(self.dbfile):
  46. logging.warn("Removing existing file: " + self.dbfile)
  47. try:
  48. os.unlink(self.dbfile)
  49. except:
  50. logging.warn("Failure in removing file: " + self.dbfile)
  51. self.loader = mpp.api.Loader()
  52. created = self.loader.create_database(self.dbfile, previous_db = self.dbfile_prev)
  53. if created == False:
  54. self.parser.error("option --db-file: Can not create file '{0}'".format(self.dbfile))
  55. else:
  56. self.loader = mpp.api.Loader()
  57. if self.loader.open_database(self.dbfile) == False:
  58. self.parser.error("option --db-file: Can not open file '{0}'".format(self.dbfile))
  59. self.loader_prev = mpp.api.Loader()
  60. if self.dbfile_prev != None:
  61. if self.loader_prev.open_database(self.dbfile_prev) == False:
  62. self.parser.error("option --db-file-prev: Can not open file '{0}'".format(self.dbfile_prev))
  63. self._warn_on_metadata()
  64. def _warn_on_metadata(self):
  65. for each in self.loader.iterate_properties():
  66. prev = self.loader_prev.get_property(each.name)
  67. if prev != each.value:
  68. logging.warn("Data files have been created by different versions of the tool or with different settings.")
  69. logging.warn(" - identification of some change trends can be not reliable")
  70. logging.warn(" - use 'info' action to view more details")
  71. def get_dbfile_path(self):
  72. return self.dbfile
  73. def get_dbfile_prev_path(self):
  74. return self.dbfile_prev
  75. def get_loader(self):
  76. return self.loader
  77. def get_loader_prev(self, none_if_empty=False):
  78. if none_if_empty == True and self.dbfile_prev == None:
  79. return None
  80. return self.loader_prev