Skip to content

Instantly share code, notes, and snippets.

@copyninja
Created August 28, 2014 11:28
Show Gist options
  • Save copyninja/575bb3d9fa9e8ca3c8fc to your computer and use it in GitHub Desktop.
Save copyninja/575bb3d9fa9e8ca3c8fc to your computer and use it in GitHub Desktop.
CodeSearch query
import argparse
import requests
import sys
from collections import namedtuple
from bs4 import BeautifulSoup
CSResult = namedtuple('CSResult', ['pkg', 'version', 'file', 'href', 'lineno'])
def fetch_page(query):
r = requests.get('http://codesearch.debian.net/search?q={}'.format(query))
return r.text, r.status_code
def parse_result_data(data):
bs = BeautifulSoup(data)
results = bs.select('html body div#content ul#results li a')
if len(results) == 0:
return None
for result in results:
href = result.attrs.get('href')
rline = result.text
pkg, *rem = rline.split('_')
version, *file_line = rem[0].split('/')
path, lineno = file_line[0].split(':')
yield CSResult(pkg, version, path, href, lineno)
def display_result(results):
for r in results:
print("%-6s : %20s", 'Package', r.pkg)
print("%-6s : %20s", 'Version', r.version)
print("%-6s : %20s", 'File Match', r.file)
print("%-6s : %20s", 'Line No', r.lineno)
print("%-6s : %20s", 'URL', r.href)
def setup_options():
parser = argparse.ArgumentParser(
description="Command line interface to codesearch.debian.net",
epilog="For more information please check \
http://codesearch.debian.net/faq")
# query
parser.add_argument("-q", "--query", action="append",
help="Query to be executed, can be used more than once",
required=True)
# options to code search
parser.add_argument("-f", "--file-type",
help="Search only in specific file type",
choices=["c", "c++", "perl", "python", "go", "java",
"ruby", "shell", "vala"])
parser.add_argument("-P", "--package", help="Search in only given package")
parser.add_argument("-p", "--path", help="Search only in given path")
return parser
def main():
parser = setup_options()
args = parser.parse_args()
query = []
query.append(*args.query)
if args.file_type:
query.append('filetype:' + args.file_type)
if args.package:
query.append('package:' + args.package)
if args.path:
query.append('path:' + args.path)
page, scode = fetch_page(' '.join(query))
if scode != 200:
printf("Something went wrong with server, code: {}".format(scode),
file=sys.stderr)
return 1
else:
rgen = parse_result_data(page)
if rgen is None:
printf("No match found for your query", file=sys.stderr)
return 1
display_result(rgen)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment