Skip to content

Commit

Permalink
chore: rewrite chromium-version to eio
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Jan 8, 2023
1 parent 6829478 commit e2525b3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 106 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
all: build

build:
dune build -p chromium-version --profile=release
dune build -p osnap --profile=release
dune build -p chromium-version,osnap --profile=release

install:
if ! [ -e _opam ]; then \
Expand Down
127 changes: 63 additions & 64 deletions bin/ChromiumVersion.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Cohttp_lwt_unix
open Lwt.Syntax
open Cohttp_eio

type platform =
| Win64
Expand All @@ -11,35 +10,27 @@ type platform =
Fmt.set_style_renderer Fmt.stdout `Ansi_tty

let print_green msg =
let printer = Fmt.pr " %a " (Fmt.styled `Green Fmt.string) in
let printer = Fmt.pr "%a" (Fmt.styled `Green Fmt.string) in
printer msg
;;

let print_red msg =
let printer = Fmt.pr " %a " (Fmt.styled `Red Fmt.string) in
let printer = Fmt.pr "%a" (Fmt.styled `Red Fmt.string) in
printer msg
;;

let check_availability revision platform =
let uri =
Uri.make
~scheme:"http"
~host:"storage.googleapis.com"
~port:80
~path:
(match platform with
| MacOS -> "/chromium-browser-snapshots/Mac/" ^ revision ^ "/chrome-mac.zip"
| MacOS_ARM ->
"/chromium-browser-snapshots/Mac_Arm/" ^ revision ^ "/chrome-mac.zip"
| Linux ->
"/chromium-browser-snapshots/Linux_x64/" ^ revision ^ "/chrome-linux.zip"
| Win64 -> "/chromium-browser-snapshots/Win_x64/" ^ revision ^ "/chrome-win.zip")
()
let check_availability env revision platform =
let path =
match platform with
| MacOS -> "/chromium-browser-snapshots/Mac/" ^ revision ^ "/chrome-mac.zip"
| MacOS_ARM -> "/chromium-browser-snapshots/Mac_Arm/" ^ revision ^ "/chrome-mac.zip"
| Linux -> "/chromium-browser-snapshots/Linux_x64/" ^ revision ^ "/chrome-linux.zip"
| Win64 -> "/chromium-browser-snapshots/Win_x64/" ^ revision ^ "/chrome-win.zip"
in
let* response = Client.head uri in
let response, _ = Client.head env ~host:"storage.googleapis.com" path in
match response with
| { status = `OK; _ } -> Lwt_result.return platform
| _ -> Lwt_result.fail platform
| Http.Response.{ status = `OK; _ } -> Result.ok platform
| _ -> Result.error platform
;;

let platform_to_string = function
Expand All @@ -49,59 +40,67 @@ let platform_to_string = function
| Linux -> "linux"
;;

let rec check_revision revision =
let rec check_revision env revision =
let revision_str = string_of_int revision in
let* results =
Lwt.all
[ check_availability revision_str Linux
; check_availability revision_str MacOS
; check_availability revision_str MacOS_ARM
; check_availability revision_str Win64
]
let results =
Eio.Fiber.List.map
(check_availability env revision_str)
[ Linux; MacOS; MacOS_ARM; Win64 ]
in
flush stdout;
print_int revision;
print_string ":";
print_string ": ";
let _available, not_available =
results
|> List.partition_map (function
| Result.Ok platform ->
print_green (platform_to_string platform);
print_green (platform_to_string platform ^ " ");
Either.left platform
| Result.Error platform ->
print_red (platform_to_string platform);
print_red (platform_to_string platform ^ " ");
Either.right platform)
in
flush stdout;
print_endline "";
Fmt.pr "@.";
match not_available with
| [] -> Lwt.return revision
| _ -> check_revision (pred revision)
| [] -> revision
| _ -> check_revision env (pred revision)
;;

let main env =
flush_all ();
print_newline ();
print_newline ();
let latestRevisions =
Eio.Fiber.List.map
(Client.get env ~host:"storage.googleapis.com")
[ "/chromium-browser-snapshots/Mac/LAST_CHANGE"
; "/chromium-browser-snapshots/Mac_Arm/LAST_CHANGE"
; "/chromium-browser-snapshots/Linux_x64/LAST_CHANGE"
; "/chromium-browser-snapshots/Win_x64/LAST_CHANGE"
]
in
let latestRevisions =
latestRevisions
|> Eio.Fiber.List.map (function
| ({ Http.Response.status = `OK; _ }, _) as r -> Client.read_fixed r
| response, _body ->
Format.fprintf
Format.err_formatter
"Latest version could not be checked:\n%a\n%!"
Http.Response.pp
response;
exit 1)
in
let matched_revision =
latestRevisions
|> List.map int_of_string
|> List.fold_left min Int.max_int
|> check_revision env
in
print_newline ();
print_green
(Printf.sprintf "Latest available revision across all platforms: %i" matched_revision);
flush_all ();
Fmt.pr "@."
;;

Lwt_main.run
(let uri = Uri.make ~scheme:"http" ~host:"storage.googleapis.com" ~port:80 in
let* latestRevisions =
Lwt.all
[ Client.get (uri ~path:"/chromium-browser-snapshots/Mac/LAST_CHANGE" ())
; Client.get (uri ~path:"/chromium-browser-snapshots/Mac_Arm/LAST_CHANGE" ())
; Client.get (uri ~path:"/chromium-browser-snapshots/Linux_x64/LAST_CHANGE" ())
; Client.get (uri ~path:"/chromium-browser-snapshots/Win_x64/LAST_CHANGE" ())
]
in
let* latestRevisions =
latestRevisions
|> Lwt_list.map_p (function
| { Response.status = `OK; _ }, body -> Cohttp_lwt.Body.to_string body
| response, _body ->
Format.fprintf
Format.err_formatter
"Latest version could not be checked:\n%a\n%!"
Response.pp_hum
response;
exit 1)
in
latestRevisions
|> List.map int_of_string
|> List.fold_left min Int.max_int
|> check_revision)
let () = Eio_main.run main
2 changes: 1 addition & 1 deletion bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
(modules ChromiumVersion)
(public_name chromium-version)
(package chromium-version)
(libraries cohttp cohttp-lwt cohttp-lwt-unix fmt uri lwt lwt.unix))
(libraries cohttp cohttp-eio http eio_main eio eio.core fmt))
8 changes: 6 additions & 2 deletions chromium-version.opam
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ depends: [
"cmdliner"
"fmt"
"cohttp"
"cohttp-lwt-unix"
"cohttp-eio"
"fileutils"
"lwt"
"eio_main"
"odoc" {with-doc}
]
build: [
Expand All @@ -35,3 +35,7 @@ build: [
]
]
dev-repo: "git+https://github.com/eWert-Online/OSnap.git"
pin-depends: [
[ "cohttp.6.0.0~alpha0" "git+https://github.com/mirage/ocaml-cohttp.git#master"]
[ "cohttp-eio.6.0.0~alpha0" "git+https://github.com/mirage/ocaml-cohttp.git#master"]
]
59 changes: 26 additions & 33 deletions chromium-version.opam.locked
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ homepage: "https://github.com/eWert-Online/OSnap"
bug-reports: "https://github.com/eWert-Online/OSnap/issues"
depends: [
"angstrom" {= "0.15.0"}
"asn1-combinators" {= "0.2.6"}
"astring" {= "0.8.5"}
"base" {= "v0.15.1"}
"base-bigarray" {= "base"}
Expand All @@ -20,57 +19,45 @@ depends: [
"base-threads" {= "base"}
"base-unix" {= "base"}
"base64" {= "3.5.0"}
"bigarray-compat" {= "1.1.0"}
"bigstringaf" {= "0.9.0"}
"bos" {= "0.2.1"}
"ca-certs" {= "0.2.3"}
"camlp-streams" {= "5.0.1"}
"chrome-trace" {= "3.6.1"}
"cmdliner" {= "1.1.1"}
"cohttp" {= "5.0.0"}
"cohttp-lwt" {= "5.0.0"}
"cohttp-lwt-unix" {= "5.0.0"}
"conduit" {= "6.1.0"}
"conduit-lwt" {= "6.1.0"}
"conduit-lwt-unix" {= "6.1.0"}
"conf-gmp" {= "4"}
"conf-gmp-powm-sec" {= "3"}
"cohttp-eio" {= "6.0.0~alpha0"}
"conf-pkg-config" {= "2"}
"cppo" {= "1.6.9"}
"csexp" {= "1.5.1"}
"cstruct" {= "6.1.1"}
"domain-name" {= "0.4.0"}
"ctypes" {= "0.20.1"}
"dune" {= "3.6.1"}
"dune-build-info" {= "3.6.1"}
"dune-configurator" {= "3.6.1"}
"dune-rpc" {= "3.6.1"}
"duration" {= "0.2.1"}
"dyn" {= "3.6.1"}
"eio" {= "0.7"}
"eio_luv" {= "0.7"}
"eio_main" {= "0.7"}
"either" {= "1.0.0"}
"eqaf" {= "0.9"}
"fiber" {= "3.6.1"}
"fileutils" {= "0.6.4"}
"fix" {= "20220121"}
"fmt" {= "0.9.0"}
"fpath" {= "0.7.3"}
"gmap" {= "0.3.0"}
"ipaddr" {= "5.3.1"}
"ipaddr-sexp" {= "5.3.1"}
"hmap" {= "0.8.1"}
"http" {= "6.0.0~alpha0"}
"integers" {= "0.7.0"}
"jsonm" {= "1.0.1"}
"logs" {= "0.7.0"}
"luv" {= "0.5.11"}
"luv_unix" {= "0.5.0"}
"lwt" {= "5.6.1"}
"macaddr" {= "5.3.1"}
"magic-mime" {= "1.3.0"}
"lwt-dllist" {= "1.0.1"}
"menhir" {= "20220210"}
"menhirLib" {= "20220210"}
"menhirSdk" {= "20220210"}
"mirage-crypto" {= "0.10.7"}
"mirage-crypto-ec" {= "0.10.7"}
"mirage-crypto-pk" {= "0.10.7"}
"mirage-crypto-rng" {= "0.10.7"}
"mirage-no-solo5" {= "1"}
"mirage-no-xen" {= "1"}
"mtime" {= "2.0.0"}
"num" {= "1.4"}
"mtime" {= "1.4.0"}
"ocaml" {= "5.0.0"}
"ocaml-base-compiler" {= "5.0.0"}
"ocaml-compiler-libs" {= "v0.12.4"}
Expand All @@ -89,20 +76,18 @@ depends: [
"octavius" {= "1.2.2"}
"odoc-parser" {= "2.0.0"}
"omd" {= "1.3.2"}
"optint" {= "0.3.0"}
"ordering" {= "3.6.1"}
"parsexp" {= "v0.15.0"}
"pbkdf" {= "1.2.0"}
"pp" {= "1.1.2"}
"ppx_derivers" {= "1.2.1"}
"ppx_sexp_conv" {= "v0.15.1"}
"ppx_yojson_conv_lib" {= "v0.15.0"}
"ppxlib" {= "0.28.0"}
"psq" {= "0.2.1"}
"ptime" {= "1.1.0"}
"re" {= "1.10.4"}
"result" {= "1.5"}
"rresult" {= "0.7.0"}
"seq" {= "base"}
"sexplib" {= "v0.15.1"}
"sexplib0" {= "v0.15.1"}
"spawn" {= "v0.15.1"}
"stdio" {= "v0.15.0"}
Expand All @@ -116,10 +101,8 @@ depends: [
"uucp" {= "15.0.0"}
"uuseg" {= "15.0.0"}
"uutf" {= "1.0.3"}
"x509" {= "0.16.2"}
"xdg" {= "3.6.1"}
"yojson" {= "2.0.2"}
"zarith" {= "1.12"}
]
build: [
["dune" "subst"] {dev}
Expand All @@ -135,4 +118,14 @@ build: [
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/eWert-Online/OSnap.git"
dev-repo: "git+https://github.com/eWert-Online/OSnap.git"
pin-depends: [
[
"cohttp.5.0.0"
"https://github.com/mirage/ocaml-cohttp/releases/download/v5.0.0/cohttp-5.0.0.tbz"
]
[
"cohttp-eio.6.0.0~alpha0"
"git+https://github.com/mirage/ocaml-cohttp.git#master"
]
]
4 changes: 4 additions & 0 deletions chromium-version.opam.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pin-depends: [
[ "cohttp.6.0.0~alpha0" "git+https://github.com/mirage/ocaml-cohttp.git#master"]
[ "cohttp-eio.6.0.0~alpha0" "git+https://github.com/mirage/ocaml-cohttp.git#master"]
]
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
cmdliner
fmt
cohttp
cohttp-lwt-unix
cohttp-eio
fileutils
lwt))
eio_main))
2 changes: 1 addition & 1 deletion lib/OSnap_Config/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
(name OSnap_Config)
(libraries OSnap_Response OSnap_Utils re fileutils yaml yojson))
(libraries OSnap_Response OSnap_Utils re fileutils unix yaml yojson))
6 changes: 5 additions & 1 deletion osnap.opam.locked
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ depends: [
"mirage-crypto-rng" {= "0.10.7"}
"mirage-no-solo5" {= "1"}
"mirage-no-xen" {= "1"}
"mtime" {= "2.0.0"}
"mtime" {= "1.4.0"}
"num" {= "1.4"}
"ocaml" {= "5.0.0"}
"ocaml-base-compiler" {= "5.0.0"}
Expand Down Expand Up @@ -159,6 +159,10 @@ pin-depends: [
[
"cdp.dev"
"git+https://github.com/eWert-Online/reason-cdp.git#ebb07093e93a27e302afde04296fbc4d22f83948"
]
[
"cohttp.5.0.0"
"https://github.com/mirage/ocaml-cohttp/releases/download/v5.0.0/cohttp-5.0.0.tbz"
]
["libspng.dev" "git+https://github.com/eWert-Online/esy-libspng.git#opam"]
[
Expand Down

0 comments on commit e2525b3

Please sign in to comment.