Rename Spec -> rpmSpec for namespacing
[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
36 static void 
37 spec_dealloc(specObject * s) 
38 {
39         if (s->spec) {
40             s->spec=freeSpec(s->spec);
41         }
42         PyObject_Del(s);
43 }
44
45 static int
46 spec_print(specObject * s)
47 {
48     return 0;
49 }
50
51 /* XXX TODO return something sensible if spec exists but component (eg %clean)
52  * does not. Possibly "" or None */
53
54 static PyObject * 
55 spec_get_buildroot(specObject * s) 
56 {
57     rpmSpec spec = specFromSpec(s);
58     if (spec != NULL && spec->buildRootURL) {
59         return Py_BuildValue("s", spec->buildRootURL);
60     }
61     else {
62         return NULL;
63     }
64 }
65
66 static PyObject * 
67 spec_get_prep(specObject * s) 
68 {
69     rpmSpec spec = specFromSpec(s);
70     if (spec != NULL && spec->prep) {
71         StringBuf sb = newStringBuf();
72         sb=spec->prep;
73         return Py_BuildValue("s",getStringBuf(sb));
74     }
75      else {
76          return NULL;
77      }
78 }
79
80 static PyObject * 
81 spec_get_build(specObject * s) 
82 {
83     rpmSpec spec = specFromSpec(s);
84     if (spec != NULL && spec->build) {
85         StringBuf sb = newStringBuf();
86         sb=spec->build;
87         return Py_BuildValue("s",getStringBuf(sb));
88     }
89      else {
90          return NULL;
91      }
92 }
93
94 static PyObject * 
95 spec_get_install(specObject * s) 
96 {
97     rpmSpec spec = specFromSpec(s);
98     if (spec != NULL && spec->install) {
99         StringBuf sb = newStringBuf();
100         sb=spec->install;
101         return Py_BuildValue("s",getStringBuf(sb));
102     }
103      else {
104          return NULL;
105      }
106 }
107
108 static PyObject * 
109 spec_get_clean(specObject * s) 
110 {
111     rpmSpec spec = specFromSpec(s);
112     if (spec != NULL && spec->clean) {
113         StringBuf sb = newStringBuf();
114         sb=spec->clean;
115         return Py_BuildValue("s",getStringBuf(sb));
116     }
117      else {
118          return NULL;
119      }
120 }
121
122 static PyObject *
123 spec_get_sources(specObject *s)
124 {
125     struct Source * source;
126     PyObject *sourceList, *srcUrl;
127     rpmSpec spec;
128     char * fullSource;
129
130     sourceList = PyList_New(0);
131     spec = specFromSpec(s);
132     if ( spec != NULL) {
133         source = spec->sources;
134
135          while (source != NULL) {
136             fullSource = source->fullSource;
137             srcUrl = Py_BuildValue("(sii)", fullSource, source->num, source->flags);
138             PyList_Append(sourceList, srcUrl);
139             source=source->next;
140         } 
141
142         return PyList_AsTuple(sourceList);
143     }
144     else {
145         return NULL;
146     }
147
148 }
149
150 /**
151  */
152
153 static char spec_doc[] = "RPM Spec file object";
154
155 static PyMethodDef spec_Spec_methods[] = {
156     {"sources",   (PyCFunction) spec_get_sources, METH_VARARGS,  NULL },
157     {"prep",   (PyCFunction) spec_get_prep, METH_VARARGS,  NULL },
158     {"build",   (PyCFunction) spec_get_build, METH_VARARGS,  NULL },
159     {"install",   (PyCFunction) spec_get_install, METH_VARARGS,  NULL },
160     {"clean",   (PyCFunction) spec_get_clean, METH_VARARGS,  NULL },
161     {"buildRoot",   (PyCFunction) spec_get_buildroot, METH_VARARGS,  NULL },
162     {NULL}  /* Sentinel */
163 };
164
165 PyTypeObject spec_Type = {
166     PyObject_HEAD_INIT(&PyType_Type)
167     0,                         /*ob_size*/
168     "rpm.spec",               /*tp_name*/
169     sizeof(specObject),        /*tp_basicsize*/
170     0,                         /*tp_itemsize*/
171     (destructor) spec_dealloc, /*tp_dealloc*/
172     (printfunc) spec_print,    /*tp_print*/
173     0,                         /*tp_getattr*/
174     0,                         /*tp_setattr*/
175     0,                         /*tp_compare*/
176     0,                         /*tp_repr*/
177     0,                         /*tp_as_number*/
178     0,                         /*tp_as_sequence*/
179     0,                         /*tp_as_mapping*/
180     0,                         /*tp_hash */
181     0,                         /*tp_call*/
182     0,                         /*tp_str*/
183     0,                         /*tp_getattro*/
184     0,                         /*tp_setattro*/
185     0,                         /*tp_as_buffer*/
186     Py_TPFLAGS_DEFAULT,        /*tp_flags*/
187     spec_doc,                  /* tp_doc */
188     0,                         /* tp_traverse */
189     0,                         /* tp_clear */
190     0,                         /* tp_richcompare */
191     0,                         /* tp_weaklistoffset */
192     0,                         /* tp_iter */
193     0,                         /* tp_iternext */
194     spec_Spec_methods,         /* tp_methods */
195     0,                         /* tp_members */
196     0,                         /* tp_getset */
197     0,                         /* tp_base */
198     0,                         /* tp_dict */
199     0,                         /* tp_descr_get */
200     0,                         /* tp_descr_set */
201     0,                         /* tp_dictoffset */
202     0,                         /* tp_init */
203     0,                         /* tp_alloc */
204     0,                         /* tp_new */
205     0,                         /* tp_free */
206     0,                         /* tp_is_gc */
207 };
208
209 rpmSpec specFromSpec(specObject *s) 
210 {
211     return s->spec;
212 }
213
214 specObject *
215 spec_Wrap(rpmSpec spec) 
216 {
217     specObject * s = PyObject_New(specObject, &spec_Type);
218     if (s == NULL)
219         return NULL;
220     s->spec = spec; 
221     return s;
222 }