Make the python object structures opaque
[platform/upstream/rpm.git] / python / spec-py.c
1 /** \ingroup py_c
2  * \file python/spec-py.c
3  */
4
5 #include "system.h"
6
7 #include "spec-py.h"
8
9 /** \ingroup python
10  * \name Class: Rpmspec
11  * \class Rpmspec
12  * \brief A python rpm.spec object represents an RPM spec file set.
13  * 
14  *  The spec file is at the heart of RPM's packaging building process. Similar
15  *  in concept to a makefile, it contains information required by RPM to build
16  *  the package, as well as instructions telling RPM how to build it. The spec
17  *  file also dictates exactly what files are a part of the package, and where
18  *  they should be installed.
19  *  
20  *  The rpm.spec object represents a parsed specfile to aid extraction of data.
21  *  
22  *  For example
23  * \code
24  *  import rpm
25  *  rpm.addMacro("_topdir","/path/to/topdir")
26  *  ts=rpm.ts()
27  *  s=ts.parseSpec("foo.spec")
28  *  print s.prep()
29  * \endcode
30  *
31  *  Macros set using add macro will be used allowing testing of conditional builds
32  *
33  */
34
35 struct specObject_s {
36     PyObject_HEAD
37     /*type specific fields */
38     rpmSpec spec;
39 };
40
41 static void 
42 spec_dealloc(specObject * s) 
43 {
44         if (s->spec) {
45             s->spec=freeSpec(s->spec);
46         }
47         PyObject_Del(s);
48 }
49
50 static int
51 spec_print(specObject * s)
52 {
53     return 0;
54 }
55
56 /* XXX TODO return something sensible if spec exists but component (eg %clean)
57  * does not. Possibly "" or None */
58
59 static PyObject * 
60 spec_get_buildroot(specObject * s) 
61 {
62     rpmSpec spec = specFromSpec(s);
63     if (spec != NULL && spec->buildRoot) {
64         return Py_BuildValue("s", spec->buildRoot);
65     }
66     else {
67         return NULL;
68     }
69 }
70
71 static PyObject * 
72 spec_get_prep(specObject * s) 
73 {
74     rpmSpec spec = specFromSpec(s);
75     PyObject *res = NULL;
76     if (spec != NULL && spec->prep) {
77         res = Py_BuildValue("s",getStringBuf(spec->prep));
78     }
79     return res;
80 }
81
82 static PyObject * 
83 spec_get_build(specObject * s) 
84 {
85     rpmSpec spec = specFromSpec(s);
86     PyObject *res = NULL;
87     if (spec != NULL && spec->build) {
88         res = Py_BuildValue("s",getStringBuf(spec->build));
89     }
90     return res;
91 }
92
93 static PyObject * 
94 spec_get_install(specObject * s) 
95 {
96     rpmSpec spec = specFromSpec(s);
97     PyObject *res = NULL;
98     if (spec != NULL && spec->install) {
99         res = Py_BuildValue("s",getStringBuf(spec->install));
100     }
101     return res;
102 }
103
104 static PyObject * 
105 spec_get_clean(specObject * s) 
106 {
107     rpmSpec spec = specFromSpec(s);
108     PyObject *res = NULL;
109     if (spec != NULL && spec->clean) {
110         res = Py_BuildValue("s",getStringBuf(spec->clean));
111     }
112     return res;
113 }
114
115 static PyObject *
116 spec_get_sources(specObject *s)
117 {
118     struct Source * source;
119     PyObject *sourceList, *srcUrl;
120     rpmSpec spec;
121     char * fullSource;
122
123     sourceList = PyList_New(0);
124     spec = specFromSpec(s);
125     if ( spec != NULL) {
126         source = spec->sources;
127
128          while (source != NULL) {
129             fullSource = source->fullSource;
130             srcUrl = Py_BuildValue("(sii)", fullSource, source->num, source->flags);
131             PyList_Append(sourceList, srcUrl);
132             source=source->next;
133         } 
134
135         return PyList_AsTuple(sourceList);
136     }
137     else {
138         return NULL;
139     }
140
141 }
142
143 /**
144  */
145
146 static char spec_doc[] = "RPM Spec file object";
147
148 static PyMethodDef spec_Spec_methods[] = {
149     {"sources",   (PyCFunction) spec_get_sources, METH_VARARGS,  NULL },
150     {"prep",   (PyCFunction) spec_get_prep, METH_VARARGS,  NULL },
151     {"build",   (PyCFunction) spec_get_build, METH_VARARGS,  NULL },
152     {"install",   (PyCFunction) spec_get_install, METH_VARARGS,  NULL },
153     {"clean",   (PyCFunction) spec_get_clean, METH_VARARGS,  NULL },
154     {"buildRoot",   (PyCFunction) spec_get_buildroot, METH_VARARGS,  NULL },
155     {NULL}  /* Sentinel */
156 };
157
158 PyTypeObject spec_Type = {
159     PyObject_HEAD_INIT(&PyType_Type)
160     0,                         /*ob_size*/
161     "rpm.spec",               /*tp_name*/
162     sizeof(specObject),        /*tp_basicsize*/
163     0,                         /*tp_itemsize*/
164     (destructor) spec_dealloc, /*tp_dealloc*/
165     (printfunc) spec_print,    /*tp_print*/
166     0,                         /*tp_getattr*/
167     0,                         /*tp_setattr*/
168     0,                         /*tp_compare*/
169     0,                         /*tp_repr*/
170     0,                         /*tp_as_number*/
171     0,                         /*tp_as_sequence*/
172     0,                         /*tp_as_mapping*/
173     0,                         /*tp_hash */
174     0,                         /*tp_call*/
175     0,                         /*tp_str*/
176     0,                         /*tp_getattro*/
177     0,                         /*tp_setattro*/
178     0,                         /*tp_as_buffer*/
179     Py_TPFLAGS_DEFAULT,        /*tp_flags*/
180     spec_doc,                  /* tp_doc */
181     0,                         /* tp_traverse */
182     0,                         /* tp_clear */
183     0,                         /* tp_richcompare */
184     0,                         /* tp_weaklistoffset */
185     0,                         /* tp_iter */
186     0,                         /* tp_iternext */
187     spec_Spec_methods,         /* tp_methods */
188     0,                         /* tp_members */
189     0,                         /* tp_getset */
190     0,                         /* tp_base */
191     0,                         /* tp_dict */
192     0,                         /* tp_descr_get */
193     0,                         /* tp_descr_set */
194     0,                         /* tp_dictoffset */
195     0,                         /* tp_init */
196     0,                         /* tp_alloc */
197     0,                         /* tp_new */
198     0,                         /* tp_free */
199     0,                         /* tp_is_gc */
200 };
201
202 rpmSpec specFromSpec(specObject *s) 
203 {
204     return s->spec;
205 }
206
207 specObject *
208 spec_Wrap(rpmSpec spec) 
209 {
210     specObject * s = PyObject_New(specObject, &spec_Type);
211     if (s == NULL)
212         return NULL;
213     s->spec = spec; 
214     return s;
215 }