post.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 core.api
  20. import os.path
  21. import re
  22. import logging
  23. class Plugin(core.api.Plugin, core.api.IConfigurable):
  24. def declare_configuration(self, parser):
  25. parser.add_option("--db-file", "--dbf", default='./metrixpp.db',
  26. help="Primary database file to write (by the collector) and post-process (by other tools) [default: %default]")
  27. parser.add_option("--db-file-prev", "--dbfp", default=None,
  28. help="Database file with data collected for the past/previous revision."
  29. " If it is set for the collector tool to perform an incremental/iterative collection,"
  30. " it may reduce the processing time significantly."
  31. " Post-processing tools use it in order to recognise/evaluate change trends. [default: %default].")
  32. self.parser = parser
  33. def configure(self, options):
  34. self.dbfile = options.__dict__['db_file']
  35. self.dbfile_prev = options.__dict__['db_file_prev']
  36. if self.dbfile_prev != None and os.path.exists(self.dbfile_prev) == False:
  37. self.parser.error("File does not exist:" + self.dbfile_prev)
  38. def initialize(self):
  39. if self.get_plugin_loader() != None:
  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. created = self.get_plugin_loader().get_database_loader().create_database(self.dbfile, previous_db = self.dbfile_prev)
  49. if created == False:
  50. self.parser.error("Failure in creating file: " + self.dbfile)
  51. # do not process files dumped by this module
  52. self.get_plugin_loader().get_plugin('core.dir').add_exclude_rule(re.compile(r'^' + os.path.basename(self.dbfile) + r'$'))
  53. if self.dbfile_prev != None:
  54. self.get_plugin_loader().get_plugin('core.dir').add_exclude_rule(re.compile(r'^' + os.path.basename(self.dbfile_prev) + r'$'))
  55. else:
  56. self.loader_prev = core.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.loader = core.api.Loader()
  61. if self.loader.open_database(self.dbfile) == False:
  62. self.parser.error("Can not open file: " + self.dbfile)
  63. def get_loader(self):
  64. return self.loader
  65. def get_loader_prev(self):
  66. return self.loader_prev