dbf.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if self.get_plugin_loader() != None:
  39. if os.path.exists(self.dbfile):
  40. logging.warn("Removing existing file: " + self.dbfile)
  41. # TODO can reuse existing db file to speed up the processing?
  42. # TODO add option to choose to remove or to overwrite?
  43. try:
  44. os.unlink(self.dbfile)
  45. except:
  46. logging.warn("Failure in removing file: " + self.dbfile)
  47. self.loader = mpp.api.Loader()
  48. created = self.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. else:
  52. self.loader_prev = mpp.api.Loader()
  53. if self.dbfile_prev != None:
  54. if self.loader_prev.open_database(self.dbfile_prev) == False:
  55. self.parser.error("Can not open file: " + self.dbfile_prev)
  56. self.loader = mpp.api.Loader()
  57. if self.loader.open_database(self.dbfile) == False:
  58. self.parser.error("Can not open file: " + self.dbfile)
  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