Skip to content

Commit

Permalink
Merge pull request #105 from hammerlab/sm-more-dune-and-base
Browse files Browse the repository at this point in the history
more dune, and base, etc
  • Loading branch information
smondet authored Feb 21, 2020
2 parents f468dbc + b95ff33 commit 263c0a3
Show file tree
Hide file tree
Showing 32 changed files with 953 additions and 1,875 deletions.
1 change: 1 addition & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile=compact
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ os:
- linux
- osx
env:
- OCAML_VERSION=4.03.0 OPAM_VERSION=1.2.1 DOCKER_BUILD=false
- OCAML_VERSION=4.04.2 OPAM_VERSION=1.2.1 DOCKER_BUILD=false
- OCAML_VERSION=4.05.0 OPAM_VERSION=1.2.1 DOCKER_BUILD=false
- OCAML_VERSION=4.06.1 OPAM_VERSION=1.2.1 DOCKER_BUILD=true
- OCAML_VERSION=4.06.1 DOCKER_BUILD=true
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ You can also build locally:
You need OCaml ≥ 4.03.0 together with
[`nonstd`](http://www.hammerlab.org/docs/nonstd/master/index.html),
[`sosa`](http://www.hammerlab.org/docs/sosa/master/index.html), and
[`jbuilder`](https://github.com/janestreet/jbuilder):
[`dune`](https://github.com/janestreet/dune):

ocaml please.mlt configure
jbuilder build @install
dune build @install

Getting Started
---------------
Expand Down Expand Up @@ -148,7 +147,7 @@ To run the tests you also need `make` and there is an additional dependency on
the `uri` library, see:

genspio_test=_build/default/src/test/main.exe
jbuilder build $genspio_test
dune build $genspio_test
$genspio_test --help


Expand Down
2 changes: 1 addition & 1 deletion doc/extra-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ provides a command line tool to generate “Qemu” environments.
You can build it with:

export genspio_vm_tester=_build/default/src/examples/vm_tester.exe
jbuilder build $genspio_vm_tester
dune build $genspio_vm_tester

A version is also available in the genspio-doc docker-images (note that the
[build](https://hub.docker.com/r/smondet/genspio-doc-dockerfiles/builds/)
Expand Down
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 1.11)
(name genspio)
10 changes: 4 additions & 6 deletions genspio.opam
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# This Opam file was auto-generated, see the `please.mlt` script.
opam-version: "2.0"
maintainer: "Seb Mondet <seb@mondet.org>"
authors: [
Expand All @@ -10,14 +9,13 @@ dev-repo: "git+https://github.com/hammerlab/genspio.git"
license: "Apache 2.0"
version: "0.0.3-dev"
build: [
["ocaml" "please.mlt" "configure"]
["jbuilder" "build" "-p" "genspio" "-j" jobs ]
["dune" "build" "-p" name "-j" jobs ]
]
depends: [
"ocaml" { >= "4.03.0" }
"jbuilder" {build & >= "1.0+beta20"}
"nonstd"
"sosa"
"dune"
"base"
"fmt"
]
synopsis: "Typed EDSL to generate POSIX Shell scripts"
description: """
Expand Down
72 changes: 0 additions & 72 deletions please.mlt

This file was deleted.

79 changes: 30 additions & 49 deletions src/examples/downloader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ let downloader () =
let say strings =
let sayone ?(prompt = false) s =
let prompt = if prompt then "downloader: " else "" in
call [string "printf"; string (prompt ^ "%s"); s]
in
call [string "printf"; string (prompt ^ "%s"); s] in
match strings with
| [] -> nop
| s :: more ->
seq
( (sayone ~prompt:true s :: List.map more ~f:sayone)
@ [sayone (string "\n")] )
in
@ [sayone (string "\n")] ) in
let sayf fmt = ksprintf (fun s -> say [string s]) fmt in
let fail l = seq [say (string "ERROR: " :: l); fail "fail-list"] in
let failf fmt = ksprintf (fun s -> fail [string s]) fmt in
Expand All @@ -36,20 +34,16 @@ let downloader () =
[ sayf "Expression %s failed!" name
; call [string "cat"; self#stderr]
; failf "Fatal failure of %s" name ]
end
in
end in
let silence ~name unit =
let s = silent ~name [unit] in
s#exec
in
s#exec in
let succeed_in_silence_or_fail ~name units =
let s = silent ~name units in
s#succeed_or_fail
in
s#succeed_or_fail in
let download ~url ~output =
let try_help ?(opt = "--help") cmd =
exec [cmd; opt] |> silence ~name:(cmd ^ opt) |> succeeds
in
exec [cmd; opt] |> silence ~name:(cmd ^ opt) |> succeeds in
let do_call exec args =
[ sayf "Using `%s`." exec
; succeed_in_silence_or_fail ~name:exec [call (string exec :: args)] ]
Expand All @@ -59,22 +53,18 @@ let downloader () =
(do_call "wget" [url; string "--output-document"; output])
; case (try_help "curl")
(do_call "curl" [string "-L"; string "-o"; output; url])
; default [failf "Can't find a downloading application"] ]
in
; default [failf "Can't find a downloading application"] ] in
let string_matches_any string regexp_list =
(* Cf. http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html *)
let options = List.concat_map regexp_list ~f:(fun r -> ["-e"; r]) in
string >> exec (["grep"; "-q"] @ options) |> succeeds
in
string >> exec (["grep"; "-q"] @ options) |> succeeds in
let no_newline_sed ~input expr =
let with_potential_newline =
Str.concat_list [input; string "\n"] >> exec ["sed"; expr] |> get_stdout
in
with_potential_newline >> exec ["tr"; "-d"; "\\n"] |> get_stdout
in
with_potential_newline >> exec ["tr"; "-d"; "\\n"] |> get_stdout in
let module Unwrapper = struct
type cmd = unit t

type t = {extension: string; verb: string; commands: file -> cmd list}

let make ~ext ~verb commands = {extension= ext; verb; commands}
Expand All @@ -92,38 +82,35 @@ let downloader () =
(t.commands name_variable)
; name_variable#set
(remove_suffix name_variable#get (sprintf "\\.%s" t.extension))
]
in
] in
seq
[ say [string "Extract loop: "; name_variable#get]
; switch (List.map t_list ~f:make_case) ]

let to_loop name_variable t_list =
loop_while
(string_matches_any name_variable#get
(List.map t_list (fun t -> sprintf "\\.%s$" t.extension)))
(List.map t_list ~f:(fun t -> sprintf "\\.%s$" t.extension)))
~body:(to_switch name_variable t_list)

let all =
[ make ~ext:"gz" ~verb:"Gunzipping" (fun current_name ->
[call [string "gunzip"; string "-f"; current_name#get]] )
[call [string "gunzip"; string "-f"; current_name#get]])
; make ~ext:"bz2" ~verb:"Bunzip2-ing" (fun current_name ->
[call [string "bunzip2"; string "-f"; current_name#get]] )
[call [string "bunzip2"; string "-f"; current_name#get]])
; make ~ext:"zip" ~verb:"Unzipping" (fun current_name ->
[call [string "unzip"; current_name#get]] )
[call [string "unzip"; current_name#get]])
; make ~ext:"tar" ~verb:"Untarring" (fun current_name ->
[call [string "tar"; string "xf"; current_name#get]] )
[call [string "tar"; string "xf"; current_name#get]])
; make ~ext:"tgz" ~verb:"Untar-gzip-ing" (fun name ->
[call [string "tar"; string "zxf"; name#get]] )
[call [string "tar"; string "zxf"; name#get]])
; make ~ext:"tbz2" ~verb:"Untar-bzip2-ing" (fun name ->
[call [string "tar"; string "xfj"; name#get]] )
[call [string "tar"; string "xfj"; name#get]])
; make ~ext:"gpg" ~verb:"Decyphering" (fun name ->
[ call
[ string "gpg"
; string "--output"
[ string "gpg"; string "--output"
; remove_suffix name#get "\\.gpg"
; string "-d"
; name#get ] ] ) ]
; string "-d"; name#get ] ]) ]
end in
let no_value = sprintf "none_%x" (Random.int 100_000) |> string in
let cli_spec =
Expand All @@ -136,27 +123,23 @@ let downloader () =
~default:(str "/tmp/genspio-downloader-tmpdir")
& usage
"Download archives and decrypt/unarchive them.\n\
./downloader -u URL [-c] [-f <file>] [-t <tmpdir>]"
in
Command_line.parse cli_spec (fun ~anon url all_in_tmp filename_ov tmp_dir ->
./downloader -u URL [-c] [-f <file>] [-t <tmpdir>]" in
Command_line.parse cli_spec
(fun ~anon:_ url all_in_tmp filename_ov tmp_dir ->
let current_name = tmp_file ~tmp_dir "current-name" in
let set_output_of_download () =
if_seq
Str.(filename_ov =$= no_value)
~t:
(let filename =
no_newline_sed ~input:url "s/.*\\/\\([^?\\/]*\\).*/\\1/"
in
no_newline_sed ~input:url "s/.*\\/\\([^?\\/]*\\).*/\\1/" in
let output_path =
Str.concat_list [tmp_dir; string "/"; filename]
in
Str.concat_list [tmp_dir; string "/"; filename] in
[current_name#set output_path])
~e:
(let output_path =
Str.concat_list [tmp_dir; string "/"; filename_ov]
in
[current_name#set output_path])
in
Str.concat_list [tmp_dir; string "/"; filename_ov] in
[current_name#set output_path]) in
seq
[ call [string "mkdir"; string "-p"; tmp_dir]
; if_then all_in_tmp
Expand All @@ -171,18 +154,16 @@ let downloader () =
; Unwrapper.to_loop current_name Unwrapper.all ])
(seq
[ fail
[ string "URL: "
; url
; string " -> not HTTP(s) or FTP: NOT IMPLEMENTED" ] ]) ] )
[ string "URL: "; url
; string " -> not HTTP(s) or FTP: NOT IMPLEMENTED" ] ]) ])

let () =
match Sys.argv |> Array.to_list |> List.tl_exn with
| ["make"; path] -> (
let script = Genspio.Compile.to_many_lines (downloader ()) in
let content =
sprintf "#!/bin/sh\n\n# Generated by Genspio Example Tests\n\n%s\n%!"
script
in
script in
match path with
| "-" -> printf "\n`````\n%s`````\n%!" content
| other ->
Expand All @@ -191,6 +172,6 @@ let () =
| other ->
eprintf "Wrong command line: [%s]\n"
(List.map ~f:(sprintf "%S") other |> String.concat ~sep:"; ") ;
eprintf "Usage:\n%s make <path>\n\ Create the downloader script.\n%!"
eprintf "Usage:\n%s make <path>\n Create the downloader script.\n%!"
Sys.argv.(0) ;
exit 1
8 changes: 8 additions & 0 deletions src/examples/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(executable (name downloader) (libraries genspio nonstd sosa)(modules downloader))
(executable (name vm_tester) (libraries genspio nonstd sosa)(modules vm_tester))
(executable (name service_composer) (libraries unix genspio nonstd sosa)(modules service_composer))
(executable (name multigit) (libraries unix genspio nonstd sosa)(modules multigit))
(executable (name small) (libraries genspio nonstd sosa)(modules small))
(rule (targets small_examples.ml)(deps small.exe)(action (progn
(run ./small.exe small_examples.ml))))
(executable (name small_examples) (libraries genspio tests nonstd sosa)(modules small_examples))
Loading

0 comments on commit 263c0a3

Please sign in to comment.