summaryrefslogtreecommitdiff
path: root/python/apt_instmodule.cc
blob: d672a40a06c8c5587e24e45cd54f87c59f6ebb48 (plain)
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
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: apt_instmodule.cc,v 1.3 2002/01/08 06:53:04 jgg Exp $
/* ######################################################################

   apt_intmodule - Top level for the python module. Create the internal
                   structures for the module in the interpriter.
      
   Note, this module shares state (particularly global config) with the
   apt_pkg module.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include "apt_instmodule.h"
#include "generic.h"

#include <apt-pkg/debfile.h>
#include <apt-pkg/error.h>
    
#include <sys/stat.h>
#include <unistd.h>
#include <Python.h>
									/*}}}*/

// debExtractControl - Exctract an arbitary control member		/*{{{*/
// ---------------------------------------------------------------------
/* This is a common operation so this function will stay, but others that
   expose the full range of the apt-inst .deb processing will join it some
   day. */
static char *doc_debExtractControl =
"debExtractControl(File[,Member]) -> String\n"
"Returns the indicated file from the control tar. The default is 'control'\n";
static PyObject *debExtractControl(PyObject *Self,PyObject *Args)
{
   char *Member = "control";
   PyObject *File;
   if (PyArg_ParseTuple(Args,"O!|s",&PyFile_Type,&File,&Member) == 0)
      return 0;
   
   // Subscope makes sure any clean up errors are properly handled.
   PyObject *Res = 0;
   {
      // Open the file and associate the .deb
      FileFd Fd(fileno(PyFile_AsFile(File)),false);
      debDebFile Deb(Fd);
      if (_error->PendingError() == true)
	 return HandleErrors();
      
      debDebFile::MemControlExtract Extract(Member);
      if (Extract.Read(Deb) == false)
	 return HandleErrors();
      
      // Build the return result
      
      if (Extract.Control == 0)
      {
	 Py_INCREF(Py_None);
	 Res = Py_None;
      }
      else
	 Res = PyString_FromStringAndSize(Extract.Control,Extract.Length+2);
   }
   
   return HandleErrors(Res);
}
									/*}}}*/

// debExtractArchive - Exctract the archive		/*{{{*/
// ---------------------------------------------------------------------
static char *doc_debExtractArchive =
"debExtractArchve(File,rootdir) -> Bool\n"
"Extracts the Archive into the given root dir";
static PyObject *debExtractArchive(PyObject *Self,PyObject *Args)
{
   char *Rootdir = NULL;
   PyObject *File;
   if (PyArg_ParseTuple(Args,"O!|s",&PyFile_Type,&File,&Rootdir) == 0)
      return 0;
   
   // Subscope makes sure any clean up errors are properly handled.
   bool res = false;
   {
      if(Rootdir != NULL) 
      {
	 chdir(Rootdir);
      }

      // Open the file and associate the .deb
      FileFd Fd(fileno(PyFile_AsFile(File)),false);
      debDebFile Deb(Fd);
      if (_error->PendingError() == true)
	 return HandleErrors();

      // extracts relative to the current dir
      pkgDirStream Extract;
      res = Deb.ExtractArchive(Extract);

      if (res == false)
	 return HandleErrors();
   }   
   return HandleErrors(Py_BuildValue("b",res));
}
									/*}}}*/

// initapt_inst - Core Module Initialization				/*{{{*/
// ---------------------------------------------------------------------
/* */
static PyMethodDef methods[] =
{
   // Stuff
   {"debExtractControl",debExtractControl,METH_VARARGS,doc_debExtractControl},
   {"debExtractArchive",debExtractArchive,METH_VARARGS,doc_debExtractArchive},
   {"tarExtract",tarExtract,METH_VARARGS,doc_tarExtract},
   {"debExtract",debExtract,METH_VARARGS,doc_debExtract},

   {}
};

extern "C" void initapt_inst()
{
   Py_InitModule("apt_inst",methods);
}
									/*}}}*/