utils.py 4.6 KB

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