Files
totpin/tools/gen_licenses.ml
T

186 lines
6.1 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, declared licence and
documentation directory from the state of the switch, read with the opam
libraries. The licence texts are the files that opam installed in that
directory. *)
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 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)
(* The state of the current switch, loaded the way the [opam] command does. *)
let with_switch_state f =
OpamSystem.init ();
OpamFormatConfig.init ();
OpamRepositoryConfig.init ();
let root = OpamStateConfig.opamroot () in
OpamStateConfig.init ~root_dir:root ();
ignore (OpamStateConfig.load_defaults ~lock_kind:`Lock_none root);
OpamGlobalState.with_ `Lock_none @@ fun gt ->
OpamSwitchState.with_ `Lock_none gt @@ fun st -> f root st
(* The opam package that installed each file of the switch. *)
let load_owners root (st : _ OpamStateTypes.switch_state) =
let owners = Hashtbl.create 4096 in
OpamPackage.Set.iter
(fun nv ->
let name = OpamPackage.name nv in
let changes =
OpamFile.Changes.safe_read (OpamPath.Switch.changes root st.switch name)
in
OpamStd.String.Map.iter
(fun file _ ->
Hashtbl.replace owners (normalize file)
(OpamPackage.Name.to_string name))
changes)
st.installed;
owners
let owner ~stdlib ~owners lib =
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
if String.starts_with ~prefix:(normalize stdlib) (normalize dir) then
Ok "ocaml"
else
let top = List.hd (String.split_on_char '.' lib) in
match Hashtbl.find_opt owners (Printf.sprintf "lib/%s/meta" top) with
| Some pkg -> Ok pkg
| None -> msg "no opam package found for the library %s" lib
let package_info root (st : _ OpamStateTypes.switch_state) pkg =
let name = OpamPackage.Name.of_string pkg in
match OpamSwitchState.find_installed_package_by_name st name with
| exception Not_found -> msg "package %s is not installed" pkg
| nv ->
let version = OpamPackage.Version.to_string (OpamPackage.version nv) in
let license =
match OpamFile.OPAM.license (OpamSwitchState.opam st nv) with
| [] -> "unknown"
| l -> String.concat ", " l
in
let doc =
OpamPath.Switch.doc root st.switch st.switch_config name
|> OpamFilename.Dir.to_string |> Fpath.v
in
Ok (version, license, doc)
let license_file dir =
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
String.starts_with ~prefix:"LICENSE" n
|| String.starts_with ~prefix:"COPYING" n)
entries)
let run file =
with_switch_state @@ fun root st ->
let* roots = roots file in
let* stdlib = line (ocamlfind [ "printconf"; "stdlib" ]) in
let owners = load_owners root st in
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
let* owners =
map_result (owner ~stdlib ~owners) (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, doc = package_info root st p in
let* file = license_file doc 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