1
// -*- mode: cpp; mode: fold -*-
3
// $Id: apt_pkgmodule.cc,v 1.4 2002/01/08 06:53:04 jgg Exp $
4
/* ######################################################################
6
apt_pkgmodule - Top level for the python module. Create the internal
7
structures for the module in the interpriter.
9
##################################################################### */
11
// Include Files /*{{{*/
12
#include "apt_pkgmodule.h"
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>
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)
35
return CppPyObject_NEW<Configuration>(&ConfigurationType);
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)
49
if (PyArg_ParseTuple(Args,"s#s#",&A,&LenA,&B,&LenB) == 0)
54
PyErr_SetString(PyExc_ValueError,"_system not initialized");
58
return Py_BuildValue("i",_system->VS->DoCmpVersion(A,A+LenA,B,B+LenB));
61
static char *doc_CheckDep = "CheckDep(PkgVer,DepOp,DepVer) -> int";
62
static PyObject *CheckDep(PyObject *Self,PyObject *Args)
69
if (PyArg_ParseTuple(Args,"sss",&A,&OpStr,&B) == 0)
71
if (*debListParser::ConvertRelation(OpStr,Op) != 0)
73
PyErr_SetString(PyExc_ValueError,"Bad comparision operation");
79
PyErr_SetString(PyExc_ValueError,"_system not initialized");
83
return Py_BuildValue("i",_system->VS->CheckDep(A,Op,B));
84
// return Py_BuildValue("i",pkgCheckDep(B,A,Op));
87
static char *doc_UpstreamVersion = "UpstreamVersion(a) -> string";
88
static PyObject *UpstreamVersion(PyObject *Self,PyObject *Args)
91
if (PyArg_ParseTuple(Args,"s",&Ver) == 0)
93
return CppPyString(_system->VS->UpstreamVersion(Ver));
96
static char *doc_ParseDepends =
97
"ParseDepends(s) -> list of tuples\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,
114
if (PyArg_ParseTuple(Args,"s#",&Start,&Len) == 0)
118
PyObject *List = PyList_New(0);
119
PyObject *LastRow = 0;
125
Start = debListParser::ParseDepends(Start,Stop,Package,Version,Op,
129
PyErr_SetString(PyExc_ValueError,"Problem Parsing Dependency");
135
LastRow = PyList_New(0);
137
if (Package.empty() == false)
140
PyList_Append(LastRow,Obj = Py_BuildValue("sss",Package.c_str(),
142
pkgCache::CompTypeDeb(Op)));
146
// Group ORd deps into a single row..
147
if ((Op & pkgCache::Dep::Or) != pkgCache::Dep::Or)
149
if (PyList_Size(LastRow) != 0)
150
PyList_Append(List,LastRow);
157
static PyObject *ParseDepends(PyObject *Self,PyObject *Args)
159
return RealParseDepends(Self,Args,false);
161
static PyObject *ParseSrcDepends(PyObject *Self,PyObject *Args)
163
return RealParseDepends(Self,Args,true);
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)
172
if (PyArg_ParseTuple(Args,"O",&Obj) == 0)
175
// Digest of a string.
176
if (PyString_Check(Obj) != 0)
179
Sum.Add(PyString_AsString(Obj));
180
return CppPyString(Sum.Result().Value());
184
if (PyFile_Check(Obj) != 0)
187
int Fd = fileno(PyFile_AsFile(Obj));
189
if (fstat(Fd,&St) != 0 ||
190
Sum.AddFD(Fd,St.st_size) == false)
192
PyErr_SetFromErrno(PyExc_SystemError);
196
return CppPyString(Sum.Result().Value());
199
PyErr_SetString(PyExc_TypeError,"Only understand strings and files");
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)
209
if (PyArg_ParseTuple(Args,"O",&Obj) == 0)
212
// Digest of a string.
213
if (PyString_Check(Obj) != 0)
216
Sum.Add(PyString_AsString(Obj));
217
return CppPyString(Sum.Result().Value());
221
if (PyFile_Check(Obj) != 0)
224
int Fd = fileno(PyFile_AsFile(Obj));
226
if (fstat(Fd,&St) != 0 ||
227
Sum.AddFD(Fd,St.st_size) == false)
229
PyErr_SetFromErrno(PyExc_SystemError);
233
return CppPyString(Sum.Result().Value());
236
PyErr_SetString(PyExc_TypeError,"Only understand strings and files");
240
// init - 3 init functions /*{{{*/
241
// ---------------------------------------------------------------------
242
static char *doc_Init =
244
"Legacy. Do InitConfig then parse the command line then do InitSystem\n";
245
static PyObject *Init(PyObject *Self,PyObject *Args)
247
if (PyArg_ParseTuple(Args,"") == 0)
250
pkgInitConfig(*_config);
251
pkgInitSystem(*_config,_system);
254
return HandleErrors(Py_None);
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)
262
if (PyArg_ParseTuple(Args,"") == 0)
265
pkgInitConfig(*_config);
268
return HandleErrors(Py_None);
271
static char *doc_InitSystem =
272
"initsystem() -> None\n"
273
"Construct the underlying system\n";
274
static PyObject *InitSystem(PyObject *Self,PyObject *Args)
276
if (PyArg_ParseTuple(Args,"") == 0)
279
pkgInitSystem(*_config,_system);
282
return HandleErrors(Py_None);
286
// initapt_pkg - Core Module Initialization /*{{{*/
287
// ---------------------------------------------------------------------
289
static PyMethodDef methods[] =
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},
298
{"ParseSection",ParseSection,METH_VARARGS,doc_ParseSection},
299
{"ParseTagFile",ParseTagFile,METH_VARARGS,doc_ParseTagFile},
300
{"RewriteSection",RewriteSection,METH_VARARGS,doc_RewriteSection},
303
{"ReadConfigFile",LoadConfig,METH_VARARGS,doc_LoadConfig},
304
{"ReadConfigFileISC",LoadConfigISC,METH_VARARGS,doc_LoadConfig},
305
{"ParseCommandLine",ParseCommandLine,METH_VARARGS,doc_ParseCommandLine},
308
{"VersionCompare",VersionCompare,METH_VARARGS,doc_VersionCompare},
309
{"CheckDep",CheckDep,METH_VARARGS,doc_CheckDep},
310
{"UpstreamVersion",UpstreamVersion,METH_VARARGS,doc_UpstreamVersion},
313
{"ParseDepends",ParseDepends,METH_VARARGS,doc_ParseDepends},
314
{"ParseSrcDepends",ParseSrcDepends,METH_VARARGS,doc_ParseDepends},
317
{"md5sum",md5sum,METH_VARARGS,doc_md5sum},
318
{"sha1sum",sha1sum,METH_VARARGS,doc_sha1sum},
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"},
333
{"GetCache",TmpGetCache,METH_VARARGS,"GetCache() -> PkgCache"},
334
{"GetPkgRecords",GetPkgRecords,METH_VARARGS,"GetPkgRecords(Cache) -> PkgRecrods"},
339
static void AddStr(PyObject *Dict,const char *Itm,const char *Str)
341
PyObject *Obj = PyString_FromString(Str);
342
PyDict_SetItemString(Dict,(char *)Itm,Obj);
346
static void AddInt(PyObject *Dict,const char *Itm,unsigned long I)
348
PyObject *Obj = Py_BuildValue("i",I);
349
PyDict_SetItemString(Dict,(char *)Itm,Obj);
353
extern "C" void initapt_pkg()
355
PyObject *Module = Py_InitModule("apt_pkg",methods);
356
PyObject *Dict = PyModule_GetDict(Module);
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);
364
// Tag file constants
366
PyDict_SetItemString(Dict,"RewritePackageOrder",
367
Obj = CharCharToList(TFRewritePackageOrder));
369
PyDict_SetItemString(Dict,"RewriteSourceOrder",
370
Obj = CharCharToList(TFRewriteSourceOrder));
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__);
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);
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);