-
Notifications
You must be signed in to change notification settings - Fork 139
/
iutil-py.cpp
378 lines (331 loc) · 10.8 KB
/
iutil-py.cpp
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (C) 2012-2014 Red Hat, Inc.
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library 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 library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Python.h"
#include <vector>
#include <string>
#include <ctime>
#include <datetime.h>
#include "dnf-advisory.h"
#include "dnf-advisorypkg.h"
#include "dnf-advisoryref.h"
#include "hy-packageset.h"
#include "dnf-reldep.h"
#include "dnf-reldep-list.h"
#include "hy-iutil.h"
#include "hy-util-private.hpp"
#include "advisory-py.hpp"
#include "advisorypkg-py.hpp"
#include "advisoryref-py.hpp"
#include "iutil-py.hpp"
#include "package-py.hpp"
#include "query-py.hpp"
#include "reldep-py.hpp"
#include "sack-py.hpp"
#include "pycomp.hpp"
#include "sack/advisorypkg.hpp"
#include "sack/changelog.hpp"
#include "sack/packageset.hpp"
#include "sack/query.hpp"
#include "../../libdnf/repo/solvable/Dependency.hpp"
#include "libdnf/repo/solvable/DependencyContainer.hpp"
UniquePtrPyObject & UniquePtrPyObject::operator =(UniquePtrPyObject && src) noexcept
{
if (this == &src)
return *this;
Py_XDECREF(pyObj);
pyObj = src.pyObj;
src.pyObj = NULL;
return *this;
}
void UniquePtrPyObject::reset(PyObject * pyObj) noexcept
{
Py_XDECREF(this->pyObj);
this->pyObj = pyObj;
}
UniquePtrPyObject::~UniquePtrPyObject()
{
Py_XDECREF(pyObj);
}
PyObject *
advisorylist_to_pylist(const GPtrArray *advisorylist, PyObject *sack)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (unsigned int i = 0; i < advisorylist->len; ++i) {
auto cadvisory =
static_cast<DnfAdvisory *>(g_steal_pointer(&g_ptr_array_index(advisorylist, i)));
UniquePtrPyObject advisory(advisoryToPyObject(cadvisory, sack));
if (!advisory)
return NULL;
int rc = PyList_Append(list.get(), advisory.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
advisoryPkgVectorToPylist(const std::vector<libdnf::AdvisoryPkg> & advisorypkgs)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (auto& advisorypkg : advisorypkgs) {
UniquePtrPyObject pyAdvisoryPkg(advisorypkgToPyObject(new libdnf::AdvisoryPkg(advisorypkg)));
if (!pyAdvisoryPkg)
return NULL;
int rc = PyList_Append(list.get(), pyAdvisoryPkg.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
advisoryRefVectorToPylist(const std::vector<libdnf::AdvisoryRef> & advisoryRefs, PyObject *sack)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (auto& advisoryRef : advisoryRefs) {
UniquePtrPyObject pyAdvisoryRef(advisoryrefToPyObject(new libdnf::AdvisoryRef(advisoryRef), sack));
if (!pyAdvisoryRef)
return NULL;
int rc = PyList_Append(list.get(), pyAdvisoryRef.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
changelogslist_to_pylist(const std::vector<libdnf::Changelog> & changelogslist)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
PyDateTime_IMPORT;
for (auto & citem: changelogslist) {
UniquePtrPyObject d(PyDict_New());
if (!d)
return NULL;
UniquePtrPyObject author(PyUnicode_FromString(citem.getAuthor().c_str()));
if (PyDict_SetItemString(d.get(), "author", author.get()) == -1)
return NULL;
UniquePtrPyObject description(PyUnicode_FromString(citem.getText().c_str()));
if (PyDict_SetItemString(d.get(), "text", description.get()) == -1)
return NULL;
time_t itemts=citem.getTimestamp();
struct tm ts;
ts = *localtime(&itemts);
UniquePtrPyObject timestamp(PyDate_FromDate(ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday));
if (PyDict_SetItemString(d.get(), "timestamp", timestamp.get()) == -1)
return NULL;
if (PyList_Append(list.get(), d.get()) == -1)
return NULL;
}
return list.release();
}
PyObject *
packagelist_to_pylist(GPtrArray *plist, PyObject *sack)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (unsigned int i = 0; i < plist->len; i++) {
auto cpkg = static_cast<DnfPackage *>(g_ptr_array_index(plist, i));
UniquePtrPyObject package(new_package(sack, dnf_package_get_id(cpkg)));
if (!package)
return NULL;
int rc = PyList_Append(list.get(), package.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
packageset_to_pylist(const DnfPackageSet *pset, PyObject *sack)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
Id id = -1;
while(true) {
id = pset->next(id);
if (id == -1)
break;
UniquePtrPyObject package(new_package(sack, id));
if (!package)
return NULL;
int rc = PyList_Append(list.get(), package.get());
if (rc == -1)
return NULL;
}
return list.release();
}
std::unique_ptr<DnfPackageSet>
pyseq_to_packageset(PyObject *obj, DnfSack *sack)
{
if (queryObject_Check(obj)) {
HyQuery target = queryFromPyObject(obj);
return std::unique_ptr<DnfPackageSet>(new libdnf::PackageSet(*target->runSet()));
}
UniquePtrPyObject sequence(PySequence_Fast(obj, "Expected a sequence."));
if (!sequence)
return NULL;
std::unique_ptr<DnfPackageSet> pset(new libdnf::PackageSet(sack));
const unsigned count = PySequence_Size(sequence.get());
for (unsigned int i = 0; i < count; ++i) {
PyObject *item = PySequence_Fast_GET_ITEM(sequence.get(), i);
if (item == NULL)
return NULL;
DnfPackage *pkg = packageFromPyObject(item);
if (pkg == NULL)
return NULL;
pset->set(pkg);
}
return pset;
}
std::unique_ptr<libdnf::DependencyContainer>
pyseq_to_reldeplist(PyObject *obj, DnfSack *sack, int cmp_type)
{
UniquePtrPyObject sequence(PySequence_Fast(obj, "Expected a sequence."));
if (!sequence)
return NULL;
std::unique_ptr<libdnf::DependencyContainer> reldeplist(new libdnf::DependencyContainer(sack));
const unsigned count = PySequence_Size(sequence.get());
for (unsigned int i = 0; i < count; ++i) {
PyObject *item = PySequence_Fast_GET_ITEM(sequence.get(), i);
if (item == NULL)
return NULL;
if (reldepObject_Check(item)) {
DnfReldep * reldep = reldepFromPyObject(item);
if (reldep == NULL)
return NULL;
reldeplist->add(reldep);
} else if (cmp_type == HY_GLOB) {
PycompString reldep_str(item);
if (!reldep_str.getCString())
return NULL;
if (!hy_is_glob_pattern(reldep_str.getCString())) {
if (!reldeplist->addReldep(reldep_str.getCString()))
continue;
} else {
if (!reldeplist->addReldepWithGlob(reldep_str.getCString()))
continue;
}
} else {
PycompString reldepStr(item);
if (!reldepStr.getCString())
return NULL;
if (!reldeplist->addReldep(reldepStr.getCString()))
continue;
}
}
return reldeplist;
}
PyObject *
strlist_to_pylist(const char **slist)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (const char **iter = slist; *iter; ++iter) {
UniquePtrPyObject str(PyUnicode_FromString(*iter));
if (!str)
return NULL;
int rc = PyList_Append(list.get(), str.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
strCpplist_to_pylist(const std::vector<std::string> & cppList)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
for (auto & cStr:cppList) {
UniquePtrPyObject str(PyUnicode_FromString(cStr.c_str()));
if (!str)
return NULL;
int rc = PyList_Append(list.get(), str.get());
if (rc == -1)
return NULL;
}
return list.release();
}
PyObject *
reldeplist_to_pylist(DnfReldepList *reldeplist, PyObject *sack)
{
UniquePtrPyObject list(PyList_New(0));
if (!list)
return NULL;
const int count = reldeplist->count();
for (int i = 0; i < count; ++i) {
UniquePtrPyObject reldep(new_reldep(sack, reldeplist->getId(i)));
if (!reldep)
return NULL;
int rc = PyList_Append(list.get(), reldep.get());
if (rc == -1)
return NULL;
}
return list.release();
}
std::vector<std::string>
pySequenceConverter(PyObject * pySequence)
{
UniquePtrPyObject seq(PySequence_Fast(pySequence, "Expected a sequence."));
if (!seq)
throw std::runtime_error("Expected a sequence.");
const unsigned count = PySequence_Size(seq.get());
std::vector<std::string> output;
output.reserve(count);
for (unsigned int i = 0; i < count; ++i) {
PyObject *item = PySequence_Fast_GET_ITEM(seq.get(), i);
if (PyUnicode_Check(item) || PyString_Check(item)) {
PycompString pycomStr(item);
if (!pycomStr.getCString())
throw std::runtime_error("Invalid value.");
output.push_back(pycomStr.getCString());
} else {
PyErr_SetString(PyExc_TypeError, "Invalid value.");
throw std::runtime_error("Invalid value.");
}
}
return output;
}
PyObject *
problemRulesPyConverter(std::vector<std::vector<std::string>> & allProblems)
{
UniquePtrPyObject list_output(PyList_New(0));
if (!list_output)
return NULL;
for (auto & problemList: allProblems) {
if (problemList.empty()) {
PyErr_SetString(PyExc_ValueError, "Index out of range.");
continue;
}
UniquePtrPyObject list(strCpplist_to_pylist(problemList));
int rc = PyList_Append(list_output.get(), list.get());
if (rc == -1)
return NULL;
}
return list_output.release();
}