dbf.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import mpp.api
  8. import os.path
  9. import logging
  10. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable):
  11. def declare_configuration(self, parser):
  12. if self.get_action() == 'collect':
  13. dbfile_help = "Path to a database file to create and write [default: %default]."
  14. dbfile_prev_help = ("Path to database file with data collected for the past/previous code revision."
  15. " If it is set, the tool will do an incremental/iterative collection."
  16. " It may reduce the time of processing significantly [default: %default].")
  17. else:
  18. dbfile_help = "Path to a database file to read and process [default: %default]."
  19. dbfile_prev_help = ("Path to database file with data collected for the past/previous code revision."
  20. " It is used to identify and evaluate/analyze change trends. [default: %default].")
  21. parser.add_option("--db-file", "--dbf", default='./metrixpp.db',
  22. help=dbfile_help)
  23. parser.add_option("--db-file-prev", "--dbfp", default=None,
  24. help=dbfile_prev_help)
  25. self.parser = parser
  26. def configure(self, options):
  27. self.dbfile = options.__dict__['db_file']
  28. self.dbfile_prev = options.__dict__['db_file_prev']
  29. if self.dbfile_prev != None and os.path.exists(self.dbfile_prev) == False:
  30. self.parser.error("option --db-file-prev: File '{0}' does not exist".format(self.dbfile_prev))
  31. def initialize(self):
  32. if self.get_action() in ['collect', 'test']:
  33. if os.path.exists(self.dbfile):
  34. logging.warn("Removing existing file: " + self.dbfile)
  35. try:
  36. os.unlink(self.dbfile)
  37. except:
  38. logging.warn("Failure in removing file: " + self.dbfile)
  39. self.loader = mpp.api.Loader()
  40. created = self.loader.create_database(self.dbfile, previous_db = self.dbfile_prev)
  41. if created == False:
  42. self.parser.error("option --db-file: Can not create file '{0}'".format(self.dbfile))
  43. else:
  44. self.loader = mpp.api.Loader()
  45. if self.loader.open_database(self.dbfile) == False:
  46. self.parser.error("option --db-file: Can not open file '{0}'".format(self.dbfile))
  47. self.loader_prev = mpp.api.Loader()
  48. if self.dbfile_prev != None:
  49. if self.loader_prev.open_database(self.dbfile_prev) == False:
  50. self.parser.error("option --db-file-prev: Can not open file '{0}'".format(self.dbfile_prev))
  51. self._warn_on_metadata()
  52. def _warn_on_metadata(self):
  53. for each in self.loader.iterate_properties():
  54. prev = self.loader_prev.get_property(each.name)
  55. if prev != each.value:
  56. logging.warn("Data files have been created by different versions of the tool or with different settings.")
  57. logging.warn(" - identification of some change trends can be not reliable")
  58. logging.warn(" - use 'info' action to view more details")
  59. def get_dbfile_path(self):
  60. return self.dbfile
  61. def get_dbfile_prev_path(self):
  62. return self.dbfile_prev
  63. def get_loader(self):
  64. return self.loader
  65. def get_loader_prev(self, none_if_empty=False):
  66. if none_if_empty == True and self.dbfile_prev == None:
  67. return None
  68. return self.loader_prev