gen_licenses: read the switch state with the opam libraries
This commit is contained in:
1 parent
31d500a955
commit
d0487ddae9
2 files changed
+54
-51
No files matched your search
+53
-50
@@ -4,9 +4,10 @@
|
||||
|
||||
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>/]. *)
|
||||
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
|
||||
|
||||
@@ -61,59 +62,62 @@ 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))
|
||||
(* 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
|
||||
|
||||
let owner ~stdlib ~changes lib =
|
||||
(* 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
|
||||
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
|
||||
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 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 -> String.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 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 Re.exec_opt license_re opam with
|
||||
| Some g -> Re.Group.get g 1
|
||||
| None -> "unknown"
|
||||
match OpamFile.OPAM.license (OpamSwitchState.opam st nv) with
|
||||
| [] -> "unknown"
|
||||
| l -> String.concat ", " l
|
||||
in
|
||||
Ok (version, license)
|
||||
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 ~switch pkg =
|
||||
let dir = Fpath.(switch / "doc" / pkg) in
|
||||
let license_file dir =
|
||||
let* exists = OS.Dir.exists dir in
|
||||
if not exists then Ok None
|
||||
else
|
||||
@@ -127,14 +131,13 @@ let license_file ~switch pkg =
|
||||
entries)
|
||||
|
||||
let run file =
|
||||
with_switch_state @@ fun root st ->
|
||||
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 owners = load_owners root st in
|
||||
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
|
||||
let* owners =
|
||||
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
|
||||
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
|
||||
@@ -142,8 +145,8 @@ let run file =
|
||||
let* infos =
|
||||
map_result
|
||||
(fun p ->
|
||||
let* version, license = package_info ~switch p in
|
||||
let* file = license_file ~switch p in
|
||||
let* version, license, doc = package_info root st p in
|
||||
let* file = license_file doc in
|
||||
Ok (p, version, license, file))
|
||||
packages
|
||||
in
|
||||
|
||||
Reference in new issue
Block a user