GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/pyalpm.c Lines: 43 43 100.0 %
Date: 2019-02-09 19:11:25 Branches: 10 16 62.5 %

Line Branch Exec Source
1
/**
2
 * pyalpm - a Python C module wrapping libalpm
3
 *
4
 *  Copyright 2008 Imanol Celaya <ilcra1989@gmail.com>
5
 *  Copyright (c) 2011 Rémy Oudompheng <remy@archlinux.org>
6
 *
7
 *  pyalpm is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation, either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  pyalpm is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with pyalpm.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "pyalpm.h"
22
#include "util.h"
23
#include "package.h"
24
#include "db.h"
25
26
1
static PyObject * alpmversion_alpm(PyObject *self, PyObject *dummy)
27
{
28
1
  const char *str;
29
1
  str = alpm_version();
30
31
1
  return Py_BuildValue("s", str);
32
}
33
34
1
static PyObject * version_alpm(PyObject *self, PyObject *dummy)
35
{
36
1
  return Py_BuildValue("s", VERSION);
37
}
38
39
/** Finds a package satisfying a dependency constraint in a package list */
40
3
static PyObject* pyalpm_find_satisfier(PyObject *self, PyObject* args) {
41
3
  PyObject *pkglist;
42
3
  char *depspec;
43
3
  alpm_list_t *alpm_pkglist;
44
3
  alpm_pkg_t *p;
45
46
3
  if(!PyArg_ParseTuple(args, "Os", &pkglist, &depspec))
47
  {
48
1
    PyErr_SetString(PyExc_TypeError, "find_satisfier() takes a Package list and a string");
49
1
    return NULL;
50
  }
51
52
2
  if(pylist_pkg_to_alpmlist(pkglist, &alpm_pkglist) == -1)
53
    return NULL;
54
55
2
  p = alpm_find_satisfier(alpm_pkglist, depspec);
56
2
  alpm_list_free(alpm_pkglist);
57
58
2
  if (p == NULL) {
59
1
    Py_RETURN_NONE;
60
  } else {
61
1
    PyObject *result;
62
1
    result = pyalpm_package_from_pmpkg(p);
63
1
    if (result == NULL) {
64
      return NULL;
65
    } else {
66
1
      return result;
67
    }
68
  }
69
}
70
71
6
static PyObject *pyalpm_vercmp(PyObject *self, PyObject *args) {
72
6
  const char *x, *y;
73
6
  int result;
74
6
  if (!PyArg_ParseTuple(args, "ss", &x, &y))
75
    return NULL;
76
6
  result = alpm_pkg_vercmp(x, y);
77
6
  return PyLong_FromLong(result);
78
}
79
80
static PyMethodDef methods[] = {
81
  {"version", version_alpm, METH_NOARGS, "returns pyalpm version."},
82
  {"alpmversion", alpmversion_alpm, METH_NOARGS, "returns alpm version."},
83
  {"vercmp", pyalpm_vercmp, METH_VARARGS, "compares version strings"},
84
85
  { "find_satisfier", pyalpm_find_satisfier, METH_VARARGS,
86
    "finds a package satisfying the given dependency among a list\n"
87
    "args: a list of packages, a dependency string\n"
88
    "returns: a Package object or None" },
89
90
  {"sync_newversion", pyalpm_sync_newversion, METH_VARARGS,
91
    "finds an available upgrade for a package in a list of databases\n"
92
    "args: a package, a list of databases\n"
93
    "returns: an upgrade candidate or None" },
94
95
  /* from db.c */
96
  {"find_grp_pkgs", pyalpm_find_grp_pkgs, METH_VARARGS,
97
   "find packages from a given group across databases\n"
98
   "args: a list of databases, a group name"},
99
100
  {NULL, NULL, 0, NULL}
101
};
102
103
1
static int pyalpm_clear(PyObject *m)
104
{
105
1
  PyObject *dict = PyModule_GetDict(m);
106
1
  if (dict == NULL) {
107
    return 0;
108
  }
109
110
1
  PyObject *error = PyDict_GetItemString(dict, "error");
111
1
  if (error == NULL) {
112
    return 0;
113
  }
114
115
1
  Py_DECREF(error);
116
117
  return 0;
118
}
119
120
static struct PyModuleDef pyalpm_def = {
121
  PyModuleDef_HEAD_INIT,
122
  "alpm",
123
  "This module wraps the libalpm library",
124
  -1,
125
  methods,
126
  NULL, NULL,
127
  pyalpm_clear,
128
  NULL,
129
};
130
131
1
PyMODINIT_FUNC PyInit_pyalpm(void)
132
{
133
1
  PyObject* m = PyModule_Create(&pyalpm_def);
134
135
1
  init_pyalpm_error(m);
136
1
  init_pyalpm_handle(m);
137
1
  init_pyalpm_package(m);
138
1
  init_pyalpm_db(m);
139
1
  init_pyalpm_transaction(m);
140
141
1
  return m;
142
}
143
144
/* vim: set ts=2 sw=2 et: */
145