utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. class FileRegionsMatcher(object):
  22. class FileRegionsDisposableGetter(object):
  23. def __init__(self, file_data):
  24. self.checksums = {}
  25. self.names = {}
  26. for each in file_data.iterate_regions():
  27. if each.get_checksum() not in self.checksums:
  28. self.checksums[each.get_checksum()] = []
  29. self.checksums[each.get_checksum()].append((each.get_id(), each.get_name()))
  30. if each.get_name() not in self.names:
  31. self.names[each.get_name()] = []
  32. self.names[each.get_name()].append((each.get_id(), each.get_checksum()))
  33. def get_next_id_once_by_checksum(self, checksum):
  34. if checksum not in self.checksums.keys():
  35. return None
  36. if len(self.checksums[checksum]) == 0:
  37. return None
  38. elem = self.checksums[checksum].pop(0)
  39. next_id = elem[0]
  40. next_name = elem[1]
  41. self.names[next_name].remove((next_id, checksum))
  42. return next_id
  43. def get_next_id_once_by_name(self, name):
  44. if name not in self.names.keys():
  45. return None
  46. if len(self.names[name]) == 0:
  47. return None
  48. elem = self.names[name].pop(0)
  49. next_id = elem[0]
  50. next_checksum = elem[1]
  51. self.checksums[next_checksum].remove((next_id, name))
  52. return next_id
  53. def __init__(self, file_data, prev_file_data):
  54. self.ids = [None] # add one to shift id from zero
  55. once_filter = self.FileRegionsDisposableGetter(prev_file_data)
  56. unmatched_region_ids = []
  57. for (ind, region) in enumerate(file_data.iterate_regions()):
  58. assert(ind + 1 == region.get_id())
  59. # Identify corresponding region in previous database (attempt by checksum)
  60. prev_id = once_filter.get_next_id_once_by_checksum(region.checksum)
  61. if prev_id != None:
  62. self.ids.append((prev_id, False))
  63. else:
  64. unmatched_region_ids.append(region.get_id())
  65. self.ids.append((None, True))
  66. # Identify corresponding region in previous database (attempt by name)
  67. for region_id in unmatched_region_ids:
  68. prev_id = once_filter.get_next_id_once_by_name(file_data.get_region(region_id).name)
  69. if prev_id != None:
  70. self.ids[region_id] = (prev_id, True)
  71. def get_prev_id(self, curr_id):
  72. return self.ids[curr_id][0]
  73. def is_matched(self, curr_id):
  74. return (self.ids[curr_id][0] != None)
  75. def is_modified(self, curr_id):
  76. return self.ids[curr_id][1]
  77. def check_db_metadata(loader, loader_prev):
  78. for each in loader.iterate_properties():
  79. prev = loader_prev.get_property(each.name)
  80. if prev != each.value:
  81. logging.warn("Previous data file has got different metadata:")
  82. logging.warn(" - identification of change trends can be not reliable")
  83. logging.warn(" - use 'info' tool to view more details")
  84. return 1
  85. return 0
  86. def preprocess_path(path):
  87. path = re.sub(r'''[\\]+''', "/", path)
  88. logging.info("Processing: " + path)
  89. return path
  90. def report_bad_path(path):
  91. logging.error("Specified path '" + path + "' is invalid: not found in the database records.")