summaryrefslogtreecommitdiff
path: root/python/collaboration/merge.py
blob: af06d602ad5028e03a82bfefa7b651634ec8bd73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import abc
import ctypes
import json
import sys
import traceback
from typing import Dict, Optional

from .. import _binaryninjacore as core
from ..enums import MergeConflictDataType
from . import util

from ..database import Database, Snapshot
from ..filemetadata import FileMetadata

OptionalStringDict = Optional[Dict[str, object]]


class MergeConflict:
	"""
	Structure representing an individual merge conflict
	"""
	def __init__(self, handle: core.BNAnalysisMergeConflictHandle):
		"""
		FFI constructor

		:param handle: FFI handle for internal use
		"""
		self._handle = ctypes.cast(handle, core.BNAnalysisMergeConflictHandle)

	def __del__(self):
		core.BNFreeAnalysisMergeConflict(self._handle)

	@property
	def database(self) -> Database:
		"""
		Database backing all snapshots in the merge conflict

		:return: Database object
		"""
		result = core.BNAnalysisMergeConflictGetDatabase(self._handle)
		return Database(handle=ctypes.cast(result, ctypes.POINTER(core.BNDatabase)))

	@property
	def base_snapshot(self) -> Optional[Snapshot]:
		"""
		Snapshot which is the parent of the two being merged

		:return: Snapshot object
		"""
		result = core.BNAnalysisMergeConflictGetBaseSnapshot(self._handle)
		if result is None:
			return None
		return Snapshot(handle=ctypes.cast(result, ctypes.POINTER(core.BNSnapshot)))

	@property
	def first_snapshot(self) -> Optional[Snapshot]:
		"""
		First snapshot being merged

		:return: Snapshot object
		"""
		result = core.BNAnalysisMergeConflictGetFirstSnapshot(self._handle)
		if result is None:
			return None
		return Snapshot(handle=ctypes.cast(result, ctypes.POINTER(core.BNSnapshot)))

	@property
	def second_snapshot(self) -> Optional[Snapshot]:
		"""
		Second snapshot being merged

		:return: Snapshot object
		"""
		result = core.BNAnalysisMergeConflictGetSecondSnapshot(self._handle)
		if result is None:
			return None
		return Snapshot(handle=ctypes.cast(result, ctypes.POINTER(core.BNSnapshot)))

	@property
	def base_file(self) -> Optional[FileMetadata]:
		"""
		FileMetadata with contents of file for base snapshot
		This function is slow! Only use it if you really need it.

		:return: FileMetadata object
		"""
		result = core.BNAnalysisMergeConflictGetBaseFile(self._handle)
		if result is None:
			return None
		lazy = util.LazyT(handle=result)
		file = FileMetadata(handle=ctypes.cast(lazy.get(ctypes.POINTER(core.BNFileMetadata)), ctypes.POINTER(core.BNFileMetadata)))
		core.BNCollaborationFreeLazyT(result)
		return file

	@property
	def first_file(self) -> Optional[FileMetadata]:
		"""
		FileMetadata with contents of file for first snapshot
		This function is slow! Only use it if you really need it.

		:return: FileMetadata object
		"""
		result = core.BNAnalysisMergeConflictGetFirstFile(self._handle)
		if result is None:
			return None
		lazy = util.LazyT(handle=result)
		file = FileMetadata(handle=ctypes.cast(lazy.get(ctypes.POINTER(core.BNFileMetadata)), ctypes.POINTER(core.BNFileMetadata)))
		core.BNCollaborationFreeLazyT(result)
		return file

	@property
	def second_file(self) -> Optional[FileMetadata]:
		"""
		FileMetadata with contents of file for second snapshot
		This function is slow! Only use it if you really need it.

		:return: FileMetadata object
		"""
		result = core.BNAnalysisMergeConflictGetSecondFile(self._handle)
		if result is None:
			return None
		lazy = util.LazyT(handle=result)
		file = FileMetadata(handle=ctypes.cast(lazy.get(ctypes.POINTER(core.BNFileMetadata)), ctypes.POINTER(core.BNFileMetadata)))
		core.BNCollaborationFreeLazyT(result)
		return file

	@property
	def base(self) -> OptionalStringDict:
		"""
		Json object for conflicting data in the base snapshot

		:return: Python dictionary from parsed json
		"""
		result = core.BNAnalysisMergeConflictGetBase(self._handle)
		if result is None:
			return None
		return json.loads(result)

	@property
	def first(self) -> OptionalStringDict:
		"""
		Json object for conflicting data in the first snapshot

		:return: Python dictionary from parsed json
		"""
		result = core.BNAnalysisMergeConflictGetFirst(self._handle)
		if result is None:
			return None
		return json.loads(result)

	@property
	def second(self) -> OptionalStringDict:
		"""
		Json object for conflicting data in the second snapshot

		:return: Python dictionary from parsed json
		"""
		result = core.BNAnalysisMergeConflictGetSecond(self._handle)
		if result is None:
			return None
		return json.loads(result)

	@property
	def data_type(self) -> 'MergeConflictDataType':
		"""
		Type of data in the conflict, Text/Json/Binary

		:return: Conflict data type
		"""
		return MergeConflictDataType(core.BNAnalysisMergeConflictGetDataType(self._handle))

	@property
	def type(self) -> str:
		"""
		String representing the type name of the data, not the same as data_type.
		This is like "typeName" or "tag" depending on what object the conflict represents.

		:return: Type name
		"""
		return core.BNAnalysisMergeConflictGetType(self._handle)

	@property
	def key(self) -> str:
		"""
		Lookup key for the merge conflict, ideally a tree path that contains the name of the conflict
		and all the recursive children leading up to this conflict.

		:return: Key name
		"""
		return core.BNAnalysisMergeConflictGetKey(self._handle)

	def success(self, value: OptionalStringDict) -> bool:
		"""
		Call this when you've resolved the conflict to save the result

		:param value: Resolved value
		:return: True if successful
		"""
		if value is None:
			printed = None
		else:
			printed = json.dumps(value)
		return core.BNAnalysisMergeConflictSuccess(self._handle, printed)

	def get_path_item(self, path_key: str) -> Optional[object]:
		"""
		Get item in the merge conflict's path for a given key.

		:param path_key: Key for path item
		:return: Path item, or an None if not found
		"""
		value = core.BNAnalysisMergeConflictGetPathItem(self._handle, path_key)
		if value is None:
			return None
		return ctypes.py_object(value)


class ConflictHandler:
	"""
	Helper class that resolves conflicts
	"""
	def _handle(self, ctxt: ctypes.c_void_p, keys: ctypes.POINTER(ctypes.c_char_p), conflicts: ctypes.POINTER(core.BNAnalysisMergeConflictHandle), count: ctypes.c_ulonglong) -> bool:
		try:
			py_conflicts = {}
			for i in range(count.value):
				py_conflicts[core.pyNativeStr(keys[i])] = MergeConflict(handle=conflicts[i])
			return self.handle(py_conflicts)
		except:
			traceback.print_exc(file=sys.stderr)
			return False

	@abc.abstractmethod
	def handle(self, conflicts: Dict[str, MergeConflict]) -> bool:
		"""
		Handle any merge conflicts by calling their success() function with a merged value

		:param conflicts: Map of conflict id to conflict structure
		:return: True if all conflicts were successfully merged
		"""
		raise NotImplementedError("Not implemented")