OCaml implementation of the Mozilla Public Suffix service

expose version information

+103 -7
+12
bin/main.ml
··· 93 93 in 94 94 Cmd.v info term 95 95 96 + let version_cmd = 97 + let doc = "Print version information about the Public Suffix List data" in 98 + let info = Cmd.info "version" ~doc in 99 + let term = 100 + Term.(const (fun (version, commit) -> 101 + Printf.printf "Version: %s\n" version; 102 + Printf.printf "Commit: %s\n" commit) 103 + $ Publicsuffix_cmd.version_term (psl ())) 104 + in 105 + Cmd.v info term 106 + 96 107 let default_cmd = 97 108 let doc = "Query the Public Suffix List" in 98 109 let sdocs = Manpage.s_common_options in ··· 105 116 registrable_section_cmd; 106 117 suffix_section_cmd; 107 118 stats_cmd; 119 + version_cmd; 108 120 ] 109 121 110 122 let () = exit (Cmd.eval default_cmd)
+42 -7
gen/gen_psl.ml
··· 126 126 let rule_count = ref 0 in 127 127 let icann_count = ref 0 in 128 128 let private_count = ref 0 in 129 + let version = ref None in 130 + let commit = ref None in 129 131 (* Helper to check if string contains substring *) 130 132 let contains_substring s sub = 131 133 try ··· 133 135 true 134 136 with Not_found -> false 135 137 in 138 + (* Helper to extract value after "KEY: " pattern *) 139 + let extract_value line prefix = 140 + let prefix_len = String.length prefix in 141 + if String.length line > prefix_len && 142 + String.sub line 0 prefix_len = prefix then 143 + Some (String.trim (String.sub line prefix_len (String.length line - prefix_len))) 144 + else 145 + None 146 + in 136 147 try 137 148 while true do 138 149 let line = input_line ic in 150 + (* Check for version and commit info *) 151 + if !version = None then 152 + version := extract_value line "// VERSION: "; 153 + if !commit = None then 154 + commit := extract_value line "// COMMIT: "; 139 155 (* Check for section markers *) 140 156 if contains_substring line "===BEGIN ICANN DOMAINS===" then 141 157 current_section := ICANN ··· 149 165 else incr private_count 150 166 ) (parse_line !current_section line) 151 167 done; 152 - (trie, !rule_count, !icann_count, !private_count) 168 + (trie, !rule_count, !icann_count, !private_count, !version, !commit) 153 169 with End_of_file -> 154 170 close_in ic; 155 - (trie, !rule_count, !icann_count, !private_count) 171 + (trie, !rule_count, !icann_count, !private_count, !version, !commit) 156 172 157 173 (** Escape a string for OCaml source code *) 158 174 let escape_string s = ··· 172 188 Buffer.contents b 173 189 174 190 (** Generate OCaml code for the trie *) 175 - let generate_code trie rule_count icann_count private_count = 191 + let generate_code trie rule_count icann_count private_count version commit = 176 192 (* Print header *) 177 193 print_string {|(* Auto-generated from public_suffix_list.dat - DO NOT EDIT *) 178 194 (* This file contains the parsed Public Suffix List as OCaml data structures *) ··· 191 207 } 192 208 193 209 |}; 194 - Printf.printf "(* Statistics: %d total rules (%d ICANN, %d private) *)\n\n" 210 + Printf.printf "(* Statistics: %d total rules (%d ICANN, %d private) *)\n" 195 211 rule_count icann_count private_count; 212 + Printf.printf "(* Version: %s *)\n" version; 213 + Printf.printf "(* Commit: %s *)\n" commit; 214 + print_string "\n"; 196 215 197 216 (* Generate the trie as nested let bindings using a depth-first traversal *) 198 217 let node_counter = ref 0 in ··· 280 299 |}; 281 300 Printf.printf "let rule_count = %d\n\n" rule_count; 282 301 Printf.printf "let icann_rule_count = %d\n\n" icann_count; 283 - Printf.printf "let private_rule_count = %d\n" private_count 302 + Printf.printf "let private_rule_count = %d\n\n" private_count; 303 + (* Generate version and commit values *) 304 + Printf.printf "let version = %S\n\n" version; 305 + Printf.printf "let commit = %S\n" commit 284 306 285 307 let () = 286 308 if Array.length Sys.argv < 2 then begin ··· 288 310 exit 1 289 311 end; 290 312 let filename = Sys.argv.(1) in 291 - let trie, rule_count, icann_count, private_count = parse_file filename in 292 - generate_code trie rule_count icann_count private_count 313 + let trie, rule_count, icann_count, private_count, version, commit = parse_file filename in 314 + (* Ensure version and commit are present *) 315 + let version = match version with 316 + | Some v -> v 317 + | None -> 318 + Printf.eprintf "ERROR: VERSION not found in %s\n" filename; 319 + exit 1 320 + in 321 + let commit = match commit with 322 + | Some c -> c 323 + | None -> 324 + Printf.eprintf "ERROR: COMMIT not found in %s\n" filename; 325 + exit 1 326 + in 327 + generate_code trie rule_count icann_count private_count version commit
+6
lib/cmd/publicsuffix_cmd.ml
··· 44 44 Publicsuffix.icann_rule_count psl, 45 45 Publicsuffix.private_rule_count psl)) 46 46 $ const ()) 47 + 48 + let version_term psl = 49 + Term.(const (fun () -> 50 + (Publicsuffix.version psl, 51 + Publicsuffix.commit psl)) 52 + $ const ())
+4
lib/cmd/publicsuffix_cmd.mli
··· 44 44 val stats_term : Publicsuffix.t -> (int * int * int) Cmdliner.Term.t 45 45 (** Term that returns statistics about the Public Suffix List as a tuple of 46 46 (total_rules, icann_rules, private_rules). *) 47 + 48 + val version_term : Publicsuffix.t -> (string * string) Cmdliner.Term.t 49 + (** Term that returns version information about the Public Suffix List as a tuple of 50 + (version, commit). *)
+3
lib/publicsuffix.ml
··· 244 244 let rule_count _t = Publicsuffix_data.rule_count 245 245 let icann_rule_count _t = Publicsuffix_data.icann_rule_count 246 246 let private_rule_count _t = Publicsuffix_data.private_rule_count 247 + 248 + let version _t = Publicsuffix_data.version 249 + let commit _t = Publicsuffix_data.commit
+16
lib/publicsuffix.mli
··· 263 263 264 264 (** Number of private section rules *) 265 265 val private_rule_count : t -> int 266 + 267 + (** {1 Version Information} *) 268 + 269 + (** Version string from the embedded PSL data. 270 + 271 + Returns the version identifier from the Public Suffix List source file, 272 + typically in the format ["YYYY-MM-DD_HH-MM-SS_UTC"]. 273 + *) 274 + val version : t -> string 275 + 276 + (** Commit hash from the embedded PSL data. 277 + 278 + Returns the git commit hash from the Public Suffix List repository 279 + corresponding to the version of the data embedded in this library. 280 + *) 281 + val commit : t -> string
+20
lib/publicsuffix_data.mli
··· 159 159 allow subdomain registration. 160 160 *) 161 161 val private_rule_count : int 162 + 163 + (** {1 Version Information} 164 + 165 + These values reflect the version and commit information from the PSL 166 + data at the time this module was generated. 167 + *) 168 + 169 + (** Version string from the PSL data file. 170 + 171 + This is the version identifier from the Public Suffix List source file, 172 + typically in the format "YYYY-MM-DD_HH-MM-SS_UTC". 173 + *) 174 + val version : string 175 + 176 + (** Commit hash from the PSL data file. 177 + 178 + This is the git commit hash from the Public Suffix List repository 179 + corresponding to the version of the data embedded in this library. 180 + *) 181 + val commit : string