utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.internal.py2xml
  20. import mpp.internal.py2txt
  21. import logging
  22. import re
  23. class FileRegionsMatcher(object):
  24. class FileRegionsDisposableGetter(object):
  25. def __init__(self, file_data):
  26. self.checksums = {}
  27. self.names = {}
  28. for each in file_data.iterate_regions():
  29. if each.get_checksum() not in self.checksums:
  30. self.checksums[each.get_checksum()] = []
  31. self.checksums[each.get_checksum()].append((each.get_id(), each.get_name()))
  32. if each.get_name() not in self.names:
  33. self.names[each.get_name()] = []
  34. self.names[each.get_name()].append((each.get_id(), each.get_checksum()))
  35. def get_next_id_once_by_checksum(self, checksum):
  36. if checksum not in self.checksums.keys():
  37. return None
  38. if len(self.checksums[checksum]) == 0:
  39. return None
  40. elem = self.checksums[checksum].pop(0)
  41. next_id = elem[0]
  42. next_name = elem[1]
  43. self.names[next_name].remove((next_id, checksum))
  44. return next_id
  45. def get_next_id_once_by_name(self, name):
  46. if name not in self.names.keys():
  47. return None
  48. if len(self.names[name]) == 0:
  49. return None
  50. elem = self.names[name].pop(0)
  51. next_id = elem[0]
  52. next_checksum = elem[1]
  53. self.checksums[next_checksum].remove((next_id, name))
  54. return next_id
  55. def __init__(self, file_data, prev_file_data):
  56. self.ids = [None] # add one to shift id from zero
  57. once_filter = self.FileRegionsDisposableGetter(prev_file_data)
  58. unmatched_region_ids = []
  59. for (ind, region) in enumerate(file_data.iterate_regions()):
  60. assert(ind + 1 == region.get_id())
  61. # Identify corresponding region in previous database (attempt by checksum)
  62. prev_id = once_filter.get_next_id_once_by_checksum(region.checksum)
  63. if prev_id != None:
  64. self.ids.append((prev_id, False))
  65. else:
  66. unmatched_region_ids.append(region.get_id())
  67. self.ids.append((None, True))
  68. # Identify corresponding region in previous database (attempt by name)
  69. for region_id in unmatched_region_ids:
  70. prev_id = once_filter.get_next_id_once_by_name(file_data.get_region(region_id).name)
  71. if prev_id != None:
  72. self.ids[region_id] = (prev_id, True)
  73. def get_prev_id(self, curr_id):
  74. return self.ids[curr_id][0]
  75. def is_matched(self, curr_id):
  76. return (self.ids[curr_id][0] != None)
  77. def is_modified(self, curr_id):
  78. return self.ids[curr_id][1]
  79. def preprocess_path(path):
  80. path = re.sub(r'''[\\]+''', "/", path)
  81. logging.info("Processing: " + path)
  82. return path
  83. def report_bad_path(path):
  84. logging.error("Specified path '" + path + "' is invalid: not found in the database records.")
  85. def serialize_to_xml(data, root_name = None):
  86. serializer = mpp.internal.py2xml.Py2XML()
  87. return serializer.parse(data, objName=root_name)
  88. def serialize_to_python(data, root_name = None):
  89. prefix = ""
  90. postfix = ""
  91. if root_name != None:
  92. prefix = "{'" + root_name + ": "
  93. postfix = "}"
  94. return prefix + data.__repr__() + postfix
  95. def serialize_to_txt(data, root_name = None):
  96. serializer = mpp.internal.py2txt.Py2TXT()
  97. return serializer.parse(data, objName=root_name, indent = -1)