184 lines
5.9 KiB
OCaml
184 lines
5.9 KiB
OCaml
(* Prints the licence texts of the opam packages that provide the libraries
|
|
listed in the [(libraries ...)] field of the dune file given as argument,
|
|
and of their dependencies.
|
|
|
|
For each library, [ocamlfind] gives the transitive closure and the directory
|
|
where it is installed. The opam package that owns a library is found from
|
|
the [.changes] files of the switch, and its version and declared licence
|
|
from the [opam] file that opam keeps for it. The licence texts are the files
|
|
that opam installed in [doc/<package>/]. *)
|
|
|
|
open Bos
|
|
|
|
let ( let* ) = Result.bind
|
|
let msg fmt = Printf.ksprintf (fun s -> Error (`Msg s)) fmt
|
|
|
|
let rec map_result f = function
|
|
| [] -> Ok []
|
|
| x :: xs ->
|
|
let* y = f x in
|
|
let* ys = map_result f xs in
|
|
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)
|
|
|
|
let line cmd =
|
|
let* l = lines cmd in
|
|
match l with
|
|
| x :: _ -> Ok x
|
|
| [] -> msg "no output from: %s" (Cmd.to_string 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 libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
|
|
|
|
(* The libraries of the first [(libraries ...)] field of a dune file, minus
|
|
those that findlib does not know (the libraries of the project). *)
|
|
let roots file =
|
|
let* s = OS.File.read (Fpath.v file) in
|
|
match Re.exec_opt libraries_re s with
|
|
| None -> msg "no (libraries ...) field in %s" file
|
|
| Some g ->
|
|
let names =
|
|
List.filter (( <> ) "")
|
|
(Re.split (Re.Perl.compile_pat "\\s+") (Re.Group.get g 1))
|
|
in
|
|
Ok
|
|
(List.filter
|
|
(fun lib ->
|
|
Result.is_ok
|
|
OS.Cmd.(
|
|
run_out ~err:err_null (ocamlfind [ "query"; lib ]) |> to_lines))
|
|
names)
|
|
|
|
let sorted_entries dir =
|
|
let* l = OS.Dir.contents ~dotfiles:true dir in
|
|
Ok (List.sort Fpath.compare l)
|
|
|
|
let load_changes switch =
|
|
let* files = sorted_entries Fpath.(switch / ".opam-switch" / "install") in
|
|
files
|
|
|> List.filter (Fpath.has_ext ".changes")
|
|
|> map_result (fun f ->
|
|
let* content = OS.File.read f in
|
|
Ok (Fpath.(basename (rem_ext f)), normalize content))
|
|
|
|
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"
|
|
else
|
|
let top = List.hd (String.split_on_char '.' lib) in
|
|
let meta =
|
|
Re.compile (Re.str (normalize (Printf.sprintf "lib/%s/META" top)))
|
|
in
|
|
match
|
|
List.find_map
|
|
(fun (pkg, content) -> if Re.execp meta content then Some pkg else None)
|
|
changes
|
|
with
|
|
| Some pkg -> Ok pkg
|
|
| None -> msg "no opam package found for the library %s" lib
|
|
|
|
let license_re = Re.Perl.compile_pat ~opts:[ `Multiline ] "^license:[ \t]*(.*)$"
|
|
|
|
(* The version is the end of the name of the directory [<pkg>.<version>]. *)
|
|
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
|
|
with
|
|
| None -> msg "no opam metadata for %s" pkg
|
|
| Some e ->
|
|
let name = Fpath.basename e in
|
|
let version =
|
|
String.sub name (String.length prefix)
|
|
(String.length name - String.length prefix)
|
|
in
|
|
let* opam = OS.File.read Fpath.(e / "opam") in
|
|
let license =
|
|
match Re.exec_opt license_re opam with
|
|
| Some g -> Re.Group.get g 1
|
|
| None -> "unknown"
|
|
in
|
|
Ok (version, license)
|
|
|
|
let license_file ~switch pkg =
|
|
let dir = Fpath.(switch / "doc" / pkg) in
|
|
let* exists = OS.Dir.exists dir in
|
|
if not exists then Ok None
|
|
else
|
|
let* entries = sorted_entries dir in
|
|
Ok
|
|
(List.find_opt
|
|
(fun e ->
|
|
let n = String.uppercase_ascii (Fpath.basename e) in
|
|
starts_with ~prefix:"LICENSE" n || starts_with ~prefix:"COPYING" n)
|
|
entries)
|
|
|
|
let run file =
|
|
let* roots = roots file in
|
|
let* destdir = line (ocamlfind [ "printconf"; "destdir" ]) in
|
|
let switch = Fpath.(parent (v destdir)) in
|
|
let* stdlib = line (ocamlfind [ "printconf"; "stdlib" ]) in
|
|
let* changes = load_changes switch in
|
|
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
|
|
let* owners =
|
|
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 ()
|
|
|
|
let () =
|
|
match Sys.argv with
|
|
| [| _; file |] -> (
|
|
match run file with
|
|
| Ok () -> ()
|
|
| Error (`Msg m) ->
|
|
prerr_endline ("gen_licenses: " ^ m);
|
|
exit 1)
|
|
| _ ->
|
|
prerr_endline "usage: gen_licenses LIBRARIES-FILE";
|
|
exit 2
|