diff --git a/tools/gen_licenses.ml b/tools/gen_licenses.ml index 7344e78..18ffedd 100644 --- a/tools/gen_licenses.ml +++ b/tools/gen_licenses.ml @@ -21,6 +21,7 @@ let rec map_result f = function Ok (y :: ys) let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args) + let lines cmd = let* l = OS.Cmd.(run_out cmd |> to_lines) in Ok (List.map String.trim l) @@ -34,14 +35,7 @@ let line cmd = (* Windows writes paths with backslashes (doubled in the .changes files, where they are escaped), and drive letters in either case. *) let slashes = Re.compile (Re.rep1 (Re.set "/\\")) - -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 normalize s = String.lowercase_ascii (Re.replace_string slashes ~by:"/" s) let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)" (* 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* 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 let top = List.hd (String.split_on_char '.' lib) in let meta = @@ -98,7 +93,9 @@ let package_info ~switch pkg = let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in let prefix = pkg ^ "." in 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 | None -> msg "no opam metadata for %s" pkg | Some e -> @@ -125,7 +122,8 @@ let license_file ~switch pkg = (List.find_opt (fun e -> 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) let run file = @@ -139,36 +137,37 @@ let run file = map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs) in let packages = List.sort_uniq compare owners in - if packages = [] then msg "no library found in %s" file else - let* infos = - map_result - (fun p -> - let* version, license = package_info ~switch p in - let* file = license_file ~switch p in - Ok (p, version, license, file)) - packages - in - print_string - "THIRD-PARTY LICENSES\n\n\ - totpin links the following libraries. Their licence texts follow.\n"; - List.iter (fun (p, v, _, _) -> Printf.printf " - %s %s\n" p v) infos; - let rule = String.make 64 '=' in - let* () = - map_result - (fun (p, v, license, file) -> - match file with - | None -> - Printf.eprintf "warning: no licence file for %s\n" p; - Ok () - | Some f -> - let* text = OS.File.read f in - Printf.printf "\n%s\n%s %s (%s)\n%s\n\n%s" rule p v license rule - text; - Ok ()) - infos - |> Result.map ignore - in - Ok () + if packages = [] then msg "no library found in %s" file + else + let* infos = + map_result + (fun p -> + let* version, license = package_info ~switch p in + let* file = license_file ~switch p in + Ok (p, version, license, file)) + packages + in + print_string + "THIRD-PARTY LICENSES\n\n\ + totpin links the following libraries. Their licence texts follow.\n"; + List.iter (fun (p, v, _, _) -> Printf.printf " - %s %s\n" p v) infos; + let rule = String.make 64 '=' in + let* () = + map_result + (fun (p, v, license, file) -> + match file with + | None -> + Printf.eprintf "warning: no licence file for %s\n" p; + Ok () + | Some f -> + let* text = OS.File.read f in + Printf.printf "\n%s\n%s %s (%s)\n%s\n\n%s" rule p v license rule + text; + Ok ()) + infos + |> Result.map ignore + in + Ok () let () = match Sys.argv with