export.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 mpp.utils
  21. import csv
  22. class Plugin(mpp.api.Plugin, mpp.api.IRunable):
  23. def run(self, args):
  24. self.loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
  25. self.loader = self.get_plugin('mpp.dbf').get_loader()
  26. paths = None
  27. if len(args) == 0:
  28. paths = [""]
  29. else:
  30. paths = args
  31. return self._export_to_stdout(paths)
  32. def _export_to_stdout(self, paths):
  33. class StdoutWriter(object):
  34. def write(self, *args, **kwargs):
  35. print args[0].strip()
  36. exit_code = 0
  37. columns = []
  38. columnNames = ["file", "region", "type", "modified", "line start", "line end"]
  39. for name in self.loader.iterate_namespace_names():
  40. namespace = self.loader.get_namespace(name)
  41. for field in namespace.iterate_field_names():
  42. columns.append((name, field))
  43. columnNames.append(name + ":" + field)
  44. writer = StdoutWriter()
  45. csvWriter = csv.writer(writer)
  46. csvWriter.writerow(columnNames)
  47. for path in paths:
  48. path = mpp.utils.preprocess_path(path)
  49. files = self.loader.iterate_file_data(path)
  50. if files == None:
  51. mpp.utils.report_bad_path(path)
  52. exit_code += 1
  53. continue
  54. for file_data in files:
  55. matcher = None
  56. file_data_prev = self.loader_prev.load_file_data(file_data.get_path())
  57. if file_data_prev != None:
  58. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  59. for reg in file_data.iterate_regions():
  60. per_reg_data = []
  61. per_reg_data.append(mpp.api.Region.T().to_str(reg.get_type()))
  62. if matcher != None and matcher.is_matched(reg.get_id()):
  63. per_reg_data.append(matcher.is_modified(reg.get_id()))
  64. else:
  65. per_reg_data.append(None)
  66. per_reg_data.append(reg.get_line_begin())
  67. per_reg_data.append(reg.get_line_end())
  68. for column in columns:
  69. per_reg_data.append(reg.get_data(column[0], column[1]))
  70. csvWriter.writerow([file_data.get_path(), reg.get_name()] + per_reg_data)
  71. per_file_data = []
  72. per_file_data.append('file')
  73. if file_data_prev != None:
  74. per_file_data.append(file_data.get_checksum() != file_data_prev.get_checksum())
  75. else:
  76. per_file_data.append(None)
  77. per_file_data.append(file_data.get_region(1).get_line_begin())
  78. per_file_data.append(file_data.get_region(1).get_line_end())
  79. for column in columns:
  80. per_file_data.append(file_data.get_data(column[0], column[1]))
  81. csvWriter.writerow([file_data.get_path(), None] + per_file_data)
  82. return exit_code