9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-12-22 11:33:29 +01:00
qp2/ocaml/Qpackage.ml

145 lines
3.4 KiB
OCaml
Raw Normal View History

2019-03-13 13:02:29 +01:00
open Qptypes
open Qputils
2019-01-25 11:39:31 +01:00
(** Variables related to the quantum package installation *)
let root =
2019-03-13 13:02:29 +01:00
match (Sys.getenv_opt "QP_ROOT") with
2019-01-25 11:39:31 +01:00
| None -> failwith "QP_ROOT environment variable is not set.
Please source the quantum_package.rc file."
| Some x -> x
2019-03-13 13:02:29 +01:00
2019-01-25 11:39:31 +01:00
let bit_kind_size = lazy (
let filename = root^"/src/bitmask/bitmasks_module.f90" in
2019-03-13 13:02:29 +01:00
if not (Sys.file_exists filename) then
2019-01-25 11:39:31 +01:00
raise (Failure ("File "^filename^" not found"));
2019-03-13 13:02:29 +01:00
let in_channel = open_in filename in
let lines = input_lines in_channel in
close_in in_channel;
2019-01-25 11:39:31 +01:00
let rec get_data = function
| [] -> raise (Failure ("bit_kind_size not found in "^filename))
| line::tail ->
2019-03-13 13:02:29 +01:00
let line =
try
String_ext.split ~on:'!' line
|> List.hd
with _ -> line
in
begin match (String_ext.rsplit2 ~on:':' line) with
| Some (_ ,buffer) ->
2020-05-25 11:31:28 +02:00
begin match (String_ext.split ~on:'=' buffer |> list_map String.trim) with
2019-03-13 13:02:29 +01:00
| ["bit_kind_size"; x] ->
int_of_string x |> Bit_kind_size.of_int
| _ -> get_data tail
end
| _ -> get_data tail
end
2019-01-25 11:39:31 +01:00
in
get_data lines )
2019-03-13 13:02:29 +01:00
2019-01-25 11:39:31 +01:00
let bit_kind = lazy (
Lazy.force bit_kind_size
|> Bit_kind_size.to_int
|> fun x -> x / 8
|> Bit_kind.of_int
)
2019-03-13 13:02:29 +01:00
2019-01-25 11:39:31 +01:00
let executables = lazy (
2019-03-13 13:02:29 +01:00
let filename = root^"/data/executables" in
let lines =
let in_channel = open_in filename in
let result = input_lines in_channel in
close_in in_channel;
result
in
lines
2020-05-25 11:31:28 +02:00
|> list_map (fun x ->
2019-03-13 13:02:29 +01:00
let e = String_ext.split ~on:' ' x
|> List.filter (fun x -> x <> "")
2019-01-25 11:39:31 +01:00
in
match e with
2019-03-13 13:02:29 +01:00
| [a;b] -> (a,String_ext.substr_replace_all ~pattern:"$QP_ROOT" ~with_:root b)
2019-01-25 11:39:31 +01:00
| _ -> ("","")
)
2019-03-13 13:02:29 +01:00
|> List.sort (fun (x,_) (y,_) ->
2019-01-25 11:39:31 +01:00
if x < y then -1
else if x > y then 1
else 0)
)
let get_ezfio_default_in_file ~directory ~data ~filename =
2019-03-13 13:02:29 +01:00
let lines =
let in_channel = open_in filename in
let result = input_lines in_channel in
close_in in_channel;
result
in
2019-01-25 11:39:31 +01:00
let rec find_dir = function
| line :: rest ->
2019-03-13 13:02:29 +01:00
if ((String.trim line) = directory) then
2019-01-25 11:39:31 +01:00
rest
else
find_dir rest
2019-03-13 13:02:29 +01:00
| [] -> raise Not_found
2019-01-25 11:39:31 +01:00
in
let rec find_data = function
| line :: rest ->
if (line = "") then
2019-03-13 13:02:29 +01:00
raise Not_found
2019-01-25 11:39:31 +01:00
else if (line.[0] <> ' ') then
2019-03-13 13:02:29 +01:00
raise Not_found
2019-01-25 11:39:31 +01:00
else
begin
2019-03-13 13:02:29 +01:00
match (String_ext.lsplit2 ~on:' ' (String.trim line)) with
2019-01-25 11:39:31 +01:00
| Some (l,r) ->
if (l = data) then
2019-03-13 13:02:29 +01:00
String.trim r
2019-01-25 11:39:31 +01:00
else
find_data rest
2019-03-13 13:02:29 +01:00
| None -> raise Not_found
2019-01-25 11:39:31 +01:00
end
2019-03-13 13:02:29 +01:00
| [] -> raise Not_found
2019-01-25 11:39:31 +01:00
in
find_dir lines
|> find_data ;
;;
let get_ezfio_default directory data =
let dirname = root^"/data/ezfio_defaults/" in
let rec aux = function
| [] ->
begin
Printf.printf "%s/%s not found\n%!" directory data;
2019-03-13 13:02:29 +01:00
raise Not_found
2019-01-25 11:39:31 +01:00
end
| filename :: tail ->
let filename =
dirname^filename
in
try
get_ezfio_default_in_file ~directory ~data ~filename
with
2019-03-13 13:02:29 +01:00
| Not_found -> aux tail
2019-01-25 11:39:31 +01:00
in
Sys.readdir dirname
|> Array.to_list
|> aux
;;
let ezfio_work ezfio_file =
let result =
Filename.concat ezfio_file "work"
in
2019-03-13 13:02:29 +01:00
if not (Sys.file_exists result) then
( Ezfio.set_file ezfio_file ; Ezfio.set_work_empty false);
2019-01-25 11:39:31 +01:00
result
;;