poldek: poldek/lib_init.c, poldek/poldek.h, poldek/python/Makefile...
mis
mis at pld-linux.org
Fri Jul 6 00:28:58 CEST 2007
Author: mis Date: Thu Jul 5 22:28:58 2007 GMT
Module: poldek Tag: HEAD
---- Log message:
- callbackable (from python too) downloading progress bar
---- Files affected:
poldek/poldek:
lib_init.c (1.83 -> 1.84) , poldek.h (1.36 -> 1.37)
poldek/poldek/python:
Makefile.am (1.13 -> 1.14) , poldek.py (1.12 -> 1.13) , poldekmod.i (1.14 -> 1.15) , pyldek.py (1.4 -> 1.5)
poldek/poldek/vfile:
vfetch.c (1.16 -> 1.17) , vfffmod.c (1.7 -> 1.8) , vfile.c (1.70 -> 1.71) , vfile.h (1.49 -> 1.50) , vfile_intern.h (1.7 -> 1.8) , vfprogress.c (1.16 -> 1.17) , vfreq.h (1.6 -> 1.7)
poldek/poldek/vfile/vfff:
vfff.c (1.6 -> 1.7) , vfff.h (1.4 -> 1.5)
---- Diffs:
================================================================
Index: poldek/poldek/lib_init.c
diff -u poldek/poldek/lib_init.c:1.83 poldek/poldek/lib_init.c:1.84
--- poldek/poldek/lib_init.c:1.83 Mon Jul 2 18:39:22 2007
+++ poldek/poldek/lib_init.c Fri Jul 6 00:28:53 2007
@@ -1193,6 +1193,12 @@
poldek_log_set_default_appender("_TTY", NULL, NULL);
break;
+ case POLDEK_CONF_VFILEPROGRESS:
+ if ((vv = va_arg(ap, void*)))
+ vfile_configure(VFILE_CONF_PROGRESS, vv);
+ break;
+
+
case POLDEK_CONF_CONFIRM_CB:
if ((vv = va_arg(ap, void*)))
ctx->confirm_fn = vv;
================================================================
Index: poldek/poldek/poldek.h
diff -u poldek/poldek/poldek.h:1.36 poldek/poldek/poldek.h:1.37
--- poldek/poldek/poldek.h:1.36 Sun Jul 1 23:04:26 2007
+++ poldek/poldek/poldek.h Fri Jul 6 00:28:53 2007
@@ -54,6 +54,7 @@
#define POLDEK_CONF_CONFIRM_CB 23
#define POLDEK_CONF_TSCONFIRM_CB 24
#define POLDEK_CONF_CHOOSEEQUIV_CB 25
+#define POLDEK_CONF_VFILEPROGRESS 26
int poldek_configure(struct poldek_ctx *ctx, int param, ...);
================================================================
Index: poldek/poldek/python/Makefile.am
diff -u poldek/poldek/python/Makefile.am:1.13 poldek/poldek/python/Makefile.am:1.14
--- poldek/poldek/python/Makefile.am:1.13 Mon Aug 21 17:38:22 2006
+++ poldek/poldek/python/Makefile.am Fri Jul 6 00:28:53 2007
@@ -5,7 +5,7 @@
py_incdir = $(py_prefix)/include/python$(py_ver)
endif
-EXTRA_DIST = poldekmod.i local_stdint.c test.py run.sh
+EXTRA_DIST = poldekmod.i local_stdint.c pyldek.py run.sh
CLEANFILES = wrap_* poldekmod.py local_stdint.h
INCLUDES = @TRURL_INCLUDE@ @TNDB_INCLUDE@ -I$(top_srcdir) -I$(py_incdir) -I.
================================================================
Index: poldek/poldek/python/poldek.py
diff -u poldek/poldek/python/poldek.py:1.12 poldek/poldek/python/poldek.py:1.13
--- poldek/poldek/python/poldek.py:1.12 Sun Jul 1 23:04:27 2007
+++ poldek/poldek/python/poldek.py Fri Jul 6 00:28:53 2007
@@ -1,6 +1,7 @@
#!/usr/bin/python
# $Id$
+import sys
import os
import re
import string
@@ -217,17 +218,21 @@
#_complete_class(pkguinf, 'pkguinf_', exclude = 'pkguinf_get', verbose = 1, with_methods = False)
#setattr(pkguinf, 'get', eval('lambda self, tag: poldekmod.pkguinf_get(self, ord(tag[0]))'))
+class py_poldek_util:
+ def _implement__me(self):
+ print "## %s.%s: Implement me!" % (self.__class__.__name__,
+ sys._getframe(1).f_code.co_name)
-class callbacks:
+class callbacks(py_poldek_util):
def log(self, pri, message):
- print "## log: Implement me"
+ self._implement__me()
def confirm(self, ts, hint, message):
- print "## confirm: Implement me"
+ self._implement__me()
return hint
def confirm_transaction(self, ts):
- print "## confirm_transaction: Implement me"
+ self._implement__me()
return False
# n_array_proxy() applied to tn_array*
@@ -236,6 +241,15 @@
return self.choose_equiv(ts, capability_name, packages, hint)
def choose_equiv(self, ts, capability_name, packages, hint):
- print "## choose_equiv: Implement me"
+ print "## confirm_transaction: Implement me"
return hint
+class vfile_progress(py_poldek_util):
+ def initialize(self, label):
+ self._implement__me()
+
+ def reset(self):
+ self._implement__me()
+
+ def progress(self, total, amount):
+ self._implement__me()
================================================================
Index: poldek/poldek/python/poldekmod.i
diff -u poldek/poldek/python/poldekmod.i:1.14 poldek/poldek/python/poldekmod.i:1.15
--- poldek/poldek/python/poldekmod.i:1.14 Sun Jul 1 23:04:27 2007
+++ poldek/poldek/python/poldekmod.i Fri Jul 6 00:28:53 2007
@@ -14,6 +14,7 @@
#include "pkgdir/pkgdir.h"
#include "cli/poclidek.h"
#include "log.h"
+#include "vfile/vfile.h" /* for vf_progress */
static void PythonDoLog(void *data, int pri, const char *fmt, va_list vargs);
static int PythonConfirm(void *data, const struct poldek_ts *ts,
@@ -22,6 +23,7 @@
static int PythonChooseEquiv(void *data, const struct poldek_ts *ts,
const char *cap, tn_array *pkgs, int hint);
+static struct vf_progress vfPyProgress;
%}
%include exception.i
@@ -35,7 +37,6 @@
%include "pkgdir/source.h"
%include "pkgdir/pkgdir.h"
%include "cli/poclidek.h"
-//%include "cli/dent.h"
struct poldek_ctx {};
struct poldek_ts {};
@@ -45,33 +46,59 @@
struct poclidek_ctx {};
struct poclidek_rcmd {};
+
%{
-static void PythonDoLogOLD(void *data, int pri, const char *fmt, va_list vargs)
+static void *py_progress_new(void *data, const char *label)
{
- PyObject *log, *args;
- const char *spri = "info";
- char message[2048];
+ PyObject *obj, *method, *pylabel, *r;
+
+ obj = (PyObject *) data;
+ method = Py_BuildValue("s", "initialize");
+ pylabel = Py_BuildValue("s", label);
+
+ r = PyObject_CallMethodObjArgs(obj, method, pylabel, NULL);
+ if (r)
+ Py_DECREF(r);
- if (pri & LOGERR)
- spri = "error";
+ Py_DECREF(method);
+ Py_DECREF(pylabel);
+ return obj;
+}
- else if (pri & LOGWARN)
- spri = "warning";
+static void py_progress_reset(void *bar) {
+ PyObject *obj, *method, *r;
+
+ obj = (PyObject *) bar;
+ method = Py_BuildValue("s", "reset");
- else if (pri & LOGNOTICE)
- spri = "notice";
+ r = PyObject_CallMethodObjArgs(obj, method, NULL);
+ if (r)
+ Py_DECREF(r);
- else if (pri & LOGOPT_CONT)
- spri = "cont";
-
- log = (PyObject *) data; // pyfunction
- n_vsnprintf(message, sizeof(message), fmt, vargs);
+ Py_DECREF(method);
+}
+
+static void py_progress(void *bar, long total, long amount) {
+ PyObject *obj, *method, *pytotal, *pyamount, *r;
- args = Py_BuildValue("(ss)", spri, message);
- PyEval_CallObject(log, args);
- Py_DECREF(args);
+ obj = (PyObject *) bar;
+ method = Py_BuildValue("s", "progress");
+ pytotal = Py_BuildValue("i", total);
+ pyamount = Py_BuildValue("i", amount);
+
+ r = PyObject_CallMethodObjArgs(obj, method, pytotal, pyamount, NULL);
+ if (r)
+ Py_DECREF(r);
+
+ Py_DECREF(method);
+ Py_DECREF(pytotal);
+ Py_DECREF(pyamount);
}
+static struct vf_progress vfPyProgress = {
+ NULL, py_progress_new, py_progress, py_progress_reset, NULL
+};
+
static void PythonDoLog(void *data, int pri, const char *fmt, va_list vargs)
{
PyObject *obj, *method, *pypri, *pymessage, *r;
@@ -453,6 +480,16 @@
poldek_configure(self, POLDEK_CONF_CONFIRM_CB, (void*)PythonConfirm, v);
poldek_configure(self, POLDEK_CONF_TSCONFIRM_CB, (void*)PythonTsConfirm, v);
poldek_configure(self, POLDEK_CONF_CHOOSEEQUIV_CB, (void*)PythonChooseEquiv, v);
+ Py_INCREF(obj);
+ }
+
+ void set_vfile_progress(PyObject *obj) {
+ struct vf_progress *progress;
+ progress = n_malloc(sizeof(*progress));
+ *progress = vfPyProgress;
+ progress->data = obj;
+ progress->free = free;
+ poldek_configure(self, POLDEK_CONF_VFILEPROGRESS, progress);
Py_INCREF(obj);
}
}
================================================================
Index: poldek/poldek/python/pyldek.py
diff -u poldek/poldek/python/pyldek.py:1.4 poldek/poldek/python/pyldek.py:1.5
--- poldek/poldek/python/pyldek.py:1.4 Mon Jul 2 10:11:09 2007
+++ poldek/poldek/python/pyldek.py Fri Jul 6 00:28:53 2007
@@ -14,6 +14,25 @@
raise ImportError, "run me via $ ./run.sh %s" % sys.argv[0]
+class vfileProgress(poldek.vfile_progress):
+ def __init__(self):
+ self.label = None
+
+ def initializex(self, label):
+ print "[py] downloading %s..." % label
+ self.label = label
+
+ def reset(self):
+ pass
+
+ def progress(self, total, amount):
+ if amount == 0:
+ print " start downloading"
+ elif amount == -1:
+ print "\n end downloading"
+ else:
+ print "\r - got %d of total %d" % (amount, total),
+
class PyldekCallbacks(poldek.callbacks):
def __init__(self, prefix = 'py'):
@@ -84,8 +103,10 @@
def __init__(self, source_name = None, verbose = 1, config = None):
ctx = poldek.poldek_ctx()
self._cb = PyldekCallbacks()
+ self._progress = vfileProgress()
ctx.set_callbacks(self._cb)
+ ctx.set_vfile_progress(self._progress)
ctx.set_verbose(verbose)
src = None
================================================================
Index: poldek/poldek/vfile/vfetch.c
diff -u poldek/poldek/vfile/vfetch.c:1.16 poldek/poldek/vfile/vfetch.c:1.17
--- poldek/poldek/vfile/vfetch.c:1.16 Sat Jun 23 21:18:58 2007
+++ poldek/poldek/vfile/vfetch.c Fri Jul 6 00:28:53 2007
@@ -127,12 +127,13 @@
struct stat st;
int rc = 0, vf_errno = 0;
int end = 1, ntry = 0;
- struct vf_progress_bar bar;
n_assert(reqtype == REQTYPE_FETCH || reqtype == REQTYPE_STAT);
- if (reqtype == REQTYPE_FETCH)
- req->bar = &bar;
+ if (reqtype == REQTYPE_FETCH) {
+ n_assert(req->bar == NULL);
+ req->bar = vf_progress_new(req->url);
+ }
if (vfile_conf.flags & VFILE_CONF_STUBBORN_RETR)
end = vfile_conf.nretries;
@@ -153,7 +154,7 @@
switch (reqtype) {
case REQTYPE_FETCH:
- vf_progress_init(&bar);
+ vf_progress_reset(req->bar);
rc = mod->fetch(req);
break;
@@ -198,7 +199,12 @@
req->bar = NULL;
if (!rc && req->destpath)
vf_unlink(req->destpath);
-
+
+ if (req->bar) {
+ vf_progress_free(req->bar);
+ req->bar = NULL;
+ }
+
return rc;
}
================================================================
Index: poldek/poldek/vfile/vfffmod.c
diff -u poldek/poldek/vfile/vfffmod.c:1.7 poldek/poldek/vfile/vfffmod.c:1.8
--- poldek/poldek/vfile/vfffmod.c:1.7 Wed Sep 28 01:28:02 2005
+++ poldek/poldek/vfile/vfffmod.c Fri Jul 6 00:28:53 2007
@@ -143,7 +143,7 @@
len = strlen(login) + 1 + strlen(req->host) + 1;
s = alloca(len);
- snprintf(s, len, "%s@%s", login, req->host);
+ n_snprintf(s, len, "%s@%s", login, req->host);
login = s;
host = req->proxy_host;
@@ -231,9 +231,8 @@
struct vcn *cn;
struct vfff_req vreq;
int rc;
-
- vfff_verbose = vfile_verbose;
+ vfff_verbose = vfile_verbose;
req->req_errno = 0;
if (recursion_deep > 32) {
================================================================
Index: poldek/poldek/vfile/vfile.c
diff -u poldek/poldek/vfile/vfile.c:1.70 poldek/poldek/vfile/vfile.c:1.71
--- poldek/poldek/vfile/vfile.c:1.70 Mon Jun 25 01:35:47 2007
+++ poldek/poldek/vfile/vfile.c Fri Jul 6 00:28:53 2007
@@ -46,13 +46,14 @@
int *vfile_verbose = &verbose;
static const char default_anon_passwd[] = "poldek at znienacka.net";
+extern struct vf_progress vf_tty_progress;
struct vfile_configuration vfile_conf = {
NULL, VFILE_CONF_STUBBORN_RETR, 1000 /* nretries */,
NULL, NULL, NULL,
&verbose,
(char*)default_anon_passwd,
- NULL, NULL
+ NULL, NULL, &vf_tty_progress
};
static inline const char *vfile_cachedir(void)
@@ -85,7 +86,8 @@
va_list ap;
int v, *vp, rc;
char *vs;
-
+ void *vv;
+
if (vfile_conf.default_clients_ht == NULL) {
vfile_conf.default_clients_ht = n_hash_new(7, free);
vfile_conf.proxies_ht = n_hash_new(7, free);
@@ -100,6 +102,11 @@
switch (param) {
case VFILE_CONF_LOGCB:
vfile_conf.log = va_arg(ap, void*);
+ break;
+
+ case VFILE_CONF_PROGRESS:
+ if ((vv = va_arg(ap, void*)))
+ vfile_conf.bar = vv;
break;
case VFILE_CONF_VERBOSE:
================================================================
Index: poldek/poldek/vfile/vfile.h
diff -u poldek/poldek/vfile/vfile.h:1.49 poldek/poldek/vfile/vfile.h:1.50
--- poldek/poldek/vfile/vfile.h:1.49 Mon Jun 25 01:35:47 2007
+++ poldek/poldek/vfile/vfile.h Fri Jul 6 00:28:53 2007
@@ -45,9 +45,8 @@
#define VFILE_CONF_NOPROXY (1 << 5) /*const char *hostmask */
#define VFILE_CONF_ANONFTP_PASSWD (1 << 6) /*const char *passwd */
#define VFILE_CONF_LOGCB (1 << 7) /*vf_vlog() like fn */
-#define VFILE_CONF_PROGRESSCB (1 << 8)
-#define VFILE_CONF_PROGRESSDATA (1 << 9)
-#define VFILE_CONF_STUBBORN_RETR (1 << 10)
+#define VFILE_CONF_PROGRESS (1 << 8) /* vf_progress struct */
+#define VFILE_CONF_STUBBORN_RETR (1 << 10) /* retry fetch on error */
#define VFILE_CONF_STUBBORN_NRETRIES (1 << 11) /* how many retries */
#define VFILE_CONF_EXTCOMPR (1 << 12) /* use external script to
file (de)compression */
@@ -218,6 +217,18 @@
/* create directory and lock it */
struct vflock *vf_lock_mkdir(const char *path);
+
+
+struct vf_progress {
+ void *data;
+ void *(*new)(void *data, const char *label);
+ void (*progress)(void *bar, long total, long amount);
+ void (*reset)(void *bar);
+ void (*free)(void *bar);
+};
+
+
+
#endif /* POLDEK_VFILE_H */
================================================================
Index: poldek/poldek/vfile/vfile_intern.h
diff -u poldek/poldek/vfile/vfile_intern.h:1.7 poldek/poldek/vfile/vfile_intern.h:1.8
--- poldek/poldek/vfile/vfile_intern.h:1.7 Mon Jun 25 01:35:47 2007
+++ poldek/poldek/vfile/vfile_intern.h Fri Jul 6 00:28:53 2007
@@ -36,6 +36,8 @@
#include <trurl/nhash.h>
extern int *vfile_verbose;
+struct vf_progress;
+
struct vfile_configuration {
char *_cachedir;
unsigned flags;
@@ -47,6 +49,7 @@
char *anon_passwd;
void (*log)(unsigned flags, const char *fmt, ...);
int (*sigint_reached)(int reset);
+ struct vf_progress *bar;
};
extern struct vfile_configuration vfile_conf;
================================================================
Index: poldek/poldek/vfile/vfprogress.c
diff -u poldek/poldek/vfile/vfprogress.c:1.16 poldek/poldek/vfile/vfprogress.c:1.17
--- poldek/poldek/vfile/vfprogress.c:1.16 Sun Jul 17 17:05:44 2005
+++ poldek/poldek/vfile/vfprogress.c Fri Jul 6 00:28:53 2007
@@ -1,5 +1,5 @@
/*
- Copyright (C) 2001 - 2004 Pawel A. Gajda <mis at k2.net.pl>
+ Copyright (C) 2001 - 2007 Pawel A. Gajda <mis at pld-linux.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2 as
@@ -29,10 +29,51 @@
#define PROGRESSBAR_WIDTH 30
-void vf_progress_init(struct vf_progress_bar *bar)
+/* state */
+#define VF_PROGRESS_VIRGIN 0
+#define VF_PROGRESS_DISABLED 1
+#define VF_PROGRESS_RUNNING 2
+
+struct tty_progress_bar {
+ int width;
+ int state;
+ int is_tty;
+ int prev_n;
+ int prev_perc;
+ time_t time_base;
+ time_t time_last;
+ float transfer_rate;
+ float eta; /* estimated time of arrival */
+ int maxlen;
+ int freq;
+};
+
+static void *tty_progress_new(void *data, const char *label);
+static void tty_progress(void *data, long total, long amount);
+static void tty_progress_reset(void *data);
+
+struct vf_progress vf_tty_progress = {
+ NULL, tty_progress_new, tty_progress, tty_progress_reset, free
+};
+
+static void *tty_progress_new(void *data, const char *label)
{
+ struct tty_progress_bar *bar;
+
+ label = label;
+ data = data;
+
+ bar = n_malloc(sizeof(*bar));
memset(bar, 0, sizeof(*bar));
+
+ bar->width = PROGRESSBAR_WIDTH;
+ bar->is_tty = isatty(fileno(stdout));
+ return bar;
+}
+static void tty_progress_reset(void *data)
+{
+ struct tty_progress_bar *bar = data;
bar->width = PROGRESSBAR_WIDTH;
bar->is_tty = isatty(fileno(stdout));
}
@@ -58,7 +99,7 @@
return snprintf(buf, bufsize, "%.1f%c", nb, unit);
}
-static int eta2str(char *buf, int bufsize, struct vf_progress_bar *bar)
+static int eta2str(char *buf, int bufsize, struct tty_progress_bar *bar)
{
int hh, mm, ss, n = 0;
float eta = bar->eta + 0.5;
@@ -74,13 +115,13 @@
}
-static void calculate_tt(long total, long amount, struct vf_progress_bar *bar)
+static void calculate_tt(long total, long amount, struct tty_progress_bar *bar)
{
time_t current_time;
current_time = time(NULL);
if (current_time == bar->time_last) {
- bar->freq++;
+ bar->freq++;
return;
}
@@ -91,16 +132,13 @@
bar->eta = (total - amount) / bar->transfer_rate;
}
-
-void vf_progress(long total, long amount, void *data)
+static void tty_progress(void *data, long total, long amount)
{
- struct vf_progress_bar *bar = data;
+ struct tty_progress_bar *bar = data;
char line[256], outline[256], fmt[40];
float frac, percent;
long n;
-
-
if (bar->state == VF_PROGRESS_DISABLED)
return;
@@ -226,3 +264,28 @@
bar->prev_perc = 10 * percent;
}
+
+void *vf_progress_new(const char *label)
+{
+ struct vf_progress *p = vfile_conf.bar;
+ return p->new(p->data, label);
+}
+
+void vf_progress_reset(void *bar)
+{
+ struct vf_progress *p = vfile_conf.bar;
+ p->reset(bar);
+}
+
+void vf_progress(void *bar, long total, long amount)
+{
+ struct vf_progress *p = vfile_conf.bar;
+ p->progress(bar, total, amount);
+}
+
+void vf_progress_free(void *bar)
+{
+ struct vf_progress *p = vfile_conf.bar;
+ if (p->free)
+ p->free(bar);
+}
================================================================
Index: poldek/poldek/vfile/vfreq.h
diff -u poldek/poldek/vfile/vfreq.h:1.6 poldek/poldek/vfile/vfreq.h:1.7
--- poldek/poldek/vfile/vfreq.h:1.6 Sun Jul 17 17:05:44 2005
+++ poldek/poldek/vfile/vfreq.h Fri Jul 6 00:28:53 2007
@@ -3,26 +3,11 @@
#include <time.h>
-#define VF_PROGRESS_VIRGIN 0
-#define VF_PROGRESS_DISABLED 1
-#define VF_PROGRESS_RUNNING 2
+void *vf_progress_new(const char *label);
+void vf_progress_reset(void *bar);
+void vf_progress(void *bar, long total, long amount);
+void vf_progress_free(void *bar);
-struct vf_progress_bar {
- int width;
- int state;
- int is_tty;
<<Diff was trimmed, longer than 597 lines>>
---- CVS-web:
http://cvs.pld-linux.org/poldek/poldek/lib_init.c?r1=1.83&r2=1.84&f=u
http://cvs.pld-linux.org/poldek/poldek/poldek.h?r1=1.36&r2=1.37&f=u
http://cvs.pld-linux.org/poldek/poldek/python/Makefile.am?r1=1.13&r2=1.14&f=u
http://cvs.pld-linux.org/poldek/poldek/python/poldek.py?r1=1.12&r2=1.13&f=u
http://cvs.pld-linux.org/poldek/poldek/python/poldekmod.i?r1=1.14&r2=1.15&f=u
http://cvs.pld-linux.org/poldek/poldek/python/pyldek.py?r1=1.4&r2=1.5&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfetch.c?r1=1.16&r2=1.17&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfffmod.c?r1=1.7&r2=1.8&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfile.c?r1=1.70&r2=1.71&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfile.h?r1=1.49&r2=1.50&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfile_intern.h?r1=1.7&r2=1.8&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfprogress.c?r1=1.16&r2=1.17&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfreq.h?r1=1.6&r2=1.7&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfff/vfff.c?r1=1.6&r2=1.7&f=u
http://cvs.pld-linux.org/poldek/poldek/vfile/vfff/vfff.h?r1=1.4&r2=1.5&f=u
More information about the pld-cvs-commit
mailing list