-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec-py.c
368 lines (327 loc) · 9.64 KB
/
spec-py.c
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
/** \ingroup py_c
* \file python/spec-py.c
*/
#include "system-py.h"
#include <rpmiotypes.h>
#include <rpmio.h>
#include "spec-py.h"
#include "debug.h"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#endif
/** \ingroup python
* \name Class: Rpmspec
* \class Rpmspec
* \brief A python rpm.spec object represents an RPM spec file set.
*
* The spec file is at the heart of RPM's packaging building process. Similar
* in concept to a makefile, it contains information required by RPM to build
* the package, as well as instructions telling RPM how to build it. The spec
* file also dictates exactly what files are a part of the package, and where
* they should be installed.
*
* The rpm.spec object represents a parsed specfile to aid extraction of data.
*
* For example
* \code
* import rpm
* rpm.addMacro("_topdir","/path/to/topdir")
* s=rpm.spec("foo.spec")
* print s.prep()
* \endcode
*
* Macros set using add macro will be used allowing testing of conditional builds
*
*/
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
/* Header objects are in another module, some hoop jumping required... */
static PyObject *makeHeader(Header h)
{
PyObject *rpmmod = PyImport_ImportModuleNoBlock("rpm");
if (rpmmod == NULL) return NULL;
PyObject *ptr = CAPSULE_BUILD(h, NULL);
PyObject *hdr = PyObject_CallMethod(rpmmod, "hdr", "(O)", ptr);
Py_XDECREF(ptr);
Py_XDECREF(rpmmod);
return hdr;
}
struct specPkgObject_s {
PyObject_HEAD
/*type specific fields */
Package pkg;
};
static char specPkg_doc[] =
"";
static PyObject * specpkg_get_header(specPkgObject *s, void *closure)
{
return makeHeader( (s->pkg ? s->pkg->header : NULL) );
}
static PyGetSetDef specpkg_getseters[] = {
{ "header", (getter) specpkg_get_header, NULL, NULL },
{ NULL } /* sentinel */
};
PyTypeObject specPkg_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"rpm.specpkg", /* tp_name */
sizeof(specPkgObject), /* tp_size */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
specPkg_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
specpkg_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
};
struct specObject_s {
PyObject_HEAD
/*type specific fields */
Spec spec;
};
static void
spec_dealloc(specObject * s)
{
if (s->spec)
s->spec = freeSpec(s->spec);
Py_TYPE(s)->tp_free((PyObject *)s);
}
static PyObject *
spec_get_buildroot(specObject * s, void * closure)
{
#ifdef REFERENCE
Spec spec = specFromSpec(s);
if (spec->buildRoot)
return Py_BuildValue("s", spec->buildRoot);
Py_RETURN_NONE;
#else
Spec spec = specFromSpec(s);
PyObject * result = NULL;
const char * buildRootURL = rpmExpand("%{?buildroot}", NULL);
if (spec != NULL && *buildRootURL)
result = Py_BuildValue("s", buildRootURL);
buildRootURL = _free(buildRootURL);
return result;
#endif
}
static PyObject *
getSection(Spec spec, int section)
{
rpmiob iob = NULL;;
const char * str = NULL;
if (spec == NULL)
goto exit;
switch (section) {
#ifdef NOTYET
case RPMBUILD_NONE: iob = spec->parsed; break;
#endif
case RPMBUILD_PREP: iob = spec->prep; break;
case RPMBUILD_BUILD: iob = spec->build; break;
case RPMBUILD_INSTALL: iob = spec->install; break;
case RPMBUILD_CHECK: iob = spec->check; break;
case RPMBUILD_CLEAN: iob = spec->clean; break;
}
if (iob)
str = rpmiobStr(iob);
if (str)
return Py_BuildValue("s", str);
exit:
Py_RETURN_NONE;
}
static PyObject *
spec_get_prep(specObject * s)
{
return getSection(specFromSpec(s), RPMBUILD_PREP);
}
static PyObject *
spec_get_build(specObject * s)
{
return getSection(specFromSpec(s), RPMBUILD_BUILD);
}
static PyObject *
spec_get_install(specObject * s)
{
return getSection(specFromSpec(s), RPMBUILD_INSTALL);
}
static PyObject *
spec_get_check(specObject * s)
{
return getSection(specFromSpec(s), RPMBUILD_CHECK);
}
static PyObject *
spec_get_clean(specObject * s)
{
return getSection(specFromSpec(s), RPMBUILD_CLEAN);
}
static PyObject *
spec_get_sources(specObject *s)
{
PyObject *sourceList = PyList_New(0);
Spec spec = specFromSpec(s);
struct Source * source;
if (sourceList == NULL || spec == NULL)
return NULL;
for (source = spec->sources; source != NULL; source = source->next) {
PyObject *srcUrl = Py_BuildValue("(sii)",
source->fullSource, source->num, source->flags);
PyList_Append(sourceList, srcUrl);
Py_DECREF(srcUrl);
}
return sourceList;
}
static PyObject *
spec_get_packages(specObject *s, void *closure)
{
PyObject *pkgList = PyList_New(0);
Spec spec = specFromSpec(s);
Package pkg;
if (pkgList == NULL || spec == NULL)
return NULL;
for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
PyObject *po = specPkg_Wrap(&specPkg_Type, pkg);
PyList_Append(pkgList, po);
Py_DECREF(po);
}
return pkgList;
}
static PyObject *
spec_get_source_header(specObject *s, void *closure)
{
Spec spec = specFromSpec(s);
if (spec->sourceHeader == NULL)
initSourceHeader(spec, NULL);
return makeHeader(spec->sourceHeader);
}
static char spec_doc[] = "RPM Spec file object";
static PyGetSetDef spec_getseters[] = {
{"sources", (getter) spec_get_sources, NULL, NULL },
{"prep", (getter) spec_get_prep, NULL, NULL },
{"build", (getter) spec_get_build, NULL, NULL },
{"install", (getter) spec_get_install, NULL, NULL },
{"check", (getter) spec_get_check, NULL, NULL },
{"clean", (getter) spec_get_clean, NULL, NULL },
{"buildRoot", (getter) spec_get_buildroot, NULL, NULL },
{"packages", (getter) spec_get_packages, NULL, NULL },
{"sourceHeader", (getter) spec_get_source_header, NULL, NULL },
{NULL} /* Sentinel */
};
static PyObject *
spec_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{
rpmts ts = NULL;
const char * specfile;
Spec spec = NULL;
int recursing = 0;
char * passPhrase = "";
char *cookie = NULL;
int anyarch = 1;
int verify = 1;
int force = 1;
char * kwlist[] = {"specfile", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:spec_new", kwlist,
&specfile))
return NULL;
ts = rpmtsCreate();
if (parseSpec(ts, specfile, "/", recursing, passPhrase,
cookie, anyarch, force, verify) == 0)
spec = rpmtsSpec(ts);
else
PyErr_SetString(PyExc_ValueError, "can't parse specfile\n");
ts = rpmtsFree(ts);
return spec ? spec_Wrap(subtype, spec) : NULL;
}
PyTypeObject spec_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"rpm.spec", /*tp_name*/
sizeof(specObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor) spec_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
spec_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
spec_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
spec_new, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
};
Spec specFromSpec(specObject *s)
{
return s->spec;
}
PyObject *
spec_Wrap(PyTypeObject *subtype, Spec spec)
{
specObject * s = (specObject *)subtype->tp_alloc(subtype, 0);
if (s == NULL)
return NULL;
s->spec = spec;
return (PyObject *)s;
}
PyObject *
specPkg_Wrap(PyTypeObject *subtype, Package pkg)
{
specPkgObject * s = (specPkgObject *)subtype->tp_alloc(subtype, 0);
if (s == NULL)
return NULL;
s->pkg = pkg;
return (PyObject *)s;
}