From e289669ebe26cdaa3b505ec50ecf9bd348a614f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Dunand?= <aurelien.dunand@smartjog.com>
Date: Fri, 29 Jul 2011 16:48:34 +0200
Subject: [PATCH] Add search command

seblu: fix some code

Signed-off-by: Seblu <sebastien.luttringer@smartjog.com>
---
 bin/is                       |  6 +++++-
 installsystems/repository.py | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/bin/is b/bin/is
index e87d83c..50c2651 100755
--- a/bin/is
+++ b/bin/is
@@ -221,7 +221,10 @@ def c_search(parser, args):
     '''
     Search in repository
     '''
-    raise NotImplementedError("Not yet implemented")
+    repoman = load_repositories(args)
+    for repo in repoman:
+        arrow(repo.config.name)
+        repo.search(args.pattern)
 
 def c_get(parser, args):
     '''
@@ -351,6 +354,7 @@ p_cat.set_defaults(func=c_cat)
 
 # search command parser
 p_search = subparsers.add_parser("search", help=c_search.__doc__.lower())
+p_search.add_argument("pattern", help="search pattern in repository")
 p_search.set_defaults(func=c_search)
 
 # get command parser
diff --git a/installsystems/repository.py b/installsystems/repository.py
index 84ca421..5579fdb 100644
--- a/installsystems/repository.py
+++ b/installsystems/repository.py
@@ -202,6 +202,22 @@ class Repository(object):
                     out('    #yellow#MD5:#reset# %s' % payload_md5)
                 out()
 
+    def search(self, pattern):
+        '''
+        Search pattern in a repository
+        '''
+        images = self.db.ask("SELECT name, version, author, description\
+                              FROM image\
+                              WHERE name LIKE ? OR\
+                              description LIKE ? OR\
+                              author LIKE ?",
+                             tuple( ["%%%s%%" % pattern ] * 3)
+                             ).fetchall()
+        for name, version, author, description in images:
+            arrow("%s v%s" % (name, version), 1)
+            out("   #yellow#Author:#reset# %s" % author)
+            out("   #yellow#Description:#reset# %s" % description)
+
     def _remove_file(self, filename):
         '''
         Remove a filename from pool. Check if it's not needed by db before
-- 
GitLab