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