|
| 1 | +type t = Yum | Dnf |
| 2 | + |
| 3 | +let _cache = ref None |
| 4 | + |
| 5 | +let active () = |
| 6 | + match !_cache with |
| 7 | + | Some v -> |
| 8 | + v |
| 9 | + | None -> ( |
| 10 | + match Sys.file_exists !Xapi_globs.dnf_cmd with |
| 11 | + | true -> |
| 12 | + _cache := Some Dnf ; |
| 13 | + Dnf |
| 14 | + | false -> |
| 15 | + _cache := Some Yum ; |
| 16 | + Yum |
| 17 | + ) |
| 18 | + |
| 19 | +module type S = sig |
| 20 | + val repoquery_installed : unit -> string * string list |
| 21 | + |
| 22 | + val clean_cache : string -> string * string list |
| 23 | + |
| 24 | + val get_pkgs_from_updateinfo : string -> string list -> string * string list |
| 25 | + |
| 26 | + val get_updates_from_upgrade_dry_run : string list -> string * string list |
| 27 | + |
| 28 | + val is_obsoleted : string -> string list -> string * string list |
| 29 | + |
| 30 | + val get_pkgs_from_repoquery : string -> string list -> string * string list |
| 31 | + |
| 32 | + val config_repo : string -> string list -> string * string list |
| 33 | + |
| 34 | + val get_repo_config : string -> string * string list |
| 35 | + |
| 36 | + val make_cache : string -> string * string list |
| 37 | + |
| 38 | + val sync_repo : string -> string * string list |
| 39 | +end |
| 40 | + |
| 41 | +module type Args = sig |
| 42 | + val pkg_cmd : string |
| 43 | + |
| 44 | + val repoquery_cmd : string |
| 45 | + |
| 46 | + val repoconfig_cmd : string |
| 47 | + |
| 48 | + val reposync_cmd : string |
| 49 | + |
| 50 | + val repoquery_installed : unit -> string list |
| 51 | + |
| 52 | + val clean_cache : string -> string list |
| 53 | + |
| 54 | + val get_pkgs_from_updateinfo : string -> string list -> string list |
| 55 | + |
| 56 | + val get_updates_from_upgrade_dry_run : string list -> string list |
| 57 | + |
| 58 | + val is_obsoleted : string -> string list -> string list |
| 59 | + |
| 60 | + val get_pkgs_from_repoquery : string -> string list -> string list |
| 61 | + |
| 62 | + val config_repo : string -> string list -> string list |
| 63 | + |
| 64 | + val get_repo_config : string -> string list |
| 65 | + |
| 66 | + val make_cache : string -> string list |
| 67 | + |
| 68 | + val sync_repo : string -> string list |
| 69 | +end |
| 70 | + |
| 71 | +let repoquery_sep = ":|" |
| 72 | + |
| 73 | +let fmt = |
| 74 | + ["name"; "epoch"; "version"; "release"; "arch"; "repoid"] |
| 75 | + |> List.map (fun field -> "%{" ^ field ^ "}") |
| 76 | + |> String.concat repoquery_sep |
| 77 | + |
| 78 | +module Common_args = struct |
| 79 | + let clean_cache = function |
| 80 | + | "*" -> |
| 81 | + ["clean"; "all"] |
| 82 | + | name -> |
| 83 | + [ |
| 84 | + "--disablerepo=*" |
| 85 | + ; Printf.sprintf "--enablerepo=%s" name |
| 86 | + ; "clean" |
| 87 | + ; "all" |
| 88 | + ] |
| 89 | + |
| 90 | + let get_pkgs_from_updateinfo sub_command repositories = |
| 91 | + [ |
| 92 | + "-q" |
| 93 | + ; "--disablerepo=*" |
| 94 | + ; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories) |
| 95 | + ; "updateinfo" |
| 96 | + ; "list" |
| 97 | + ; sub_command |
| 98 | + ] |
| 99 | + |
| 100 | + let is_obsoleted pkg_name repositories = |
| 101 | + [ |
| 102 | + "-a" |
| 103 | + ; "--disablerepo=*" |
| 104 | + ; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories) |
| 105 | + ; "--whatobsoletes" |
| 106 | + ; pkg_name |
| 107 | + ; "--qf" |
| 108 | + ; "%{name}" |
| 109 | + ] |
| 110 | + |
| 111 | + let get_updates_from_upgrade_dry_run repositories = |
| 112 | + [ |
| 113 | + "--disablerepo=*" |
| 114 | + ; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories) |
| 115 | + ; "--assumeno" |
| 116 | + ; "upgrade" |
| 117 | + ] |
| 118 | + |
| 119 | + let get_pkgs_from_repoquery repositories = |
| 120 | + [ |
| 121 | + "-a" |
| 122 | + ; "--disablerepo=*" |
| 123 | + ; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories) |
| 124 | + ; "--qf" |
| 125 | + ; fmt |
| 126 | + ] |
| 127 | + |
| 128 | + let config_repo repo_name config = config @ [repo_name] |
| 129 | + |
| 130 | + let make_cache repo_name = |
| 131 | + [ |
| 132 | + "--disablerepo=*" |
| 133 | + ; Printf.sprintf "--enablerepo=%s" repo_name |
| 134 | + ; "-y" |
| 135 | + ; "makecache" |
| 136 | + ] |
| 137 | + |
| 138 | + let sync_repo repo_name = |
| 139 | + [ |
| 140 | + "-p" |
| 141 | + ; !Xapi_globs.local_pool_repo_dir |
| 142 | + ; "--downloadcomps" |
| 143 | + ; "--download-metadata" |
| 144 | + ; "--delete" |
| 145 | + ; "--newest-only" |
| 146 | + ; Printf.sprintf "--repoid=%s" repo_name |
| 147 | + ] |
| 148 | +end |
| 149 | + |
| 150 | +module Yum_args : Args = struct |
| 151 | + include Common_args |
| 152 | + |
| 153 | + let repoquery_installed () = ["-a"; "--pkgnarrow=installed"; "--qf"; fmt] |
| 154 | + |
| 155 | + let pkg_cmd = !Xapi_globs.yum_cmd |
| 156 | + |
| 157 | + let repoquery_cmd = !Xapi_globs.repoquery_cmd |
| 158 | + |
| 159 | + let repoconfig_cmd = !Xapi_globs.yum_config_manager_cmd |
| 160 | + |
| 161 | + let reposync_cmd = !Xapi_globs.reposync_cmd |
| 162 | + |
| 163 | + let get_updates_from_upgrade_dry_run repositories = |
| 164 | + ["--quiet"] @ Common_args.get_updates_from_upgrade_dry_run repositories |
| 165 | + |
| 166 | + let is_obsoleted pkg_name repositories = |
| 167 | + Common_args.is_obsoleted pkg_name repositories @ ["--plugins"] |
| 168 | + |
| 169 | + let get_pkgs_from_repoquery pkg_narrow repositories = |
| 170 | + Common_args.get_pkgs_from_repoquery repositories |
| 171 | + @ [Printf.sprintf "--pkgnarrow=%s" pkg_narrow; "--plugins"] |
| 172 | + |
| 173 | + let sync_repo repo_name = Common_args.sync_repo repo_name @ ["--plugins"] |
| 174 | + |
| 175 | + let get_repo_config repo_name = [repo_name] |
| 176 | +end |
| 177 | + |
| 178 | +module Dnf_args : Args = struct |
| 179 | + let pkg_cmd = !Xapi_globs.dnf_cmd |
| 180 | + |
| 181 | + let repoquery_cmd = !Xapi_globs.dnf_cmd |
| 182 | + |
| 183 | + let repoconfig_cmd = !Xapi_globs.dnf_cmd |
| 184 | + |
| 185 | + let reposync_cmd = !Xapi_globs.dnf_cmd |
| 186 | + |
| 187 | + type sub_cmd = Repoquery | Repoconfig | Reposync |
| 188 | + |
| 189 | + let sub_cmd_to_string = function |
| 190 | + | Reposync -> |
| 191 | + "reposync" |
| 192 | + | Repoconfig -> |
| 193 | + "config-manager" |
| 194 | + | Repoquery -> |
| 195 | + "repoquery" |
| 196 | + |
| 197 | + let add_sub_cmd sub_cmd params = [sub_cmd_to_string sub_cmd] @ params |
| 198 | + |
| 199 | + include Common_args |
| 200 | + |
| 201 | + let repoquery_installed () = |
| 202 | + ["-a"; "--qf"; fmt; "--installed"] |> add_sub_cmd Repoquery |
| 203 | + |
| 204 | + let is_obsoleted pkg_name repositories = |
| 205 | + Common_args.is_obsoleted pkg_name repositories |> add_sub_cmd Repoquery |
| 206 | + |
| 207 | + let get_pkgs_from_repoquery pkg_narrow repositories = |
| 208 | + Common_args.get_pkgs_from_repoquery repositories |
| 209 | + @ [Printf.sprintf "--%s" pkg_narrow] |
| 210 | + |> add_sub_cmd Repoquery |
| 211 | + |
| 212 | + let config_repo repo_name config = |
| 213 | + Common_args.config_repo repo_name config |> add_sub_cmd Repoconfig |
| 214 | + |
| 215 | + let sync_repo repo_name = |
| 216 | + Common_args.sync_repo repo_name |> add_sub_cmd Reposync |
| 217 | + |
| 218 | + let get_repo_config repo_name = |
| 219 | + ["--dump"; repo_name] |> add_sub_cmd Repoconfig |
| 220 | +end |
| 221 | + |
| 222 | +module Cmd_line (M : Args) : S = struct |
| 223 | + (* functor to construct comand line and arguments *) |
| 224 | + let repoquery_installed () = (M.repoquery_cmd, M.repoquery_installed ()) |
| 225 | + |
| 226 | + let clean_cache name = (M.pkg_cmd, M.clean_cache name) |
| 227 | + |
| 228 | + let get_pkgs_from_updateinfo sub_command repositories = |
| 229 | + (M.pkg_cmd, M.get_pkgs_from_updateinfo sub_command repositories) |
| 230 | + |
| 231 | + let get_updates_from_upgrade_dry_run repositories = |
| 232 | + (M.pkg_cmd, M.get_updates_from_upgrade_dry_run repositories) |
| 233 | + |
| 234 | + let is_obsoleted pkg_name repositories = |
| 235 | + (M.repoquery_cmd, M.is_obsoleted pkg_name repositories) |
| 236 | + |
| 237 | + let get_pkgs_from_repoquery pkg_narrow repositories = |
| 238 | + (M.repoquery_cmd, M.get_pkgs_from_repoquery pkg_narrow repositories) |
| 239 | + |
| 240 | + let config_repo repo_name config = |
| 241 | + (M.repoconfig_cmd, M.config_repo repo_name config) |
| 242 | + |
| 243 | + let get_repo_config repo_name = (M.repoconfig_cmd, M.get_repo_config repo_name) |
| 244 | + |
| 245 | + let make_cache repo_name = (M.pkg_cmd, M.make_cache repo_name) |
| 246 | + |
| 247 | + let sync_repo repo_name = (M.reposync_cmd, M.sync_repo repo_name) |
| 248 | +end |
| 249 | + |
| 250 | +module Yum_cmd = Cmd_line (Yum_args) |
| 251 | +module Dnf_cmd = Cmd_line (Dnf_args) |
| 252 | + |
| 253 | +let get_pkg_mgr : (module S) = |
| 254 | + match active () with Dnf -> (module Dnf_cmd) | Yum -> (module Yum_cmd) |
0 commit comments