export.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 logging
  20. import csv
  21. import core.api
  22. import core.log
  23. import core.db.post
  24. import core.cmdparser
  25. import core.utils
  26. class Tool(core.api.ITool):
  27. def run(self, tool_args):
  28. return main(tool_args)
  29. def main(tool_args):
  30. log_plugin = core.log.Plugin()
  31. db_plugin = core.db.post.Plugin()
  32. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog export [options] -- [path 1] ... [path N]")
  33. log_plugin.declare_configuration(parser)
  34. db_plugin.declare_configuration(parser)
  35. parser.add_option("--format", "--ft", default='csv', choices=['csv', 'xml'], help="Format of the output data. "
  36. "Possible values are 'xml' and 'csv' [default: %default]")
  37. (options, args) = parser.parse_args(tool_args)
  38. log_plugin.configure(options)
  39. db_plugin.configure(options)
  40. out_format = options.__dict__['format']
  41. loader_prev = core.api.Loader()
  42. if db_plugin.dbfile_prev != None:
  43. if loader_prev.open_database(db_plugin.dbfile_prev) == False:
  44. parser.error("Can not open file: " + db_plugin.dbfile_prev)
  45. loader = core.api.Loader()
  46. if loader.open_database(db_plugin.dbfile) == False:
  47. parser.error("Can not open file: " + db_plugin.dbfile)
  48. # Check for versions consistency
  49. for each in loader.iterate_properties():
  50. if db_plugin.dbfile_prev != None:
  51. prev = loader_prev.get_property(each.name)
  52. if prev != each.value:
  53. logging.warn("Previous data has got different metadata:")
  54. logging.warn(" - identification of change trends can be not reliable")
  55. logging.warn(" - use 'info' tool to get more details")
  56. break
  57. paths = None
  58. if len(args) == 0:
  59. paths = [""]
  60. else:
  61. paths = args
  62. exit_code = export_to_stdout(out_format, paths, loader, loader_prev)
  63. return exit_code
  64. def export_to_stdout(out_format, paths, loader, loader_prev):
  65. class StdoutWriter(object):
  66. def write(self, *args, **kwargs):
  67. print args[0],
  68. exit_code = 0
  69. columnNames = ["file", "region", ]
  70. columns = []
  71. for name in loader.iterate_namespace_names():
  72. namespace = loader.get_namespace(name)
  73. for field in namespace.iterate_field_names():
  74. columns.append((name, field, namespace.are_regions_supported()))
  75. columnNames.append(name + ":" + field)
  76. writer = StdoutWriter()
  77. csvWriter = csv.writer(writer)
  78. csvWriter.writerow(columnNames)
  79. if out_format == 'xml':
  80. print "<export>\n"
  81. elif out_format == 'csv':
  82. print "CSV"
  83. else:
  84. assert False, "Unknown output format " + out_format
  85. for path in paths:
  86. path = core.utils.preprocess_path(path)
  87. files = loader.iterate_file_data(path)
  88. if files != None:
  89. for file_data in files:
  90. for reg in file_data.iterate_regions():
  91. per_reg_data = []
  92. for column in columns:
  93. per_reg_data.append(reg.get_data(column[0], column[1]))
  94. csvWriter.writerow([file_data.get_path(), reg.get_name()] + per_reg_data)
  95. per_file_data = []
  96. for column in columns:
  97. per_file_data.append(file_data.get_data(column[0], column[1]))
  98. csvWriter.writerow([file_data.get_path(), None] + per_file_data)
  99. else:
  100. core.utils.report_bad_path(path)
  101. exit_code += 1
  102. if out_format == 'xml':
  103. print "XML"
  104. return 0