};
const char* path = NULL;
const char* arch = NULL;
- const char* conf = NULL;
+ PyObject* conf = NULL;
int offline = 0;
- int r;
+ int r = 1;
+
+ FILE* fconf = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOpzO", kwlist,
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOpOO", kwlist,
&path, &arch, &self->callbacks.log, &offline, &conf, &self->callbacks.confirm))
- return -1;
+ goto ERROR;
// Check if log callback is callable
if (self->callbacks.log && !PyCallable_Check(self->callbacks.log)) {
PyErr_SetString(PyExc_TypeError, "logger must be callable\n");
- return -1;
+ goto ERROR;
}
// Check if confirm callback is callable
if (self->callbacks.confirm && !PyCallable_Check(self->callbacks.confirm)) {
PyErr_SetString(PyExc_TypeError, "Confirm callback is not callable");
- return -1;
+ goto ERROR;
+ }
+
+ // Map the configuration
+ if (conf) {
+ fconf = PyObject_AsFileHandle(conf, "r");
+ if (!fconf)
+ goto ERROR;
}
int flags = 0;
Py_BEGIN_ALLOW_THREADS
// Create a new Pakfire instance
- r = pakfire_create(&self->pakfire, path, arch, conf, flags,
+ r = pakfire_create(&self->pakfire, path, arch, fconf, flags,
LOG_DEBUG, Pakfire_log_callback, self->callbacks.log);
Py_END_ALLOW_THREADS
PyErr_SetFromErrno(PyExc_OSError);
}
- return -1;
+ r = -1;
+ goto ERROR;
}
// Configure confirm callback
Py_INCREF(self->callbacks.confirm);
}
- return 0;
+ERROR:
+ if (fconf)
+ fclose(fconf);
+
+ return -r;
}
static void Pakfire_dealloc(PakfireObject* self) {
int progress, const char* status);
int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch,
- const char* conf, int flags,
- int loglevel, pakfire_log_callback log_callback, void* log_data);
+ FILE* conf, int flags, int loglevel, pakfire_log_callback log_callback, void* log_data);
struct pakfire* pakfire_ref(struct pakfire* pakfire);
struct pakfire* pakfire_unref(struct pakfire* pakfire);
return 0;
}
-static int pakfire_read_config(struct pakfire* pakfire, const char* path) {
- char default_path[PATH_MAX];
+static int pakfire_read_config(struct pakfire* pakfire, FILE* f) {
int r;
- // Use default path if none set
- if (!path) {
- r = pakfire_path(pakfire, default_path, "%s", PAKFIRE_CONFIG_DIR "/general.conf");
- if (r)
- return r;
-
- path = default_path;
- }
-
- DEBUG(pakfire, "Reading configuration from %s\n", path);
-
- FILE* f = fopen(path, "r");
- if (!f) {
- // Silently ignore when there is no default configuration file
- if (*default_path && errno == ENOENT)
- return 0;
-
- ERROR(pakfire, "Could not open configuration file %s: %m\n", path);
- return 1;
- }
+ DEBUG(pakfire, "Reading configuration\n");
// Read configuration from file
- r = pakfire_config_read(pakfire->config, f);
- if (r) {
- ERROR(pakfire, "Could not parse configuration file %s: %m\n", path);
- goto ERROR;
+ if (f) {
+ r = pakfire_config_read(pakfire->config, f);
+ if (r) {
+ ERROR(pakfire, "Could not parse configuration: %m\n");
+ return r;
+ }
}
// Import distro configuration
r = pakfire_config_import_distro(pakfire);
if (r)
- goto ERROR;
+ return r;
// Read repository configuration
r = pakfire_read_repo_config(pakfire);
if (r)
- goto ERROR;
-
-ERROR:
- fclose(f);
+ return r;
- return r;
+ return 0;
}
static int pakfire_read_os_release(struct pakfire* pakfire) {
}
PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
- const char* arch, const char* conf, int flags,
+ const char* arch, FILE* conf, int flags,
int loglevel, pakfire_log_callback log_callback, void* log_data) {
char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
char private_dir[PATH_MAX];
import asyncio
import functools
import glob
+import io
import json
import logging
import logging.handlers
# Dump pakfire configuration
log.debug("Pakfire configuration:\n%s" % conf)
- # Write the configuration to file
- f = tempfile.NamedTemporaryFile(delete=False)
- if conf:
- f.write(conf.encode())
- f.close()
-
- # Return the path
- return f.name
+ return io.StringIO(conf)
class BuildLogger(object):
)
# Create a new Pakfire instance
- p = pakfire.Pakfire(arch=ns.arch, conf=conf, logger=logger.log)
+ with open(conf) as c:
+ p = pakfire.Pakfire(arch=ns.arch, conf=c, logger=logger.log)
# Enable/disable repositories
for repo in p.repos:
version="%%(prog)s %s" % pakfire.__version__)
# Which configuration file to load?
- parser.add_argument("--config", "-c", nargs="?",
+ parser.add_argument("--config", "-c", nargs="?", type=argparse.FileType("r"),
help=_("Configuration file"))
# Enable/disable repositories
# Create Pakfire instance
p = pakfire.Pakfire(
- conf=args.config,
+ conf=args.conf,
arch=args.arch,
path=args.root,
offline=args.offline,
static int test_run(int i, struct test* t) {
struct pakfire* p = NULL;
+ FILE* c = NULL;
char root[PATH_MAX] = TEST_ROOTFS "/pakfire-test-XXXXXX";
int r;
LOG("running %s (%s)\n", t->name, root);
+ // Open the configuration file
+ c = fopen(TEST_SRC_PATH "/pakfire.conf", "r");
+ if (!c) {
+ LOG("Could not open configuration file: %m\n");
+ r = 1;
+ goto ERROR;
+ }
+
// Create a pakfire instance
- r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf",
- 0, LOG_DEBUG, pakfire_log_stderr, NULL);
+ r = pakfire_create(&t->pakfire, root, NULL, c, 0, LOG_DEBUG, pakfire_log_stderr, NULL);
if (r) {
LOG("ERROR: Could not initialize pakfire: %m\n");
goto ERROR;
t->pakfire = NULL;
}
+ // Close the configuration file
+ if (c)
+ fclose(c);
+
// Cleanup root
pakfire_rmtree(root, 0);