Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Search with Pip #80

Closed
psaavedra opened this issue Mar 12, 2015 · 10 comments
Closed

Allow Search with Pip #80

psaavedra opened this issue Mar 12, 2015 · 10 comments

Comments

@psaavedra
Copy link

This patch hacks the code in order to allow pip search PKG. For example:

pip search  --index http://pypi.intenet.com:8080/search/ MyDjango

I wonder if you are interested on this and if this patch is wellcome for your side.

--- /root/_app.py   2015-03-12 11:16:23.000000000 +0000
+++ /usr/local/lib/python2.7/dist-packages/pypiserver/_app.py   2015-03-12 12:26:29.000000000 +0000
@@ -6,6 +6,7 @@
 import mimetypes
 import logging
 import pkg_resources
+import xml.dom.minidom
 
 try:
     from io import BytesIO
@@ -24,6 +25,48 @@
 log = logging.getLogger('pypiserver.http')
 packages = None
 
+XMLRPC_RESPONSE_SKELETON='''
+
+
+
+
+%(objects)s
+
+
+
+'''
+
+def from_obj_to_xmlrpc(obj_):
+    res = ""
+    if isinstance(obj_,int):
+        res += "%s" % obj_
+        return res
+    elif isinstance(obj_,str):
+        res += "%s" % obj_
+        return res
+    elif isinstance(obj_,dict):
+        res += ""
+        for k,v in obj_.iteritems():
+            member = {}
+            member["name"]=k
+            member["value"]=from_obj_to_xmlrpc(v)
+            res_1 = ""
+            res_1 += "%(name)s"
+            res_1 += "%(value)s"
+            res_1 += ""
+            res += res_1 % member
+        res += ""
+        return res
+    elif isinstance(obj_,list):
+        res += ""
+        for i in obj_:
+            res += "%s" % from_obj_to_xmlrpc(i)
+        res += ""
+        return res
+    else:
+       raise Exception("No valid object")
+
+
 
 class Configuration(object):
     def __init__(self):
@@ -267,8 +310,31 @@
     return redirect(request.fullpath + "/")
 
 
-@app.route("/simple/")
+@app.post('/search')
+@app.post('/search/')
 @auth("list")
+def search():
+    value = ""
+    try:
+        parser = xml.dom.minidom.parse(request.body)
+        member = parser.getElementsByTagName("member")[0]
+        value  = parser.getElementsByTagName("string")[0].childNodes[0].wholeText.strip()
+    except Exception, e:
+        value = ""
+
+    response = []
+    ordering = 0
+    for p in packages():
+        if p.pkgname.count(value) > 0:
+            d = {'_pypi_ordering': ordering, 'version': p.version,
+                 'name': p.pkgname, 'summary': p.fn}
+            response.append(d)
+        ordering += 1
+    res = XMLRPC_RESPONSE_SKELETON \
+        % {"objects":from_obj_to_xmlrpc(response)}
+    return res
+
+@app.route("/simple/")
 def simpleindex():
     links = sorted(get_prefixes(packages()))
     tmpl = """\

@ankostis
Copy link
Member

Of course we are interested for new features, as long as they do not require http-requests upstream or a db.

Would mind pointing at the points of your patch that you consider "hack"-ish?

@psaavedra
Copy link
Author

Yes of course,

  • search operation is a XMLRPC request in fact, I just implemented the search request ad-hoc (see XMLRPC_RESPONSE_SKELETON), there is other operations which are in use by pip that they are not implemented. Maybe the best way to do it is using the xmlrpc.server (https://docs.python.org/3/library/xmlrpc.server.html) but this force to use a additional port.
  • from_obj_to_xmlrpc is a made by myself translator from built-in data to XML-RPC data model.
  • Package summary is replaced by the package filename since the moment I don't know how to recover this info with Anyway, if you just want using the returned object of the function packages()

Anyway, this patch is fullysafe and doesn't modifies any built logic of your code. You can play with it and with the search commnd in our recent installation of PyPIserver : http://pypi.interoud.net/:

pip search --index http://pypi.interoud.com:8080/search/ live

Thanks a lot for your PyPI clone. ;-)

@ankostis ankostis added this to the M1.1.8 milestone Mar 15, 2015
@ankostis
Copy link
Member

Would need a test-case here.

@ankostis ankostis modified the milestones: M1.1.9, M1.1.8 Sep 11, 2015
@ankostis
Copy link
Member

Dear @psaavedra would you mind making a regular pull-request?

@ankostis ankostis removed this from the M1.1.9 milestone Dec 20, 2015
@blade2005
Copy link
Contributor

@ankostis i'm really interesting in this feature. I'm gonna make a branch and see about get this ready including a test case.

blade2005 pushed a commit to blade2005/pypiserver that referenced this issue Feb 17, 2016
@janssen
Copy link

janssen commented Mar 3, 2016

I'd kind of like to see this, too.

@janssen
Copy link

janssen commented Mar 3, 2016

Why in the world does pypi drag in XML-RPC for this simple thing? You could just send a request the same way (using forms), and send back the response as a pickled Python dict, or as a JSON dict.

@psaavedra
Copy link
Author

Pull request submitted: #115

@blade2005
Copy link
Contributor

My pr #114 handles it in a cleaner fashion than the proposed code

@mplanchard
Copy link
Contributor

Handled by #114, which has been merged.

@ankostis ankostis added this to the No Action milestone Jun 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants