gen_licenses: use String.starts_with from the Stdlib

This commit is contained in:
Matthieu Dubuget committed 2026-11-22 19:22:45 +01:00
1 parent b9e82711ec
commit 31d500a955
1 file changed
+40 -41
+40 -41
View File
@@ -21,6 +21,7 @@ let rec map_result f = function
Ok (y :: ys) Ok (y :: ys)
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args) let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
let lines cmd = let lines cmd =
let* l = OS.Cmd.(run_out cmd |> to_lines) in let* l = OS.Cmd.(run_out cmd |> to_lines) in
Ok (List.map String.trim l) Ok (List.map String.trim l)
@@ -34,14 +35,7 @@ let line cmd =
(* Windows writes paths with backslashes (doubled in the .changes files, where (* Windows writes paths with backslashes (doubled in the .changes files, where
they are escaped), and drive letters in either case. *) they are escaped), and drive letters in either case. *)
let slashes = Re.compile (Re.rep1 (Re.set "/\\")) let slashes = Re.compile (Re.rep1 (Re.set "/\\"))
let normalize s = String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
let normalize s =
String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
let starts_with ~prefix s =
String.length s >= String.length prefix
&& String.sub s 0 (String.length prefix) = prefix
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)" let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
(* The libraries of the first [(libraries ...)] field of a dune file, minus (* The libraries of the first [(libraries ...)] field of a dune file, minus
@@ -77,7 +71,8 @@ let load_changes switch =
let owner ~stdlib ~changes lib = let owner ~stdlib ~changes lib =
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
if starts_with ~prefix:(normalize stdlib) (normalize dir) then Ok "ocaml" if String.starts_with ~prefix:(normalize stdlib) (normalize dir) then
Ok "ocaml"
else else
let top = List.hd (String.split_on_char '.' lib) in let top = List.hd (String.split_on_char '.' lib) in
let meta = let meta =
@@ -98,7 +93,9 @@ let package_info ~switch pkg =
let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in
let prefix = pkg ^ "." in let prefix = pkg ^ "." in
match match
List.find_opt (fun e -> starts_with ~prefix (Fpath.basename e)) entries List.find_opt
(fun e -> String.starts_with ~prefix (Fpath.basename e))
entries
with with
| None -> msg "no opam metadata for %s" pkg | None -> msg "no opam metadata for %s" pkg
| Some e -> | Some e ->
@@ -125,7 +122,8 @@ let license_file ~switch pkg =
(List.find_opt (List.find_opt
(fun e -> (fun e ->
let n = String.uppercase_ascii (Fpath.basename e) in let n = String.uppercase_ascii (Fpath.basename e) in
starts_with ~prefix:"LICENSE" n || starts_with ~prefix:"COPYING" n) String.starts_with ~prefix:"LICENSE" n
|| String.starts_with ~prefix:"COPYING" n)
entries) entries)
let run file = let run file =
@@ -139,36 +137,37 @@ let run file =
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs) map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
in in
let packages = List.sort_uniq compare owners in let packages = List.sort_uniq compare owners in
if packages = [] then msg "no library found in %s" file else if packages = [] then msg "no library found in %s" file
let* infos = else
map_result let* infos =
(fun p -> map_result
let* version, license = package_info ~switch p in (fun p ->
let* file = license_file ~switch p in let* version, license = package_info ~switch p in
Ok (p, version, license, file)) let* file = license_file ~switch p in
packages Ok (p, version, license, file))
in packages
print_string in
"THIRD-PARTY LICENSES\n\n\ print_string
totpin links the following libraries. Their licence texts follow.\n"; "THIRD-PARTY LICENSES\n\n\
List.iter (fun (p, v, _, _) -> Printf.printf " - %s %s\n" p v) infos; totpin links the following libraries. Their licence texts follow.\n";
let rule = String.make 64 '=' in List.iter (fun (p, v, _, _) -> Printf.printf " - %s %s\n" p v) infos;
let* () = let rule = String.make 64 '=' in
map_result let* () =
(fun (p, v, license, file) -> map_result
match file with (fun (p, v, license, file) ->
| None -> match file with
Printf.eprintf "warning: no licence file for %s\n" p; | None ->
Ok () Printf.eprintf "warning: no licence file for %s\n" p;
| Some f -> Ok ()
let* text = OS.File.read f in | Some f ->
Printf.printf "\n%s\n%s %s (%s)\n%s\n\n%s" rule p v license rule let* text = OS.File.read f in
text; Printf.printf "\n%s\n%s %s (%s)\n%s\n\n%s" rule p v license rule
Ok ()) text;
infos Ok ())
|> Result.map ignore infos
in |> Result.map ignore
Ok () in
Ok ()
let () = let () =
match Sys.argv with match Sys.argv with