File: _ra_iter_log.c

package info (click to toggle)
subvertpy 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,168 kB
  • sloc: ansic: 10,442; python: 3,713; makefile: 57; sh: 17
file content (314 lines) | stat: -rw-r--r-- 8,072 bytes parent folder | download
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright © 2010 Jelmer Vernooij <jelmer@jelmer.uk>
 * -*- coding: utf-8 -*-
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
#include <pythread.h>

struct log_entry {
	PyObject *tuple;
	struct log_entry *next;
};

typedef struct {
	PyObject_VAR_HEAD
	svn_revnum_t start, end;
	svn_boolean_t discover_changed_paths;
	svn_boolean_t strict_node_history;
	svn_boolean_t include_merged_revisions;
	int limit;
	apr_pool_t *pool;
	apr_array_header_t *apr_paths;
	apr_array_header_t *apr_revprops;
	RemoteAccessObject *ra;
	svn_boolean_t done;
	PyObject *exc_type;
	PyObject *exc_val;
	int queue_size;
	struct log_entry *head;
	struct log_entry *tail;
} LogIteratorObject;

static void log_iter_dealloc(PyObject *self)
{
	LogIteratorObject *iter = (LogIteratorObject *)self;

	while (iter->head) {
		struct log_entry *e = iter->head;
		Py_DECREF(e->tuple);
		iter->head = e->next;
		free(e);
	}
	Py_XDECREF(iter->exc_type);
	Py_XDECREF(iter->exc_val);
	apr_pool_destroy(iter->pool);
	Py_DECREF(iter->ra);
	PyObject_Del(iter);
}

static PyObject *log_iter_next(LogIteratorObject *iter)
{
	struct log_entry *first;
	PyObject *ret;
	Py_INCREF(iter);

	while (iter->head == NULL) {
		/* Done, raise exception */
		if (iter->exc_type != NULL) {
			PyErr_SetObject(iter->exc_type, iter->exc_val);
			Py_DECREF(iter);
			return NULL;
		} else {
			Py_BEGIN_ALLOW_THREADS
			/* FIXME: Don't waste cycles */
			Py_END_ALLOW_THREADS
		}
	}
	first = iter->head;
	ret = iter->head->tuple;
	iter->head = first->next;
	if (first == iter->tail)
		iter->tail = NULL;
	free(first);
	iter->queue_size--;
	Py_DECREF(iter);
	return ret;
}

static PyObject *py_iter_append(LogIteratorObject *iter, PyObject *tuple)
{
	struct log_entry *entry;

	entry = calloc(sizeof(struct log_entry), 1);
	if (entry == NULL) {
		PyErr_NoMemory();
		return NULL;
	}

	entry->tuple = tuple;
	if (iter->tail == NULL) {
		iter->tail = entry;
	} else {
		iter->tail->next = entry;
		iter->tail = entry;
	}
	if (iter->head == NULL)
		iter->head = entry;

	iter->queue_size++;

	Py_RETURN_NONE;
}

PyTypeObject LogIterator_Type = {
	PyVarObject_HEAD_INIT(NULL, 0)
	"_ra.LogIterator", /*	const char *tp_name;  For printing, in format "<module>.<name>" */
	sizeof(LogIteratorObject), 
	0,/*	Py_ssize_t tp_basicsize, tp_itemsize;  For allocation */
	
	/* Methods to implement standard operations */

	.tp_dealloc = (destructor)log_iter_dealloc, /*	destructor tp_dealloc;	*/

#if PY_MAJOR_VERSION < 3
	/* Flags to define presence of optional/expanded features */
	.tp_flags = Py_TPFLAGS_HAVE_ITER, /*	long tp_flags;	*/
#endif

	/* Iterators */
	.tp_iter = PyObject_SelfIter,
	.tp_iternext = (iternextfunc)log_iter_next,
};

#if ONLY_SINCE_SVN(1, 5)
static svn_error_t *py_iter_log_entry_cb(void *baton, svn_log_entry_t *log_entry, apr_pool_t *pool)
{
	PyObject *revprops, *py_changed_paths, *ret, *tuple;
	LogIteratorObject *iter = (LogIteratorObject *)baton;

	PyGILState_STATE state;

	state = PyGILState_Ensure();

#if ONLY_SINCE_SVN(1, 6)
	py_changed_paths = pyify_changed_paths2(log_entry->changed_paths2, pool);
#else
	py_changed_paths = pyify_changed_paths(log_entry->changed_paths, true, pool);
#endif
	if (py_changed_paths == NULL) {
		PyGILState_Release(state);
		return py_svn_error();
	}

	revprops = prop_hash_to_dict(log_entry->revprops);
	if (revprops == NULL) {
		Py_DECREF(py_changed_paths);
		PyGILState_Release(state);
		return py_svn_error();
	}

	tuple = Py_BuildValue("NlNb", py_changed_paths,
						log_entry->revision, revprops, log_entry->has_children);
	if (tuple == NULL) {
		Py_DECREF(revprops);
		Py_DECREF(py_changed_paths);
		PyGILState_Release(state);
		return py_svn_error();
	}

	ret = py_iter_append(iter, tuple);
	if (ret == NULL) {
		Py_DECREF(tuple);
		PyGILState_Release(state);
		return py_svn_error();
	}

	Py_DECREF(ret);

	PyGILState_Release(state);

	return NULL;
}
#else
static svn_error_t *py_iter_log_cb(void *baton, apr_hash_t *changed_paths, svn_revnum_t revision, const char *author, const char *date, const char *message, apr_pool_t *pool)
{
	PyObject *revprops, *py_changed_paths, *ret, *tuple;
	LogIteratorObject *iter = (LogIteratorObject *)baton;

	PyGILState_STATE state;

	state = PyGILState_Ensure();

	if (!pyify_log_message(changed_paths, author, date, message, true,
	pool, &py_changed_paths, &revprops)) {
		goto fail;
	}
	tuple = Py_BuildValue("NlN", py_changed_paths, revision, revprops);
	if (tuple == NULL) {
		goto fail_tuple;
	}

	ret = py_iter_append(iter, tuple);

	if (ret == NULL) {
		Py_DECREF(tuple);
		goto fail;
	}

	Py_DECREF(ret);

	PyGILState_Release(state);

	return NULL;
	
fail_tuple:
	Py_DECREF(revprops);
	Py_DECREF(py_changed_paths);
fail:
	PyGILState_Release(state);
	return py_svn_error();
}
#endif


static void py_iter_log(void *baton)
{
	LogIteratorObject *iter = (LogIteratorObject *)baton;
	svn_error_t *error;
	PyGILState_STATE state;

#if ONLY_SINCE_SVN(1, 5)
	error = svn_ra_get_log2(iter->ra->ra, 
			iter->apr_paths, iter->start, iter->end, iter->limit,
			iter->discover_changed_paths, iter->strict_node_history, 
			iter->include_merged_revisions, iter->apr_revprops,
			py_iter_log_entry_cb, iter, iter->pool);
#else
	error = svn_ra_get_log(iter->ra->ra, 
			iter->apr_paths, iter->start, iter->end, iter->limit,
			iter->discover_changed_paths, iter->strict_node_history, py_iter_log_cb, 
			iter, iter->pool);
#endif
	state = PyGILState_Ensure();
	if (error != NULL) {
		iter->exc_type = (PyObject *)PyErr_GetSubversionExceptionTypeObject();
		iter->exc_val  = PyErr_NewSubversionException(error);
		svn_error_clear(error);
	} else {
		iter->exc_type = PyExc_StopIteration;
		Py_INCREF(iter->exc_type);
		iter->exc_val = Py_None;
		Py_INCREF(iter->exc_val);
	}
	iter->done = TRUE;
	iter->ra->busy = false;

	Py_DECREF(iter);
	PyGILState_Release(state);
}

PyObject *ra_iter_log(PyObject *self, PyObject *args, PyObject *kwargs)
{
	char *kwnames[] = { "paths", "start", "end", "limit",
		"discover_changed_paths", "strict_node_history", "include_merged_revisions", "revprops", NULL };
	PyObject *paths;
	svn_revnum_t start = 0, end = 0;
	int limit=0; 
	bool discover_changed_paths=false, strict_node_history=true, include_merged_revisions=false;
	RemoteAccessObject *ra = (RemoteAccessObject *)self;
	PyObject *revprops = Py_None;
	LogIteratorObject *ret;
	apr_pool_t *pool;
	apr_array_header_t *apr_paths;
	apr_array_header_t *apr_revprops;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oll|ibbbO:iter_log", kwnames, 
						 &paths, &start, &end, &limit,
						 &discover_changed_paths, &strict_node_history,
						 &include_merged_revisions, &revprops))
		return NULL;

	if (!ra_get_log_prepare(ra, paths, include_merged_revisions,
	revprops, &pool, &apr_paths, &apr_revprops)) {
		return NULL;
	}

	ret = PyObject_New(LogIteratorObject, &LogIterator_Type);
	ret->ra = ra;
	Py_INCREF(ret->ra);
	ret->start = start;
	ret->exc_type = NULL;
	ret->exc_val = NULL;
	ret->discover_changed_paths = discover_changed_paths;
	ret->end = end;
	ret->limit = limit;
	ret->apr_paths = apr_paths;
	ret->pool = pool;
	ret->include_merged_revisions = include_merged_revisions;
	ret->strict_node_history = strict_node_history;
	ret->apr_revprops = apr_revprops;
	ret->done = FALSE;
	ret->queue_size = 0;
	ret->head = NULL;
	ret->tail = NULL;

	Py_INCREF(ret);
	PyThread_start_new_thread(py_iter_log, ret);

	return (PyObject *)ret;
}