Blob


1 #!/usr/bin/env python3.8
3 import os
4 import re
5 import sqlite3
6 import traceback
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")
15 query_search = """
16 select webpkg_fts.pkgstem,
17 webpkg_fts.comment,
18 paths.fullpkgpath
19 from webpkg_fts
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)
24 """
26 query_by_fullpkgpath = """
27 select p.fullpkgpath,
28 pp.pkgstem,
29 pp.comment,
30 pp.pkgname,
31 d.value,
32 replace(replace(e.value, '@', ' at '), '.', ' dot '),
33 r.value,
34 pp.homepage
35 from _paths p
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 = ?
41 """
43 query_all_categories = """
44 select distinct value from categories order by value
45 """
47 query_all_in_category = """
48 select fullpkgpath from categories where value = ? order by fullpkgpath
49 """
52 def verbatim(alt, text):
53 print("```", alt)
54 for line in text.splitlines():
55 if line.startswith("```"):
56 print(" ", line)
57 else:
58 print(line)
59 print("```")
62 def printraw(text):
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("*"):
69 print(" ", line)
70 else:
71 print(line)
74 def reply(code, meta):
75 print(f"{code} {meta}\r")
78 def homepage():
79 reply(20, "text/gemini;lang=en")
80 print("# GemPKG")
81 print("")
82 print("Welcome to GemPKG,",
83 "the gemini interface for the OpenBSD ports collection.")
84 print("")
85 print(f"=> {script_path}/search Search for a package")
86 print(f"=> {script_path}/all All categories")
87 print("")
88 print(
89 "What you search will be matched against the package name (pkgstem),",
90 "comment, DESCR and maintainer.")
91 foot()
94 def nav():
95 print(f"=> {script_path}/ GemPKG")
96 print(f"=> {script_path}/search Search for a package")
97 print(f"=> {script_path}/all All Categories")
98 print("")
101 def foot():
102 print()
103 print()
104 print("---")
105 print(
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('"', '""') + '"'
115 def fts_escape(s):
116 return ' '.join(fts_escape_word(i) for i in s.split())
119 def searchpage():
120 global query
122 if not query:
123 reply(10, "search for a package")
124 return
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
130 try:
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")
136 nav()
137 for row in cursor:
138 stem, comment, fullpkgpath = row
139 print(f"=> {script_path}/{fullpkgpath}/ {stem}: {comment}")
140 foot()
143 def allcatspage():
144 cursor = conn.execute(query_all_categories)
145 reply(20, "text/gemini;lang=en")
146 nav()
147 print("# All categories")
148 for row in cursor:
149 (fullpkgpath, ) = row
150 print(f"=> {script_path}/{fullpkgpath}/ {fullpkgpath}")
151 foot()
154 def bycatpage():
155 cursor = conn.execute(query_all_in_category, (path, ))
156 reply(20, "text/gemini;lang=en")
157 nav()
158 print(f"# All ports in category {path}")
159 for row in cursor:
160 (fullpkgpath, ) = row
161 if fullpkgpath.find(",") != -1: # skip flavors
162 continue
163 print(f"=> {script_path}/{fullpkgpath}/ {fullpkgpath}")
164 foot()
167 def portpage(row):
168 fullpkgpath, stem, comment, pkgname, descr, maintainer, readme, www = row
169 reply(20, "text/gemini;lang=en")
170 nav()
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}")
174 print("```")
175 print("")
176 print(f"> {comment}")
177 print("")
178 print(f"=> https://cvsweb.openbsd.org/ports/{fullpkgpath} CVS web")
179 if www:
180 print(f"=> {www} WWW")
181 print("")
182 print("Maintainer:", maintainer)
183 print("")
184 print("## Description")
185 verbatim(f"{stem} description", descr)
186 print("")
187 if readme:
188 print("## Readme")
189 verbatim(f"README for {stem}", readme)
190 foot()
193 if not path or path == '/':
194 homepage()
195 exit(0)
197 if not path.endswith("/"):
198 reply(31, f"{script_path}{path}/")
199 exit(0)
200 # drop the leading and trailing /
201 path = path.strip("/")
203 try:
204 conn = sqlite3.connect(sqlports)
206 if path == 'search':
207 searchpage()
208 elif path == 'all':
209 allcatspage()
210 else:
211 cursor = conn.execute(query_by_fullpkgpath, (path, ))
212 row = cursor.fetchone()
213 if not row:
214 bycatpage()
215 else:
216 portpage(row)
217 except SystemExit:
218 pass
219 except BaseException:
220 reply(50, "internal server error")
221 traceback.print_exc()
222 finally:
223 conn.close()