export.py 4.4 KB

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