-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpmmacro-py.c
174 lines (140 loc) · 3.18 KB
/
rpmmacro-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
/** \ingroup py_c
* \file python/rpmrc-py.c
*/
#include "system-py.h"
#include "structmember.h"
#include <rpmio.h>
#include <rpmiotypes.h>
#include <argv.h>
#include <rpmmacro.h>
#include <rpmtypes.h>
#include <rpmtag.h>
#include "rpmdebug-py.c"
#include "rpmmacro-py.h"
#include "debug.h"
/** \ingroup python
* \class Rpmmacro
* \brief A python rpm.macro object encapsulates macro configuration.
*/
/** \ingroup python
* \name Class: Rpmmacro
*/
/*@{*/
PyObject *
rpmmacro_AddMacro(PyObject * s, PyObject * args,
PyObject * kwds)
{
char * name, * val;
char * kwlist[] = {"name", "value", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss:AddMacro", kwlist,
&name, &val))
return NULL;
addMacro(NULL, name, NULL, val, -1);
Py_RETURN_NONE;
}
PyObject *
rpmmacro_DelMacro(PyObject * s, PyObject * args,
PyObject * kwds)
{
char * name;
char * kwlist[] = {"name", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:DelMacro", kwlist, &name))
return NULL;
delMacro(NULL, name);
Py_RETURN_NONE;
}
PyObject *
rpmmacro_ExpandMacro(PyObject * s, PyObject * args,
PyObject * kwds)
{
char * macro, * str;
char * kwlist[] = {"macro", NULL};
PyObject *res;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:ExpandMacro", kwlist, ¯o))
return NULL;
str = rpmExpand(macro, NULL);
res = Py_BuildValue("s", str);
str = _free(str);
return res;
}
PyObject *
rpmmacro_GetMacros(PyObject * s, PyObject * args,
PyObject * kwds)
{
char * kwlist[] = { NULL };
PyObject * mdict;
PyObject *oo, *bo;
const char ** av = NULL;
int ac = 0;
int i;
if (!PyArg_ParseTupleAndKeywords(args, kwds, ":GetMacros", kwlist))
return NULL;
mdict = PyDict_New();
ac = rpmGetMacroEntries(NULL, NULL, -1, &av);
if (mdict == NULL || ac < 0 || av == NULL) {
PyErr_SetString(pyrpmError, "out of memory");
if (av) {
int i;
for (i = 0; i < ac; i++)
av[i] = _free(av[i]);
av = _free(av);
}
return NULL;
}
if (ac == 0) {
av = argvFree(av);
return mdict;
}
oo = PyBytes_FromString("opts");
bo = PyBytes_FromString("body");
if (oo != NULL && bo != NULL)
for (i = 0; i < ac; i++) {
char *n, *o, *b;
PyObject *ndo, *no, *vo;
int failed = 0;
/* Parse out "%name(opts)\tbody" into n/o/b strings. */
n = (char *) av[i];
b = strchr(n, '\t');
assert(b != NULL);
o = ((b > n && b[-1] == ')') ? strchr(n, '(') : NULL);
if (*n == '%') n++;
if (o != NULL && *o == '(') {
b[-1] = '\0';
o++;
}
b++;
/* Create a "name" dictionary, add "opts" and "body" items. */
no = PyBytes_FromString(n);
if (no == NULL)
break;
ndo = PyDict_New();
if (ndo == NULL) {
Py_XDECREF(no);
break;
}
PyDict_SetItem(mdict, no, ndo);
Py_XDECREF(ndo);
if (o) {
if ((vo = PyBytes_FromString(o)) != NULL)
PyDict_SetItem(ndo, oo, vo);
else
failed = 1;
Py_XDECREF(vo);
}
if (b) {
if ((vo = PyBytes_FromString(b)) != NULL)
PyDict_SetItem(ndo, bo, vo);
else
failed = 1;
Py_XDECREF(vo);
}
if (failed)
PyDict_DelItem(mdict, no);
Py_XDECREF(no);
}
Py_XDECREF(oo);
Py_XDECREF(bo);
av = argvFree(av);
return mdict;
}
/*@}*/