From 255c33b1a3b612f9dd366253481386f1c6ffe95a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 12 May 2023 20:30:02 +0200 Subject: [PATCH] move preparation to selenium file --- bin/main.ml | 27 +++++++-------------------- lib/selenium_init.ml | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index cca447b..ece9519 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,26 +1,13 @@ open Pusk.Utils open Pusk.Selenium_init +let main = () + let () = - (* Selenium *) - let version_selenium = "4.9.0" in - let selenium = fmt "selenium-%s.jar" version_selenium in - if not (Sys.file_exists selenium) then Lwt_main.run (download_selenium version_selenium); - (* Gecko Driver *) - let driver = fmt "geckodriver" in - if not (Sys.file_exists driver) - then ( - let version_driver = "0.33.0" in - let archive = fmt "./gecko-%s.tar.gz" version_driver in - Lwt_main.run (download_gecko_driver version_driver archive); - let _ = Sys.command (fmt "tar xvzf %s" archive) in - ()); - let selenium_pid = - run_program_in_background "java" [ fmt "-jar %s" selenium; "standalone" ] - in + let selenium = prepare "4.9.0" (Gecko "0.33.0") in + let selenium_pid = run selenium in print_endline (fmt "Java running in %d" selenium_pid); - let closed_pid, _ = Unix.waitpid [] selenium_pid in - if not (closed_pid = selenium_pid) - then print_endline "WTF???" - else print_endline (fmt "Program %d closed!" closed_pid) + main; + let closed_pid = close selenium_pid in + print_endline (fmt "Program %d closed!" closed_pid) ;; diff --git a/lib/selenium_init.ml b/lib/selenium_init.ml index f4e9fde..f9fc7d1 100644 --- a/lib/selenium_init.ml +++ b/lib/selenium_init.ml @@ -60,3 +60,25 @@ let run_program_in_background program args = Unix.execvp program (Array.of_list (program :: args)) | _ -> pid (* Parent process *) ;; + +type driver = Gecko of string + +let prepare version_selenium driver = + (* Gecko Driver *) + (match driver with + | Gecko version_driver -> + if not (Sys.file_exists "geckodriver") + then ( + let archive = fmt "./gecko-%s.tar.gz" version_driver in + Lwt_main.run (download_gecko_driver version_driver archive); + (* TODO: Use native version instead of relying on Unix tools *) + let _ = Sys.command (fmt "tar xvzf %s" archive) in + ())); + (* Selenium *) + let selenium = fmt "selenium-%s.jar" version_selenium in + if not (Sys.file_exists selenium) then Lwt_main.run (download_selenium version_selenium); + selenium +;; + +let run path = run_program_in_background "java" [ fmt "-jar %s" path; "standalone" ] +let close pid = fst (Unix.waitpid [] pid)