1 #!/usr/bin/env python3.8
7 from urllib.parse import unquote
9 sqlports = os.environ.get("SQLPORTS")
11 query = os.environ.get("QUERY_STRING")
12 script_path = os.environ.get("SCRIPT_NAME")
13 path = os.environ.get("PATH_INFO")
16 select webpkg_fts.pkgstem,
20 join _ports p on p.fullpkgpath = webpkg_fts.id
21 join _paths paths on paths.id = webpkg_fts.id
22 where webpkg_fts match ?
23 order by bm25(webpkg_fts)
26 query_by_fullpkgpath = """
32 replace(replace(e.value, '@', ' at '), '.', ' dot '),
36 join _descr d on d.fullpkgpath = p.id
37 join _ports pp on pp.fullpkgpath = p.id
38 join _email e on e.keyref = pp.maintainer
39 left join _readme r on r.fullpkgpath = p.id
40 where p.fullpkgpath = ?
43 query_all_categories = """
44 select distinct value from categories order by value
47 query_all_in_category = """
48 select fullpkgpath from categories where value = ? order by fullpkgpath
52 def verbatim(alt, text):
54 for line in text.splitlines():
55 if line.startswith("```"):
63 for line in text.splitlines():
64 if line.startswith(">") \
65 or line.startswith("```") \
66 or line.startswith(">") \
67 or line.startswith("#") \
68 or line.startswith("*"):
74 def reply(code, meta):
75 print(f"{code} {meta}\r")
79 reply(20, "text/gemini;lang=en")
82 print("Welcome to GemPKG,",
83 "the gemini interface for the OpenBSD ports collection.")
85 print(f"=> {script_path}/search Search for a package")
86 print(f"=> {script_path}/all All categories")
89 "What you search will be matched against the package name (pkgstem),",
90 "comment, DESCR and maintainer.")
95 print(f"=> {script_path}/ GemPKG")
96 print(f"=> {script_path}/search Search for a package")
97 print(f"=> {script_path}/all All Categories")
106 "All the data is provided by sqlports",
107 "and is relative to OpenBSD-CURRENT."
111 def fts_escape_word(s):
112 return '"' + s.replace('"', '""') + '"'
116 return ' '.join(fts_escape_word(i) for i in s.split())
123 reply(10, "search for a package")
126 query = unquote(query)
128 # try the raw query. If it isn't a valid fts string (i.e. "c++"),
129 # escape it and retry
131 cursor = conn.execute(query_search, (query, ))
132 except BaseException:
133 cursor = conn.execute(query_search, (fts_escape(query), ))
135 reply(20, "text/gemini;lang=en")
138 stem, comment, fullpkgpath = row
139 print(f"=> {script_path}/{fullpkgpath}/ {stem}: {comment}")
144 cursor = conn.execute(query_all_categories)
145 reply(20, "text/gemini;lang=en")
147 print("# All categories")
149 (fullpkgpath, ) = row
150 print(f"=> {script_path}/{fullpkgpath}/ {fullpkgpath}")
155 cursor = conn.execute(query_all_in_category, (path, ))
156 reply(20, "text/gemini;lang=en")
158 print(f"# All ports in category {path}")
160 (fullpkgpath, ) = row
161 if fullpkgpath.find(",") != -1: # skip flavors
163 print(f"=> {script_path}/{fullpkgpath}/ {fullpkgpath}")
168 fullpkgpath, stem, comment, pkgname, descr, maintainer, readme, www = row
169 reply(20, "text/gemini;lang=en")
171 print(f"# {path}", "v" + re.sub(r".*-", "", pkgname))
172 print(f"``` Command to execute in order to install the package {stem}")
173 print(f"# pkg_add {stem}")
176 print(f"> {comment}")
178 print(f"=> https://cvsweb.openbsd.org/ports/{fullpkgpath} CVS web")
180 print(f"=> {www} WWW")
182 print("Maintainer:", maintainer)
184 print("## Description")
185 verbatim(f"{stem} description", descr)
189 verbatim(f"README for {stem}", readme)
193 if not path or path == '/':
197 if not path.endswith("/"):
198 reply(31, f"{script_path}{path}/")
200 # drop the leading and trailing /
201 path = path.strip("/")
204 conn = sqlite3.connect(sqlports)
211 cursor = conn.execute(query_by_fullpkgpath, (path, ))
212 row = cursor.fetchone()
219 except BaseException:
220 reply(50, "internal server error")
221 traceback.print_exc()