-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathopamFile.ml
2497 lines (2182 loc) · 80.9 KB
/
opamFile.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* Copyright 2012-2015 OCamlPro *)
(* Copyright 2012 INRIA *)
(* *)
(* All rights reserved.This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 3.0 with linking *)
(* exception. *)
(* *)
(* OPAM is distributed in the hope that it will be useful, but WITHOUT *)
(* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *)
(* or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public *)
(* License for more details. *)
(* *)
(**************************************************************************)
(** This module contains the handlers for reading and writing all of OPAM files,
and defines their internal types (records for most of them).
We handle three types of files:
- raw text files, without lexing
- "table" files, i.e. lexing is just cutting into lines and words, returning
a string list list. These are mostly used internally
- files using the "opam syntax" and lexer, parsed using OpamFormat.Pp.V
*)
open OpamTypes
open OpamTypesBase
open OpamStd.Op
module Pp = OpamFormat.Pp
open Pp.Op
module type IO_FILE = sig
type t
val empty: t
val write: filename -> t -> unit
val read : filename -> t
val safe_read: filename -> t
val read_from_channel: ?filename:filename -> in_channel -> t
val read_from_string: ?filename:filename -> string -> t
val write_to_channel: ?filename:filename -> out_channel -> t -> unit
val write_to_string: ?filename:filename -> t -> string
end
module type IO_Arg = sig
val internal : string
type t
val empty : t
val of_channel : filename -> in_channel -> t
val to_channel : filename -> out_channel -> t -> unit
val of_string : filename -> string -> t
val to_string : filename -> t -> string
end
module Stats = struct
let read_files = ref []
let write_files = ref []
let print () =
let aux kind = function
| [] -> ()
| l ->
OpamConsole.msg "%d files %s:\n %s\n%!"
(List.length !read_files) kind (String.concat "\n " l)
in
aux "read" !read_files;
aux "write" !write_files
end
module MakeIO (F : IO_Arg) = struct
let log ?level fmt =
OpamConsole.log (Printf.sprintf "FILE(%s)" F.internal) ?level fmt
let slog = OpamConsole.slog
let write f v =
let filename = OpamFilename.to_string f in
let chrono = OpamConsole.timer () in
let oc =
OpamFilename.(mkdir (dirname f));
try open_out_bin filename
with Sys_error _ -> raise (OpamSystem.File_not_found filename)
in
try
F.to_channel f oc v;
close_out oc;
Stats.write_files := filename :: !Stats.write_files;
log "Wrote %s in %.3fs" filename (chrono ())
with e -> close_out oc; raise e
let read f =
let filename = OpamFilename.prettify f in
Stats.read_files := filename :: !Stats.read_files;
let chrono = OpamConsole.timer () in
try
let ic = OpamFilename.open_in f in
try
let r = F.of_channel f ic in
close_in ic;
log ~level:3 "Read %s in %.3fs" filename (chrono ());
r
with e -> close_in ic; raise e
with
| OpamSystem.File_not_found s ->
OpamSystem.internal_error "File %s does not exist" s
| Lexer_error _ | Parsing.Parse_error as e ->
if OpamFormatConfig.(!r.strict) then
OpamConsole.error_and_exit "Strict mode: aborting"
else raise e (* Message already printed *)
| e ->
OpamStd.Exn.fatal e;
OpamConsole.error "%s" (OpamFormat.string_of_bad_format ~file:f e);
if OpamFormatConfig.(!r.strict) then OpamStd.Sys.exit 66
else raise e
let safe_read f =
if OpamFilename.exists f then
try read f with OpamFormat.Bad_format _ ->
OpamConsole.msg "[skipped]\n";
F.empty
else (
log ~level:2 "Cannot find %a" (slog OpamFilename.to_string) f;
F.empty
)
let dummy_file = OpamFilename.raw "<none>"
let read_from_f f input =
try f input with
| OpamFormat.Bad_format _ as e ->
OpamConsole.error "%s" (OpamFormat.string_of_bad_format e);
if OpamFormatConfig.(!r.strict) then
OpamConsole.error_and_exit "Strict mode: aborting"
else raise e
let read_from_channel ?(filename=dummy_file) ic =
read_from_f (F.of_channel filename) ic
let read_from_string ?(filename=dummy_file) str =
read_from_f (F.of_string filename) str
let write_to_channel ?(filename=dummy_file) oc t =
F.to_channel filename oc t
let write_to_string ?(filename=dummy_file) t =
F.to_string filename t
end
(** I - Raw text files (no parsing) *)
(** Compiler and package description files
(<repo>/packages/.../descr, <repo>/compilers/.../<v>.descr):
one-line title and content *)
module DescrIO = struct
let internal = "descr"
type t = string * string
let empty = "", ""
let synopsis = fst
let body = snd
let full (x,y) =
match y with
| "" -> x ^ "\n"
| y -> String.concat "" [x; "\n\n"; y; "\n"]
let of_channel _ ic =
let x =
try OpamStd.String.strip (input_line ic)
with End_of_file | Sys_error _ -> "" in
let y =
try OpamStd.String.strip (OpamSystem.string_of_channel ic)
with End_of_file | Sys_error _ -> ""
in
x, y
let to_channel _ oc (x,y) =
output_string oc x;
output_char oc '\n';
if y <> "" then
(output_char oc '\n';
output_string oc y;
output_char oc '\n')
let create str =
let head, tail =
match OpamStd.String.cut_at str '\n' with
| None -> str, ""
| Some (h,t) -> h, t in
OpamStd.String.strip head, OpamStd.String.strip tail
let of_string _ = create
let to_string _ = full
end
module Descr = struct
include DescrIO
include MakeIO(DescrIO)
end
module Comp_descr = Descr
(** Raw file interface used for variable expansions ( *.in ) *)
module SubstIO = struct
let internal = "subst"
type t = string
let empty = ""
let of_channel _ ic =
OpamSystem.string_of_channel ic
let to_channel _ oc t =
output_string oc t
let of_string _ str = str
let to_string _ t = t
end
module Subst = struct
include SubstIO
include MakeIO(SubstIO)
end
(** II - Base word list list parser and associated file types *)
module LinesBase = struct
(* Lines of space separated words *)
type t = string list list
let empty = []
let internal = "lines"
let find_escapes s len =
let rec aux acc i =
if i < 0 then acc else
let acc =
match s.[i] with
| '\\' | ' ' | '\t' | '\n' ->
let esc,count = acc in
i::esc, count + 1
| _ -> acc in
aux acc (i-1) in
aux ([],0) (len - 1)
let escape_spaces str =
let len = String.length str in
match find_escapes str len with
| [], _ -> str
| escapes, n ->
let buf = Bytes.create (len + n) in
let rec aux i = function
| ofs1::(ofs2::_ as r) ->
Bytes.blit_string str ofs1 buf (ofs1+i) (ofs2-ofs1);
Bytes.set buf (ofs2+i) '\\';
aux (i+1) r
| [ofs] ->
Bytes.blit_string str ofs buf (ofs+i) (len-ofs);
buf
| [] -> assert false
in
Bytes.to_string (aux 0 (0::escapes))
let of_channel (_:filename) ic =
OpamLineLexer.main (Lexing.from_channel ic)
let to_channel (_:filename) oc t =
List.iter (function
| [] -> ()
| w::r ->
output_string oc (escape_spaces w);
List.iter (fun w ->
output_char oc '\t';
output_string oc (escape_spaces w))
r;
output_char oc '\n')
t
let of_string (_:filename) str =
OpamLineLexer.main (Lexing.from_string str)
let to_string (_:filename) (lines: t) =
let buf = Buffer.create 1024 in
List.iter (fun l ->
(match l with
| [] -> ()
| w::r ->
Buffer.add_string buf (escape_spaces w);
List.iter (fun w ->
Buffer.add_char buf '\t';
Buffer.add_string buf (escape_spaces w))
r);
Buffer.add_string buf "\n"
) lines;
Buffer.contents buf
let file_none = OpamFilename.of_string "<none>"
let pp_string =
Pp.pp
(fun ~pos:_ s -> OpamLineLexer.main (Lexing.from_string s))
(fun lines -> to_string file_none lines)
let pp_channel ic oc =
Pp.pp
(fun ~pos:_ () -> of_channel file_none ic)
(to_channel file_none oc)
end
module Lines = struct
include LinesBase
include MakeIO(LinesBase)
end
module type LineFileArg = sig
val internal: string
type t
val empty: t
val pp: (string list list, t) Pp.t
end
module LineFile (X: LineFileArg) = struct
module IO = struct
include X
let to_channel _ oc t = Pp.print (Lines.pp_channel stdin oc -| pp) t
let to_string _ t = Pp.print (Lines.pp_string -| pp) t
let of_channel filename ic =
Pp.parse (Lines.pp_channel ic stdout -| pp) ~pos:(pos_file filename) ()
let of_string filename str =
Pp.parse (Lines.pp_string -| pp) ~pos:(filename,0,0) str
end
include IO
include MakeIO(IO)
end
(** (1) Internal usage only *)
(** Compiler aliases definitions (aliases): table
<name> <compiler> *)
module Aliases = LineFile(struct
let internal = "aliases"
type t = compiler switch_map
let empty = OpamSwitch.Map.empty
let pp =
OpamSwitch.Map.(Pp.lines_map ~empty ~add ~fold) @@
Pp.of_module "switch-name" (module OpamSwitch: Pp.STR with type t = OpamSwitch.t) ^+
(Pp.last -| Pp.of_module "compiler" (module OpamCompiler: Pp.STR with type t = OpamCompiler.t))
end)
(** Indices of items and their associated source repository: table
<fullname> <repo-name> <dir-prefix> *)
module Repo_index (A : OpamStd.ABSTRACT) = LineFile(struct
let internal = "repo-index"
type t = (repository_name * string option) A.Map.t
let empty = A.Map.empty
let pp =
Pp.lines_map ~empty ~add:A.Map.safe_add ~fold:A.Map.fold @@
Pp.of_module "name" (module A: Pp.STR with type t = A.t) ^+
Pp.of_module "repository" (module OpamRepositoryName: Pp.STR with type t = OpamRepositoryName.t) ^+
Pp.opt Pp.last
end)
module Package_index = Repo_index(OpamPackage)
module Compiler_index = Repo_index(OpamCompiler)
(** List of packages (<switch>/installed, <switch>/installed-roots,
<switch>/reinstall): table
<package> <version> *)
module PkgList = LineFile (struct
let internal = "package-version-list"
type t = package_set
let empty = OpamPackage.Set.empty
let pp =
OpamPackage.Set.(Pp.lines_set ~empty ~add ~fold) @@
(Pp.of_module "pkg-name" (module OpamPackage.Name: Pp.STR with type t = OpamPackage.Name.t) ^+
Pp.last -| Pp.of_module "pkg-version" (module OpamPackage.Version: Pp.STR with type t = OpamPackage.Version.t))
-| Pp.pp
(fun ~pos:_ (n,v) -> OpamPackage.create n v)
(fun nv -> OpamPackage.name nv, OpamPackage.version nv)
end)
(** Lists of pinned packages (<switch>/pinned): table
<name> <pin-kind> <target> *)
let pp_pin =
Pp.pp
~name:"?pin-kind pin-target"
(fun ~pos -> function
| [x] -> pin_option_of_string x
| [k;x] -> pin_option_of_string ~kind:(pin_kind_of_string k) x
| _ -> OpamFormat.bad_format ~pos "Invalid number of fields")
(fun x -> [string_of_pin_kind (kind_of_pin_option x);
string_of_pin_option x])
module Pinned_legacy = LineFile(struct
let internal = "pinned"
type t = pin_option OpamPackage.Name.Map.t
let empty = OpamPackage.Name.Map.empty
let pp =
OpamPackage.Name.Map.(Pp.lines_map ~empty ~add:safe_add ~fold) @@
Pp.of_module "pkg-name" (module OpamPackage.Name: Pp.STR with type t = OpamPackage.Name.t) ^+
pp_pin
end)
(** Cached environment updates (<switch>/environment) *)
module Environment = LineFile(struct
let internal = "environment"
type t = env_update list
let empty = []
let pp =
Pp.lines_set ~empty:[] ~add:OpamStd.List.cons ~fold:List.fold_right @@
Pp.identity ^+
Pp.of_pair "env_update_op"
(env_update_op_of_string, string_of_env_update_op) ^+
Pp.identity ^+
Pp.opt Pp.singleton
let pp =
pp -|
Pp.map_list
(Pp.pp
(fun ~pos:_ (a, (b, (c, d))) -> (a, b, c, d))
(fun (a, b, c, d) -> (a, (b, (c, d)))))
end)
(** (2) Part of the public repository format *)
(** repository index files ("urls.txt"): table
<filename> <md5> <perms> *)
module File_attributes = LineFile(struct
let internal = "file_attributes"
type t = file_attribute_set
let empty = OpamFilename.Attribute.Set.empty
let pp =
OpamFilename.Attribute.Set.(Pp.lines_set ~empty ~add ~fold) @@
(Pp.of_module "file" (module OpamFilename.Base: Pp.STR with type t = OpamFilename.Base.t) ^+
Pp.check ~name:"md5" OpamFilename.valid_digest ^+
Pp.opt (Pp.last -| Pp.of_pair "perm" (int_of_string, string_of_int))
) -|
Pp.pp
(fun ~pos:_ (base,(md5,perm)) ->
OpamFilename.Attribute.create base md5 perm)
(fun att -> OpamFilename.Attribute.(base att, (md5 att, perm att)))
end)
(** (3) Available in interface *)
(** Switch export/import format: table
<name> <version> <installed-state> [pinning-kind] [pinning-url] *)
module StateTable = struct
let internal = "export"
module M = OpamPackage.Name.Map
type t = {
installed: package_set;
installed_roots: package_set;
compiler: package_set;
pinned: pin_option M.t;
}
let empty = {
installed = OpamPackage.Set.empty;
installed_roots = OpamPackage.Set.empty;
compiler = OpamPackage.Set.empty;
pinned = M.empty;
}
let pp_state =
Pp.pp ~name:"pkg-state"
(fun ~pos:_ -> function
| "compiler" -> `Compiler
| "root" -> `Root
| "noroot" | "installed" -> `Installed
| "uninstalled" -> `Uninstalled
| "uninstalled-compiler" -> `Uninstalled_compiler
| _ -> Pp.unexpected ())
(function
| `Compiler -> "compiler"
| `Root -> "root"
| `Installed -> "installed"
| `Uninstalled -> "uninstalled"
| `Uninstalled_compiler -> "uninstalled-compiler")
let pp_lines =
M.(Pp.lines_map ~empty ~add:safe_add ~fold) @@
Pp.of_module "pkg-name" (module OpamPackage.Name: Pp.STR with type t = OpamPackage.Name.t) ^+
Pp.of_module "pkg-version" (module OpamPackage.Version: Pp.STR with type t = OpamPackage.Version.t) ^+
(Pp.opt (pp_state ^+ Pp.opt pp_pin) -| Pp.default (`Root, None))
(* Convert from one name-map to type t *)
let pp =
pp_lines -| Pp.pp
(fun ~pos:_ map ->
M.fold
(fun name (version,(state,pin)) t ->
let nv = OpamPackage.create name version in
{
installed = (match state with
| `Installed | `Root | `Compiler ->
OpamPackage.Set.add nv t.installed
| `Uninstalled | `Uninstalled_compiler ->
t.installed);
installed_roots = (match state with
| `Root | `Compiler ->
OpamPackage.Set.add nv t.installed_roots
| `Installed | `Uninstalled | `Uninstalled_compiler ->
t.installed_roots);
compiler = (match state with
| `Compiler | `Uninstalled_compiler ->
OpamPackage.Set.add nv t.compiler
| `Root | `Installed | `Uninstalled ->
t.compiler);
pinned = (match pin with
| Some pin -> M.add name pin t.pinned
| None -> t.pinned);
})
map
empty)
(fun t ->
M.empty |>
OpamPackage.Set.fold (fun nv ->
M.add (OpamPackage.name nv)
(OpamPackage.version nv, (`Installed, None)))
t.installed |>
OpamPackage.Set.fold (fun nv ->
M.add (OpamPackage.name nv)
(OpamPackage.version nv, (`Root, None)))
t.installed_roots |>
OpamPackage.Set.fold (fun nv acc ->
let name = OpamPackage.name nv in
try
let (v, _) = M.find name acc in
M.add name (v, (`Compiler, None)) acc
with Not_found ->
M.add name
(OpamPackage.version nv, (`Uninstalled_compiler, None))
acc)
t.compiler |>
M.fold (fun name pin map ->
try
let v, (state, _) = M.find name map in
M.add name (v, (state, Some pin)) map
with Not_found ->
let v = OpamPackage.Version.of_string "--" in
M.add name (v, (`Uninstalled, Some pin)) map)
t.pinned)
end
module State = struct
type t = StateTable.t = {
installed: package_set;
installed_roots: package_set;
compiler: package_set;
pinned: pin_option name_map;
}
include (LineFile (StateTable) : IO_FILE with type t := t)
end
(** III - Opam Syntax parser and associated file types *)
module Syntax = struct
(* Idea: have a [(ic, oc_with_lock * t) pp] that can be used to reading and
re-writing files with a guarantee that it hasn't been rewritten in the
meantime *)
let pp_channel filename ic oc =
Pp.pp
(fun ~pos:_ () ->
let lexbuf = Lexing.from_channel ic in
let filename = OpamFilename.to_string filename in
lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with
Lexing.pos_fname = filename };
OpamParser.main OpamLexer.token lexbuf filename)
(fun file ->
let fmt = Format.formatter_of_out_channel oc in
OpamFormat.Print.format_opamfile fmt file)
let of_channel (filename:filename) (ic:in_channel) =
Pp.parse ~pos:(pos_file filename) (pp_channel filename ic stdout) ()
let to_channel filename oc t =
Pp.print (pp_channel filename stdin oc) t
let of_string (filename:filename) str =
let lexbuf = Lexing.from_string str in
let filename = OpamFilename.to_string filename in
lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with
Lexing.pos_fname = filename };
OpamParser.main OpamLexer.token lexbuf filename
let to_string _file_name t =
OpamFormat.Print.opamfile t
let to_string_with_preserved_format filename ~empty ?(sections=[]) ~fields pp t =
let str = OpamFilename.read filename in
let syn_file = of_string filename str in
let syn_t = Pp.print pp (filename, t) in
let it_name = function
| Variable (_, f, _) | Section (_, {section_kind = f; _}) -> f
in
let it_pos = function
| Section (pos,_) | Variable (pos,_,_) -> pos
in
let lines_index =
let rec aux acc s =
let until =
try Some (String.index_from s (List.hd acc) '\n')
with Not_found -> None
in
match until with
| Some until -> aux (until+1 :: acc) s
| None -> Array.of_list (List.rev acc)
in
aux [0] str
in
let pos_index (_file, li, col) = lines_index.(li - 1) + col in
let field_str name =
let rec aux = function
| it1 :: r when it_name it1 = name ->
let start = pos_index (it_pos it1) in
let stop = match r with
| it2 :: _ -> pos_index (it_pos it2) - 1
| [] ->
let len = ref (String.length str) in
while str.[!len - 1] = '\n' do decr len done;
!len
in
String.sub str start (stop - start)
| _ :: r -> aux r
| [] -> raise Not_found
in
aux syn_file.file_contents
in
let rem, strs =
List.fold_left (fun (rem, strs) item ->
List.filter (fun i -> it_name i <> it_name item) rem,
match item with
| Variable (pos, name, v) ->
(try
let ppa = List.assoc name fields in
match snd (Pp.print ppa t) with
| None | Some (List (_, [])) | Some (List (_,[List(_,[])])) ->
strs
| field_syn_t when
field_syn_t =
snd (Pp.print ppa (Pp.parse ppa ~pos (empty, Some v)))
->
(* unchanged *)
field_str name :: strs
| _ ->
try
let f =
List.find (fun i -> it_name i = name) syn_t.file_contents
in
OpamFormat.Print.items [f] :: strs
with Not_found -> strs
with Not_found ->
if OpamStd.String.starts_with ~prefix:"x-" name then
field_str name :: strs
else strs)
| Section (pos, {section_kind = name; section_items = v;_}) ->
(try
let ppa = List.assoc name sections in
let sec_field_t = snd (Pp.print ppa t) in
if sec_field_t <> None &&
sec_field_t = snd
(Pp.print ppa (Pp.parse ppa ~pos (empty, Some v)))
then
(* unchanged *)
field_str name :: strs
else
try
let f =
List.find (fun i -> it_name i = name) syn_t.file_contents
in
OpamFormat.Print.items [f] :: strs
with Not_found -> strs
with Not_found -> strs)
)
(syn_t.file_contents, []) syn_file.file_contents
in
String.concat "\n"
(List.rev_append strs
(if rem = [] then [""] else [OpamFormat.Print.items rem;""]))
end
module type SyntaxFileArg = sig
val internal: string
type t
val empty: t
val pp: (opamfile, filename * t) Pp.t
end
module SyntaxFile(X: SyntaxFileArg) : IO_FILE with type t := X.t = struct
module IO = struct
let to_opamfile filename t = Pp.print X.pp (filename, t)
let of_channel filename (ic:in_channel) =
Pp.parse X.pp ~pos:(pos_file filename) (Syntax.of_channel filename ic)
|> snd
let to_channel filename oc t =
Syntax.to_channel filename oc (to_opamfile filename t)
let of_string (filename:filename) str =
Pp.parse X.pp ~pos:(pos_file filename) (Syntax.of_string filename str)
|> snd
let to_string filename t =
Syntax.to_string filename (to_opamfile filename t)
end
include IO
include X
include MakeIO(struct
include X
include IO
end)
end
(** (1) Internal files *)
(** General opam configuration (config) *)
module ConfigSyntax = struct
let internal = "config"
type t = {
opam_version : opam_version;
repositories : repository_name list ;
switch : switch;
jobs : int;
dl_tool : arg list option;
dl_jobs : int;
solver_criteria : (solver_criteria * string) list;
solver : arg list option;
}
let opam_version t = t.opam_version
let repositories t = t.repositories
let switch t = t.switch
let jobs t = t.jobs
let dl_tool t = t.dl_tool
let dl_jobs t = t.dl_jobs
let criteria t = t.solver_criteria
let criterion kind t =
try Some (List.assoc kind t.solver_criteria)
with Not_found -> None
let solver t = t.solver
let with_opam_version t opam_version = { t with opam_version }
let with_repositories t repositories = { t with repositories }
let with_switch t switch = { t with switch }
let with_jobs t jobs = { t with jobs }
let with_dl_tool t dl_tool = { t with dl_tool = Some dl_tool }
let with_dl_jobs t dl_jobs = { t with dl_jobs }
let with_criteria t solver_criteria = { t with solver_criteria }
let with_criterion kind t criterion =
{ t with solver_criteria =
(kind,criterion)::List.remove_assoc kind t.solver_criteria }
let with_solver t solver = { t with solver = Some solver }
let create switch repositories ?(criteria=[]) ?solver jobs ?download_tool dl_jobs =
{ opam_version = OpamVersion.current;
repositories ; switch ; jobs ; dl_tool = download_tool; dl_jobs ;
solver_criteria = criteria; solver }
let empty = {
opam_version = OpamVersion.current;
repositories = [];
switch = OpamSwitch.of_string "<empty>";
jobs = 1;
dl_tool = None;
dl_jobs = 1;
solver_criteria = [];
solver = None;
}
let fields =
let with_switch t sw =
if t.switch = empty.switch then with_switch t sw
else OpamFormat.bad_format "Multiple switch specifications"
in
[
"opam-version", Pp.ppacc
with_opam_version opam_version
(Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion: Pp.STR with type t = OpamVersion.t));
"repositories", Pp.ppacc
with_repositories repositories
(Pp.V.map_list ~depth:1
(Pp.V.string -|
Pp.of_module "repository" (module OpamRepositoryName: Pp.STR with type t = OpamRepositoryName.t)));
"switch", Pp.ppacc
with_switch switch
(Pp.V.string -| Pp.of_module "switch" (module OpamSwitch: Pp.STR with type t = OpamSwitch.t));
"jobs", Pp.ppacc
with_jobs jobs
Pp.V.pos_int;
"download-command", Pp.ppacc_opt
with_dl_tool dl_tool
(Pp.V.map_list ~depth:1 Pp.V.arg);
"download-jobs", Pp.ppacc
with_dl_jobs dl_jobs
Pp.V.pos_int;
"solver-criteria", Pp.ppacc_opt
(with_criterion `Default) (criterion `Default)
Pp.V.string;
"solver-upgrade-criteria", Pp.ppacc_opt
(with_criterion `Upgrade) (criterion `Upgrade)
Pp.V.string;
"solver-fixup-criteria", Pp.ppacc_opt
(with_criterion `Fixup) (criterion `Fixup)
Pp.V.string;
"solver", Pp.ppacc_opt
with_solver solver
(Pp.V.map_list ~depth:1 Pp.V.arg);
(* deprecated fields *)
"alias", Pp.ppacc_opt
with_switch OpamStd.Option.none
(Pp.V.string -| Pp.of_module "switch-name" (module OpamSwitch: Pp.STR with type t = OpamSwitch.t));
"ocaml-version", Pp.ppacc_opt
with_switch OpamStd.Option.none
(Pp.V.string -| Pp.of_module "switch-name" (module OpamSwitch: Pp.STR with type t = OpamSwitch.t));
"cores", Pp.ppacc_opt
with_jobs OpamStd.Option.none
Pp.V.pos_int;
"system_ocaml-version", Pp.ppacc_ignore;
"system-ocaml-version", Pp.ppacc_ignore;
]
let pp =
let name = internal in
Pp.I.map_file @@
Pp.I.check_fields ~name fields -|
Pp.I.fields ~name ~empty fields -|
Pp.check ~name (fun t -> t.switch <> empty.switch)
~errmsg:"missing switch"
end
module Config = struct
include ConfigSyntax
include SyntaxFile(ConfigSyntax)
end
(** Local repository config file (repo/<repo>/config) *)
module Repo_configSyntax = struct
let internal = "repo-config"
type t = repository
let empty = {
repo_name = OpamRepositoryName.of_string "<none>";
repo_url = OpamUrl.empty;
repo_root = OpamFilename.raw_dir "<none>";
repo_priority = 0;
}
let fields = [
"name", Pp.ppacc
(fun r repo_name -> {r with repo_name})
(fun r -> r.repo_name)
(Pp.V.string -|
Pp.of_module "repository-name" (module OpamRepositoryName: Pp.STR with type t = OpamRepositoryName.t));
"address", Pp.ppacc
(fun r repo_url -> {r with repo_url})
(fun r -> r.repo_url)
Pp.V.url;
"kind", Pp.ppacc_opt (* deprecated *)
(fun r backend ->
{r with repo_url = {r.repo_url with OpamUrl.backend}})
OpamStd.Option.none
(Pp.V.string -|
Pp.of_pair "repository-kind"
OpamUrl.(backend_of_string, string_of_backend));
"priority", Pp.ppacc
(fun r repo_priority -> {r with repo_priority})
(fun r -> r.repo_priority)
Pp.V.int;
"root", Pp.ppacc
(fun r repo_root -> {r with repo_root})
(fun r -> r.repo_root)
(Pp.V.string -|
Pp.of_module "directory" (module OpamFilename.Dir: Pp.STR with type t = OpamFilename.Dir.t));
]
let pp =
let name = internal in
Pp.I.map_file @@
Pp.I.check_fields fields -|
Pp.I.fields ~name:"repo-file" ~empty fields -|
Pp.check ~name (fun r -> r.repo_root <> empty.repo_root)
~errmsg:"missing 'root:'" -|
Pp.check ~name (fun r -> r.repo_url <> OpamUrl.empty)
~errmsg:"missing 'address:'" -|
Pp.check ~name (fun r -> r.repo_name <> empty.repo_name)
~errmsg:"missing 'name:'"
end
module Repo_config = struct
include Repo_configSyntax
include SyntaxFile(Repo_configSyntax)
end
(** Global or package switch-local configuration variables.
This file has free fields.
(<switch>/config/global-config.config,
<switch>/lib/<pkgname>/opam.config) *)
module Dot_configSyntax = struct
let internal = ".config"
type t = (variable * variable_contents) list
let create variables = variables
let empty = []
let pp =
Pp.I.map_file @@
Pp.I.items -|
Pp.map_list
(Pp.map_pair
(Pp.of_module "variable" (module OpamVariable: Pp.STR with type t = OpamVariable.t))
Pp.V.variable_contents)
let variables t = List.rev_map fst t
let bindings t = t
let variable t s =
try Some (List.assoc s t)
with Not_found -> None