~ubuntu-branches/ubuntu/precise/python-apt/precise-proposed

« back to all changes in this revision

Viewing changes to python/apt_pkgmodule.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2002-03-09 23:34:13 UTC
  • Revision ID: james.westby@ubuntu.com-20020309233413-fb54falxz60g6jst
Tags: 0.5.4.3
#include <new> in python/generic.h so that we can build on ia64, which
uses g++-2.96 (Closes: #137467)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: cpp; mode: fold -*-
 
2
// Description                                                          /*{{{*/
 
3
// $Id: apt_pkgmodule.cc,v 1.4 2002/01/08 06:53:04 jgg Exp $
 
4
/* ######################################################################
 
5
 
 
6
   apt_pkgmodule - Top level for the python module. Create the internal
 
7
                   structures for the module in the interpriter.
 
8
      
 
9
   ##################################################################### */
 
10
                                                                        /*}}}*/
 
11
// Include Files                                                        /*{{{*/
 
12
#include "apt_pkgmodule.h"
 
13
#include "generic.h"
 
14
 
 
15
#include <apt-pkg/configuration.h>
 
16
#include <apt-pkg/version.h>
 
17
#include <apt-pkg/deblistparser.h>
 
18
#include <apt-pkg/pkgcache.h>
 
19
#include <apt-pkg/tagfile.h>
 
20
#include <apt-pkg/md5.h>
 
21
#include <apt-pkg/sha1.h>
 
22
#include <apt-pkg/init.h>
 
23
#include <apt-pkg/pkgsystem.h>
 
24
    
 
25
#include <sys/stat.h>
 
26
#include <unistd.h>
 
27
#include <Python.h>
 
28
                                                                        /*}}}*/
 
29
 
 
30
// newConfiguration - Build a new configuration class                   /*{{{*/
 
31
// ---------------------------------------------------------------------
 
32
static char *doc_newConfiguration = "Construct a configuration instance";
 
33
static PyObject *newConfiguration(PyObject *self,PyObject *args)
 
34
{
 
35
   return CppPyObject_NEW<Configuration>(&ConfigurationType);
 
36
}
 
37
                                                                        /*}}}*/
 
38
 
 
39
// Version Wrappers                                                     /*{{{*/
 
40
// These are kind of legacy..
 
41
static char *doc_VersionCompare = "VersionCompare(a,b) -> int";
 
42
static PyObject *VersionCompare(PyObject *Self,PyObject *Args)
 
43
{
 
44
   char *A;
 
45
   char *B;
 
46
   int LenA;
 
47
   int LenB;
 
48
   
 
49
   if (PyArg_ParseTuple(Args,"s#s#",&A,&LenA,&B,&LenB) == 0)
 
50
      return 0;
 
51
   
 
52
   if (_system == 0)
 
53
   {
 
54
      PyErr_SetString(PyExc_ValueError,"_system not initialized");
 
55
      return 0;
 
56
   }
 
57
   
 
58
   return Py_BuildValue("i",_system->VS->DoCmpVersion(A,A+LenA,B,B+LenB));
 
59
}
 
60
 
 
61
static char *doc_CheckDep = "CheckDep(PkgVer,DepOp,DepVer) -> int";
 
62
static PyObject *CheckDep(PyObject *Self,PyObject *Args)
 
63
{
 
64
   char *A;
 
65
   char *B;
 
66
   char *OpStr;
 
67
   unsigned int Op = 0;
 
68
   
 
69
   if (PyArg_ParseTuple(Args,"sss",&A,&OpStr,&B) == 0)
 
70
      return 0;
 
71
   if (*debListParser::ConvertRelation(OpStr,Op) != 0)
 
72
   {
 
73
      PyErr_SetString(PyExc_ValueError,"Bad comparision operation");
 
74
      return 0;
 
75
   }
 
76
 
 
77
   if (_system == 0)
 
78
   {
 
79
      PyErr_SetString(PyExc_ValueError,"_system not initialized");
 
80
      return 0;
 
81
   }
 
82
   
 
83
   return Py_BuildValue("i",_system->VS->CheckDep(A,Op,B));
 
84
//   return Py_BuildValue("i",pkgCheckDep(B,A,Op));
 
85
}
 
86
 
 
87
static char *doc_UpstreamVersion = "UpstreamVersion(a) -> string";
 
88
static PyObject *UpstreamVersion(PyObject *Self,PyObject *Args)
 
89
{
 
90
   char *Ver;
 
91
   if (PyArg_ParseTuple(Args,"s",&Ver) == 0)
 
92
      return 0;
 
93
   return CppPyString(_system->VS->UpstreamVersion(Ver));
 
94
}
 
95
 
 
96
static char *doc_ParseDepends = 
 
97
"ParseDepends(s) -> list of tuples\n"
 
98
"\n"
 
99
"The resulting tuples are (Pkg,Ver,Operation). Each anded dependency is a\n"
 
100
"list of or'd dependencies\n"
 
101
"Source depends are evaluated against the curernt arch and only those that\n"
 
102
"Match are returned.";
 
103
static PyObject *RealParseDepends(PyObject *Self,PyObject *Args,
 
104
                                  bool ParseArchFlags)
 
105
{
 
106
   string Package;
 
107
   string Version;
 
108
   unsigned int Op;
 
109
   
 
110
   const char *Start;
 
111
   const char *Stop;
 
112
   int Len;
 
113
   
 
114
   if (PyArg_ParseTuple(Args,"s#",&Start,&Len) == 0)
 
115
      return 0;
 
116
   Stop = Start + Len;
 
117
   
 
118
   PyObject *List = PyList_New(0);
 
119
   PyObject *LastRow = 0;
 
120
   while (1)
 
121
   {
 
122
      if (Start == Stop)
 
123
         break;
 
124
      
 
125
      Start = debListParser::ParseDepends(Start,Stop,Package,Version,Op,
 
126
                                          ParseArchFlags);
 
127
      if (Start == 0)
 
128
      {
 
129
         PyErr_SetString(PyExc_ValueError,"Problem Parsing Dependency");
 
130
         Py_DECREF(List);
 
131
         return 0;
 
132
      }
 
133
      
 
134
      if (LastRow == 0)
 
135
         LastRow = PyList_New(0);
 
136
      
 
137
      if (Package.empty() == false)
 
138
      {
 
139
         PyObject *Obj;
 
140
         PyList_Append(LastRow,Obj = Py_BuildValue("sss",Package.c_str(),
 
141
                                                   Version.c_str(),
 
142
                                                pkgCache::CompTypeDeb(Op)));
 
143
         Py_DECREF(Obj);
 
144
      }
 
145
      
 
146
      // Group ORd deps into a single row..
 
147
      if ((Op & pkgCache::Dep::Or) != pkgCache::Dep::Or)
 
148
      {
 
149
         if (PyList_Size(LastRow) != 0)
 
150
            PyList_Append(List,LastRow);
 
151
         Py_DECREF(LastRow);
 
152
         LastRow = 0;
 
153
      }      
 
154
   }
 
155
   return List;
 
156
}
 
157
static PyObject *ParseDepends(PyObject *Self,PyObject *Args)
 
158
{
 
159
   return RealParseDepends(Self,Args,false);
 
160
}
 
161
static PyObject *ParseSrcDepends(PyObject *Self,PyObject *Args)
 
162
{
 
163
   return RealParseDepends(Self,Args,true);
 
164
}
 
165
                                                                        /*}}}*/
 
166
// md5sum - Compute the md5sum of a file or string                      /*{{{*/
 
167
// ---------------------------------------------------------------------
 
168
static char *doc_md5sum = "md5sum(String) -> String or md5sum(File) -> String";
 
169
static PyObject *md5sum(PyObject *Self,PyObject *Args)
 
170
{
 
171
   PyObject *Obj;
 
172
   if (PyArg_ParseTuple(Args,"O",&Obj) == 0)
 
173
      return 0;
 
174
   
 
175
   // Digest of a string.
 
176
   if (PyString_Check(Obj) != 0)
 
177
   {
 
178
      MD5Summation Sum;
 
179
      Sum.Add(PyString_AsString(Obj));
 
180
      return CppPyString(Sum.Result().Value());
 
181
   }   
 
182
   
 
183
   // Digest of a file
 
184
   if (PyFile_Check(Obj) != 0)
 
185
   {
 
186
      MD5Summation Sum;
 
187
      int Fd = fileno(PyFile_AsFile(Obj));
 
188
      struct stat St;
 
189
      if (fstat(Fd,&St) != 0 ||
 
190
          Sum.AddFD(Fd,St.st_size) == false)
 
191
      {
 
192
         PyErr_SetFromErrno(PyExc_SystemError);
 
193
         return 0;
 
194
      }
 
195
      
 
196
      return CppPyString(Sum.Result().Value());
 
197
   }
 
198
   
 
199
   PyErr_SetString(PyExc_TypeError,"Only understand strings and files");
 
200
   return 0;
 
201
}
 
202
                                                                        /*}}}*/
 
203
// sha1sum - Compute the sha1sum of a file or string                    /*{{{*/
 
204
// ---------------------------------------------------------------------
 
205
static char *doc_sha1sum = "sha1sum(String) -> String or sha1sum(File) -> String";
 
206
static PyObject *sha1sum(PyObject *Self,PyObject *Args)
 
207
{
 
208
   PyObject *Obj;
 
209
   if (PyArg_ParseTuple(Args,"O",&Obj) == 0)
 
210
      return 0;
 
211
   
 
212
   // Digest of a string.
 
213
   if (PyString_Check(Obj) != 0)
 
214
   {
 
215
      SHA1Summation Sum;
 
216
      Sum.Add(PyString_AsString(Obj));
 
217
      return CppPyString(Sum.Result().Value());
 
218
   }   
 
219
   
 
220
   // Digest of a file
 
221
   if (PyFile_Check(Obj) != 0)
 
222
   {
 
223
      SHA1Summation Sum;
 
224
      int Fd = fileno(PyFile_AsFile(Obj));
 
225
      struct stat St;
 
226
      if (fstat(Fd,&St) != 0 ||
 
227
          Sum.AddFD(Fd,St.st_size) == false)
 
228
      {
 
229
         PyErr_SetFromErrno(PyExc_SystemError);
 
230
         return 0;
 
231
      }
 
232
      
 
233
      return CppPyString(Sum.Result().Value());
 
234
   }
 
235
   
 
236
   PyErr_SetString(PyExc_TypeError,"Only understand strings and files");
 
237
   return 0;
 
238
}
 
239
                                                                        /*}}}*/
 
240
// init - 3 init functions                                              /*{{{*/
 
241
// ---------------------------------------------------------------------
 
242
static char *doc_Init = 
 
243
"init() -> None\n"
 
244
"Legacy. Do InitConfig then parse the command line then do InitSystem\n";
 
245
static PyObject *Init(PyObject *Self,PyObject *Args)
 
246
{
 
247
   if (PyArg_ParseTuple(Args,"") == 0)
 
248
      return 0;
 
249
   
 
250
   pkgInitConfig(*_config);   
 
251
   pkgInitSystem(*_config,_system);
 
252
   
 
253
   Py_INCREF(Py_None);
 
254
   return HandleErrors(Py_None);
 
255
}
 
256
 
 
257
static char *doc_InitConfig =
 
258
"initconfig() -> None\n"
 
259
"Load the default configuration and the config file\n";
 
260
static PyObject *InitConfig(PyObject *Self,PyObject *Args)
 
261
{
 
262
   if (PyArg_ParseTuple(Args,"") == 0)
 
263
      return 0;
 
264
   
 
265
   pkgInitConfig(*_config);   
 
266
   
 
267
   Py_INCREF(Py_None);
 
268
   return HandleErrors(Py_None);
 
269
}
 
270
 
 
271
static char *doc_InitSystem =
 
272
"initsystem() -> None\n"
 
273
"Construct the underlying system\n";
 
274
static PyObject *InitSystem(PyObject *Self,PyObject *Args)
 
275
{
 
276
   if (PyArg_ParseTuple(Args,"") == 0)
 
277
      return 0;
 
278
   
 
279
   pkgInitSystem(*_config,_system);
 
280
   
 
281
   Py_INCREF(Py_None);
 
282
   return HandleErrors(Py_None);
 
283
}
 
284
                                                                        /*}}}*/
 
285
 
 
286
// initapt_pkg - Core Module Initialization                             /*{{{*/
 
287
// ---------------------------------------------------------------------
 
288
/* */
 
289
static PyMethodDef methods[] = 
 
290
{
 
291
   // Constructors
 
292
   {"newConfiguration",newConfiguration,METH_VARARGS,doc_newConfiguration},
 
293
   {"init",Init,METH_VARARGS,doc_Init},
 
294
   {"InitConfig",InitConfig,METH_VARARGS,doc_InitConfig},
 
295
   {"InitSystem",InitSystem,METH_VARARGS,doc_InitSystem},
 
296
 
 
297
   // Tag File
 
298
   {"ParseSection",ParseSection,METH_VARARGS,doc_ParseSection},
 
299
   {"ParseTagFile",ParseTagFile,METH_VARARGS,doc_ParseTagFile},
 
300
   {"RewriteSection",RewriteSection,METH_VARARGS,doc_RewriteSection},
 
301
   
 
302
   // Command line
 
303
   {"ReadConfigFile",LoadConfig,METH_VARARGS,doc_LoadConfig},
 
304
   {"ReadConfigFileISC",LoadConfigISC,METH_VARARGS,doc_LoadConfig},
 
305
   {"ParseCommandLine",ParseCommandLine,METH_VARARGS,doc_ParseCommandLine},
 
306
   
 
307
   // Versioning
 
308
   {"VersionCompare",VersionCompare,METH_VARARGS,doc_VersionCompare},
 
309
   {"CheckDep",CheckDep,METH_VARARGS,doc_CheckDep},
 
310
   {"UpstreamVersion",UpstreamVersion,METH_VARARGS,doc_UpstreamVersion},
 
311
 
 
312
   // Depends
 
313
   {"ParseDepends",ParseDepends,METH_VARARGS,doc_ParseDepends},
 
314
   {"ParseSrcDepends",ParseSrcDepends,METH_VARARGS,doc_ParseDepends},
 
315
   
 
316
   // Stuff
 
317
   {"md5sum",md5sum,METH_VARARGS,doc_md5sum},
 
318
   {"sha1sum",sha1sum,METH_VARARGS,doc_sha1sum},
 
319
 
 
320
   // Strings
 
321
   {"CheckDomainList",StrCheckDomainList,METH_VARARGS,"CheckDomainList(String,String) -> Bool"},
 
322
   {"QuoteString",StrQuoteString,METH_VARARGS,"QuoteString(String,String) -> String"},
 
323
   {"DeQuoteString",StrDeQuote,METH_VARARGS,"DeQuoteString(String) -> String"},
 
324
   {"SizeToStr",StrSizeToStr,METH_VARARGS,"SizeToStr(int) -> String"},
 
325
   {"TimeToStr",StrTimeToStr,METH_VARARGS,"TimeToStr(int) -> String"},
 
326
   {"URItoFileName",StrURItoFileName,METH_VARARGS,"URItoFileName(String) -> String"},
 
327
   {"Base64Encode",StrBase64Encode,METH_VARARGS,"Base64Encode(String) -> String"},
 
328
   {"StringToBool",StrStringToBool,METH_VARARGS,"StringToBool(String) -> int"},
 
329
   {"TimeRFC1123",StrTimeRFC1123,METH_VARARGS,"TimeRFC1123(int) -> String"},
 
330
   {"StrToTime",StrStrToTime,METH_VARARGS,"StrToTime(String) -> Int"},
 
331
 
 
332
   // Cache
 
333
   {"GetCache",TmpGetCache,METH_VARARGS,"GetCache() -> PkgCache"},
 
334
   {"GetPkgRecords",GetPkgRecords,METH_VARARGS,"GetPkgRecords(Cache) -> PkgRecrods"},
 
335
 
 
336
   {}
 
337
};
 
338
 
 
339
static void AddStr(PyObject *Dict,const char *Itm,const char *Str)
 
340
{
 
341
   PyObject *Obj = PyString_FromString(Str);
 
342
   PyDict_SetItemString(Dict,(char *)Itm,Obj);
 
343
   Py_DECREF(Obj);
 
344
}
 
345
 
 
346
static void AddInt(PyObject *Dict,const char *Itm,unsigned long I)
 
347
{
 
348
   PyObject *Obj = Py_BuildValue("i",I);
 
349
   PyDict_SetItemString(Dict,(char *)Itm,Obj);
 
350
   Py_DECREF(Obj);
 
351
}
 
352
 
 
353
extern "C" void initapt_pkg()
 
354
{
 
355
   PyObject *Module = Py_InitModule("apt_pkg",methods);
 
356
   PyObject *Dict = PyModule_GetDict(Module);
 
357
   
 
358
   // Global variable linked to the global configuration class
 
359
   CppPyObject<Configuration *> *Config = CppPyObject_NEW<Configuration *>(&ConfigurationPtrType);
 
360
   Config->Object = _config;
 
361
   PyDict_SetItemString(Dict,"Config",Config);
 
362
   Py_DECREF(Config);
 
363
   
 
364
   // Tag file constants
 
365
   PyObject *Obj;
 
366
   PyDict_SetItemString(Dict,"RewritePackageOrder",
 
367
                        Obj = CharCharToList(TFRewritePackageOrder));
 
368
   Py_DECREF(Obj);
 
369
   PyDict_SetItemString(Dict,"RewriteSourceOrder",
 
370
                        Obj = CharCharToList(TFRewriteSourceOrder));
 
371
   Py_DECREF(Obj);
 
372
   
 
373
   // Version..
 
374
   AddStr(Dict,"Version",pkgVersion);
 
375
   AddStr(Dict,"LibVersion",pkgLibVersion);
 
376
   AddStr(Dict,"CPU",pkgCPU);
 
377
   AddStr(Dict,"OS",pkgOS);
 
378
   AddStr(Dict,"Date",__DATE__);
 
379
   AddStr(Dict,"Time",__TIME__);
 
380
 
 
381
   // My constants!!
 
382
   AddInt(Dict,"DepDepends",pkgCache::Dep::Depends);
 
383
   AddInt(Dict,"DepPreDepends",pkgCache::Dep::PreDepends);
 
384
   AddInt(Dict,"DepSuggests",pkgCache::Dep::Suggests);
 
385
   AddInt(Dict,"DepRecommends",pkgCache::Dep::Recommends);
 
386
   AddInt(Dict,"DepConflicts",pkgCache::Dep::Conflicts);
 
387
   AddInt(Dict,"DepReplaces",pkgCache::Dep::Replaces);
 
388
   AddInt(Dict,"DepObsoletes",pkgCache::Dep::Obsoletes);
 
389
   
 
390
   AddInt(Dict,"PriImportant",pkgCache::State::Important);
 
391
   AddInt(Dict,"PriRequired",pkgCache::State::Required);
 
392
   AddInt(Dict,"PriStandard",pkgCache::State::Standard);
 
393
   AddInt(Dict,"PriOptional",pkgCache::State::Optional);
 
394
   AddInt(Dict,"PriExtra",pkgCache::State::Extra);
 
395
}
 
396
                                                                        /*}}}*/
 
397