poldek: poldek/python/poldek.py, poldek/python/poldekmod.i, poldek...
mis
mis at pld-linux.org
Wed Jun 27 18:20:51 CEST 2007
Author: mis Date: Wed Jun 27 16:20:51 2007 GMT
Module: poldek Tag: HEAD
---- Log message:
- extended and pythonized
---- Files affected:
poldek/poldek/python:
poldek.py (1.9 -> 1.10) , poldekmod.i (1.11 -> 1.12) , test.py (1.15 -> 1.16)
---- Diffs:
================================================================
Index: poldek/poldek/python/poldek.py
diff -u poldek/poldek/python/poldek.py:1.9 poldek/poldek/python/poldek.py:1.10
--- poldek/poldek/python/poldek.py:1.9 Fri Jun 22 00:35:08 2007
+++ poldek/poldek/python/poldek.py Wed Jun 27 18:20:46 2007
@@ -40,11 +40,15 @@
return r
def __str__(self):
- return '[' + string.join(map(str, map(self._itemClass, self._arr)), ', ') + ']'
-
+ return '[' + self.join(', ') + ']'
+
def __setitem__(self, i, val):
raise TypeError, "tn_array is immutable"
+ def join(self, sep = ', '):
+ if self._arr is None: return ''
+ return string.join(map(str, map(self._itemClass, self._arr)), sep)
+
def n_array_proxy_func(prefix, func, classnam):
return eval('lambda self, *args: n_array_proxy(poldekmod.%s%s(self, *args), %s)' %
(prefix, func, classnam));
@@ -53,35 +57,81 @@
return eval('lambda self, *args: n_array_proxy(poldekmod.%s%s(self, *args), %s)' %
(module, prefix, func, classnam));
+def _modsymbols(prefix, strip_prefix = None, with_methods = True, with_constants = True,
+ verbose = 0, exclude = None):
+
+ exclude_regexp = None
+ regexp = re.compile('^%s' % prefix)
+ regexp_up = re.compile('^%s' % string.upper(prefix))
+
+ if exclude:
+ exclude_regexp = re.compile('^%s' % exclude)
+
+ if strip_prefix:
+ l = len(strip_prefix)
+ else:
+ l = len(prefix)
+
+ symbols = {}
+ for k, elem in poldekmod.__dict__.items():
+ key = None
+
+ if exclude_regexp and exclude_regexp.match(k):
+ if verbose:
+ print "Exclude %s:: %s" % (prefix, k)
+ continue
+
+ if with_methods and regexp.match(k) and type(elem) == BuiltinFunctionType:
+ key = k
+
+ elif with_constants and regexp_up.match(k) and type(elem) != BuiltinFunctionType:
+ key = k
+
+ else:
+ if verbose:
+ print "skipped %s", k
+
+ if key:
+ symbols[ key[l:] ] = elem
+
+ return symbols
-def _complete_class(aclass, prefix, delprefix = None, nomethods = False,
+
+def _complete_class(aclass, prefix, strip_prefix = None, with_methods = True,
verbose = 0, exclude = None):
exclude_regexp = None
regexp = re.compile('^%s' % prefix)
regexp_up = re.compile('^%s' % string.upper(prefix))
+ toskip = [ '%snew' % prefix , '%sfree' % prefix, '%sswigregister' % prefix,
+ '%sctx_swigregister' % prefix]
if exclude:
exclude_regexp = re.compile('^%s' % exclude)
- if delprefix:
- l = len(delprefix)
+ if strip_prefix:
+ l = len(strip_prefix)
else:
l = len(prefix)
- for k, elem in poldekmod.__dict__.items():
- #elem = poldekmod.__dict__[k]
- if not nomethods:
+ for k, elem in poldekmod.__dict__.iteritems():
+
+ if with_methods:
if exclude_regexp and exclude_regexp.match(k):
if verbose:
print "Exclude %s %s" % (aclass, k)
continue
if regexp.match(k) and type(elem) == BuiltinFunctionType:
+ #print "FF %s %s" % (k, toskip)
+ if k in toskip:
+ if verbose: print "XXXSkipped %s" % k
+ continue
+
name = k[l:]
if not hasattr(aclass, name):
fn = eval('lambda self, *args: poldekmod.%s(self, *args)' % k);
setattr(aclass, name, fn)
if verbose:
- print "SET %s %s %s" % (aclass, name, type(fn))
+ print "SET %s %s %s %s" % (k, aclass, name, type(fn))
#setattr(aclass, name, elem)
@@ -92,51 +142,71 @@
if verbose:
print "SET %s %s %s" % (aclass, name, type(elem))
+
_complete_class(tn_array, 'n_array_')
setattr(tn_array, '__getitem__', tn_array.nth)
+## Package
for name, elem in capreq.__dict__.items():
if name[0:4] == '_is_':
setattr(capreq, name[1:], elem)
_complete_class(capreq, 'capreq_', verbose = 0)
setattr(capreq, '__str__', eval('lambda self: poldekmod.capreq_snprintf_s(self)'))
-
-
-_complete_class(poldek_ctx, 'poldek_')
-for fn in ['get_avail_packages', 'search_avail_packages']:
- setattr(poldek_ctx, fn, n_array_proxy_func('poldek_', fn, 'pkg'))
-
-_complete_class(poldek_ts, 'poldek_ts_')
-_complete_class(poldek_ts, 'poldek_op_', delprefix = 'poldek_',
- nomethods = True, verbose = 0)
+setattr(pkgflist_it, '__iter__', eval('lambda self: self'));
+def _pkgflist_it_next(self):
+ t = self.get_tuple()
+ if t is None:
+ raise StopIteration
+ return t
+setattr(pkgflist_it, 'next', _pkgflist_it_next);
_complete_class(pkg, 'pkg_', verbose = 0)
setattr(pkg, '__str__', pkg.id)
+setattr(pkg, 'group', property(eval("lambda self: poldekmod.pkg_group(self)")))
-for c in ['provides', 'requires', 'conflicts']:
- setattr(pkg, c, n_array_proxy_func('pkg.', '_get_%s' % c, 'capreq'))
+for c in ['provides', 'requires', 'conflicts', 'suggests']:
+ setattr(pkg, c, property(n_array_proxy_func('pkg.', '_get_%s' % c, 'capreq')))
+setattr(pkg, 'files', property(eval("lambda self: poldekmod.pkg_get_flist_it(self)")));
-_complete_class(pkguinf, 'pkguinf_', exclude = 'pkguinf_get', verbose = 0)
-setattr(pkguinf, 'get', eval('lambda self, tag: poldekmod.pkguinf_get(self, ord(tag[0]))'))
-
-_complete_class(pkgflist_it, 'pkgflist_it_', verbose = 0)
-setattr(pkgflist_it, 'get', eval('lambda self, *args: poldekmod.pkgflist_it_get(self, None)'));
+tags = _modsymbols('pkguinf_', with_methods = False)
+for k, v in tags.iteritems():
+ fn = "lambda self: poldekmod.pkguinf_get(self, ord('%s'))" % v[0]
+ setattr(pkguinf, k.lower(), property(eval(fn)))
+## pkgdir
_complete_class(source, 'source_')
+setattr(source, 'enabled', property(lambda self: self.get_enabled(),
+ lambda self, val: self.set_enabled(val)))
_complete_class(pkgdir, 'pkgdir_')
+setattr(pkgdir, 'packages', property(n_array_proxy_func('', 'get_packages', 'pkgdir')))
+# poldek
+_complete_class(poldek_ctx, 'poldek_')
+setattr(poldek_ctx, 'packages', property(n_array_proxy_func('poldek_', 'get_avail_packages', 'pkg')));
+setattr(poldek_ctx, 'search', n_array_proxy_func('poldek_', 'search_avail_packages', 'pkg'))
+setattr(poldek_ctx, 'sources', property(n_array_proxy_func('poldek_', 'get_sources', 'source')))
+setattr(poldek_ctx, 'pkgdirs', property(n_array_proxy_func('poldek_', 'get_pkgdirs', 'pkgdir')))
-_complete_class(poclidek_rcmd, 'poclidek_rcmd_')
-setattr(poclidek_rcmd, 'get_packages',
- n_array_proxy_func('poclidek_rcmd_', 'get_packages', 'pkg'))
+_complete_class(poldek_ts, 'poldek_ts_')
+_complete_class(poldek_ts, 'poldek_op_', strip_prefix = 'poldek_',
+ with_methods = False, verbose = 0)
-_complete_class(poclidek_ctx, 'poclidek_', verbose = 0, exclude = 'poclidek_rcmd_')
+#_complete_class(poclidek_rcmd, 'poclidek_rcmd_')
+setattr(poclidek_rcmd, 'packages',
+ property(n_array_proxy_func('poclidek_rcmd_', 'get_packages', 'pkg')))
+
+_complete_class(poclidek_ctx, 'poclidek_', verbose = 0, exclude = 'poclidek_rcmd_')
+setattr(poclidek_ctx, 'rcmd', lambda self: poldekmod.poclidek_rcmd_new(self, None));
+#print poclidek_ctx.rcmd_new
+#setattr(poclidek_rcmd, 'rcmd'
+#_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]))'))
================================================================
Index: poldek/poldek/python/poldekmod.i
diff -u poldek/poldek/python/poldekmod.i:1.11 poldek/poldek/python/poldekmod.i:1.12
--- poldek/poldek/python/poldekmod.i:1.11 Fri Jun 22 00:35:08 2007
+++ poldek/poldek/python/poldekmod.i Wed Jun 27 18:20:46 2007
@@ -82,10 +82,25 @@
*/
%extend poclidek_rcmd {
- poclidek_rcmd(struct poclidek_ctx *cctx, struct poldek_ts *ts) {
+ /*poclidek_rcmd(struct poclidek_ctx *cctx, struct poldek_ts *ts) {
return poclidek_rcmd_new(cctx, ts);
+ }*/
+
+ poclidek_rcmd(struct poclidek_ctx *cctx) {
+ return poclidek_rcmd_new(cctx, NULL);
+ }
+
+ int execute(const char *cmdline) {
+ return poclidek_rcmd_execline(self, cmdline);
}
+ char *to_s() {
+ const char *s = poclidek_rcmd_get_str(self);
+ if (s)
+ return n_strdup(s);
+ return NULL;
+ }
+
~poclidek_rcmd() { poclidek_rcmd_free(self); }
};
@@ -134,6 +149,35 @@
source(const char *name) {
return source_new(name, NULL, NULL, NULL); }
+
+ source(void *src) { return source_link(src); };
+
+ char *__str__() {
+ char *id = NULL;
+ if (self->flags & PKGSOURCE_NAMED)
+ id = self->name;
+ else {
+ char path[PATH_MAX];
+ vf_url_slim(path, sizeof(path), self->path, 0);
+ id = path;
+ }
+ return n_strdup(id);
+ }
+
+ int get_enabled() {
+ //printf("get_enabled %d\n", (self->flags & PKGSOURCE_NOAUTO) == 0);
+ return (self->flags & PKGSOURCE_NOAUTO) == 0;
+ }
+
+ void set_enabled(int v) {
+ if (v)
+ self->flags &= ~PKGSOURCE_NOAUTO;
+ else
+ self->flags |= PKGSOURCE_NOAUTO;
+
+ //printf("%s enabled=%d\n", self->name, (self->flags & PKGSOURCE_NOAUTO) == 0);
+ }
+
~source() { source_free(self); }
}
@@ -141,6 +185,8 @@
pkgdir(struct source *src, unsigned flags) {
return pkgdir_srcopen(src, flags);
}
+ tn_array *get_packages() { return self->pkgs; }
+
~pkgdir() { pkgdir_free(self); }
}
@@ -174,6 +220,7 @@
tn_array *_get_provides() { return self->caps; }
tn_array *_get_requires() { return self->reqs; }
tn_array *_get_conflicts() { return self->cnfls; }
+ tn_array *_get_suggests() { return self->sugs; }
~pkg() { pkg_free(self); }
}
================================================================
Index: poldek/poldek/python/test.py
diff -u poldek/poldek/python/test.py:1.15 poldek/poldek/python/test.py:1.16
--- poldek/poldek/python/test.py:1.15 Sat Jun 16 17:11:24 2007
+++ poldek/poldek/python/test.py Wed Jun 27 18:20:46 2007
@@ -6,43 +6,55 @@
from types import *
import poldek
-def test_pkguinf(ctx):
- arr = ctx.get_avail_packages()
- print "Loaded %d packages" % len(arr)
- if len(arr) == 0:
- return
+def package_inspect(pkg):
+ print "Package: ", pkg
+
+ inf = pkg.uinf() # loaded on demand
+ if inf: # index w/o uinf?
+ print "== uinf == "
+ print "Summary: ", inf.summary
+ print "License: ", inf.license
+ print "URL: ", inf.url
+ print "Vendor: ", inf.vendor
+ print "Buildhost:", inf.buildhost
+ print "Distro: ", inf.distro
+ print "Group: ", pkg.group
+ print "Description:\n", inf.description
+
+ print "== properties =="
+ if pkg.provides:
+ print "Provides:", pkg.provides.join()
+
+ if pkg.conflicts:
+ print "Conflicts:", pkg.conflicts.join()
+
+ if pkg.suggests:
+ print "Suggests:", pkg.suggests
- pkg = arr[0]
- inf = pkg.uinf()
- print "Package: ", pkg
- print "Summary: ", inf.get(inf.SUMMARY)
- print "License: ", inf.get(inf.LICENSE)
- print "URL: ", inf.get(inf.URL)
- print "Group: ", pkg.group()
- print "Description:\n", inf.get(inf.DESCRIPTION)
- it = pkg.get_flist_it()
- f = it.get_tuple()
- while f:
- print f
- f = it.get_tuple()
-
-
-def test_avail(ctx):
- arr = ctx.get_avail_packages()
- print "Loaded %d packages" % len(arr)
- n = 0
- for ptr in arr:
- print n, ' ', ptr
- n += 1
-
-
-def test_search(ctx):
- arr = ctx.get_avail_packages()
- print "Found %d package(s)" % len(arr)
- n = 0
- for ptr in arr:
- print n, ' ', ptr
- n += 1
+ if pkg.requires:
+ print "Requires:", pkg.requires.join()
+ for r in pkg.requires:
+ type = ''
+ if r.is_prereq():
+ type += 'pre'
+
+ if r.is_prereq_un():
+ if len(type): type += ', '
+ type += 'preun'
+
+ if r.is_autodirreq():
+ type = 'dir'
+
+ if len(type) > 0:
+ print " - requirement(%s): %s" % (type, r)
+ else:
+ print " - requirement: %s" % r
+
+
+ print "Files: "
+ for (name, size, mode_t) in pkg.files:
+ print " ", name
+
def test_install(ctx):
ts = ctx.ts_new(poldek.poldek_ts.INSTALL | poldek.poldek_ts.UPGRADE)
@@ -52,57 +64,70 @@
ts.run(None)
+def cli_command(cctx, command):
+ cmd = cctx.rcmd()
+ if cmd.execute(command):
+ for p in cmd.packages:
+ package_inspect(p)
+
+
+def init_poldek_ctx(source_name = None):
+ ctx = poldek.poldek_ctx()
+ #ctx.set_verbose(1)
+
+ src = None
+ if source_name: # -n source_name ?
+ print "configure %s" % source_name
+ src = poldek.source(source_name)
+ ctx.configure(ctx.CONF_SOURCE, src)
+ ctx.load_config()
-def test_cli_ls(cctx):
- cmd = cctx.rcmd_new(None)
- if cmd.execline("ls poldek*"):
- pkgs = cmd.get_packages()
- return
- print pkgs
- n = 0
- for p in pkgs:
- print n, ' ', p
- caps = p.requires()
- print caps
- for cap in caps:
- print " R: %s" % cap
- n += 1
-
- if not pkgs:
- print cmd.get_str()
+ if not ctx.setup():
+ raise Exception, "error"
-poldek.lib_init()
+ return (ctx, src)
-ctx = poldek.poldek_ctx()
-#poldek_set_verbose(1)
-src = poldek.source('ac')
-ctx.configure(ctx.CONF_SOURCE, src)
-ctx.load_config()
-if not ctx.setup():
- raise Exception, "error"
-
-
-cctx = poldek.poclidek_ctx(ctx);
-cctx.load_packages(cctx.LOAD_ALL)
-#test_cli_ls(cctx)
-
-
-test_pkguinf(ctx)
-#test_cli_ls(poldctx)
-#arr = poldctx.get_avail_packages()
-sys.exit(0)
-ctx = poldek.poldek_ctx()
-#poldek_set_verbose(1)
-src = poldek.source('tt2')
-ctx.configure(ctx.CONF_SOURCE, poldek.source('tt2'))
-ctx.load_config()
-ctx.setup()
+def demo_poldeklib(source_name = None):
+ (ctx, src) = init_poldek_ctx(source_name)
-test_search(ctx)
+ print "Sources: "
+ for s in ctx.sources:
+ if not src:
+ s.set_enabled(False)
+ print " -", s
+
+ if src is None:
+ src = ctx.sources[0]
+ src.set_enabled(True) # load first source
+
+ print "Loading %s..." % src
+ if ctx.load_sources():
+ print " loaded %d packages" % ctx.packages.length()
+ package_inspect(ctx.packages[0])
+
+
+def demo_poclideklib(source_name = None):
+ (ctx, src) = init_poldek_ctx(source_name)
+
+ for s in ctx.sources:
+ if not src:
+ s.set_enabled(False)
+ print " -", s
+
+ if src is None:
+ src = ctx.sources[0]
+ src.set_enabled(True) # load first source
+
+ cctx = poldek.poclidek_ctx(ctx);
+ cctx.load_packages(cctx.LOAD_ALL) # see poclidek.h
+ cli_command(cctx, "ls poldek*");
-print "END"
+
+poldek.lib_init()
+
+demo_poclideklib()
================================================================
---- CVS-web:
http://cvs.pld-linux.org/poldek/poldek/python/poldek.py?r1=1.9&r2=1.10&f=u
http://cvs.pld-linux.org/poldek/poldek/python/poldekmod.i?r1=1.11&r2=1.12&f=u
http://cvs.pld-linux.org/poldek/poldek/python/test.py?r1=1.15&r2=1.16&f=u
More information about the pld-cvs-commit
mailing list