helper: Print /modules when verbose >= 2
[febootstrap.git] / febootstrap_yum_rpm.ml
1 (* febootstrap 3
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *)
18
19 (* Yum and RPM support. *)
20
21 open Unix
22 open Printf
23
24 open Febootstrap_package_handlers
25 open Febootstrap_utils
26 open Febootstrap_cmdline
27
28 (* Create a temporary directory for use by all the functions in this file. *)
29 let tmpdir = tmpdir ()
30
31 let yum_rpm_detect () =
32   (file_exists "/etc/redhat-release" || file_exists "/etc/fedora-release") &&
33     Config.yum <> "no" && Config.rpm <> "no"
34
35 let yum_rpm_resolve_dependencies_and_download names =
36   (* Liberate this data from python. *)
37   let tmpfile = tmpdir // "names.tmp" in
38   let py = sprintf "
39 import yum
40 import yum.misc
41 import sys
42
43 yb = yum.YumBase ()
44 yb.preconf.debuglevel = %d
45 yb.preconf.errorlevel = %d
46 if %s:
47     yb.preconf.fn = %S
48 yb.setCacheDir ()
49
50 # Look up the base packages from the command line.
51 deps = dict ()
52 pkgs = yb.pkgSack.returnPackages (patterns=sys.argv[1:])
53 for pkg in pkgs:
54     deps[pkg] = False
55
56 # Recursively find all the dependencies.
57 stable = False
58 while not stable:
59     stable = True
60     for pkg in deps.keys():
61         if deps[pkg] == False:
62             deps[pkg] = []
63             stable = False
64             for r in pkg.requires:
65                 ps = yb.whatProvides (r[0], r[1], r[2])
66                 best = yb._bestPackageFromList (ps.returnPackages ())
67                 if best.name != pkg.name:
68                     deps[pkg].append (best)
69                     if not deps.has_key (best):
70                         deps[best] = False
71             deps[pkg] = yum.misc.unique (deps[pkg])
72
73 # Write it to a file because yum spews garbage on stdout.
74 f = open (%S, \"w\")
75 for pkg in deps.keys ():
76     f.write (\"%%s %%s %%s %%s %%s\\n\" %%
77              (pkg.name, pkg.epoch, pkg.version, pkg.release, pkg.arch))
78 f.close ()
79 "
80     (if verbose then 1 else 0)
81     (if verbose then 1 else 0)
82     (match yum_config with None -> "False" | Some _ -> "True")
83     (match yum_config with None -> "" | Some filename -> filename)
84     tmpfile in
85   run_python py names;
86   let chan = open_in tmpfile in
87   let lines = input_all_lines chan in
88   close_in chan;
89
90   (* Get fields. *)
91   let pkgs =
92     List.map (
93       fun line ->
94         match string_split " " line with
95         | [name; epoch; version; release; arch] ->
96             name, int_of_string epoch, version, release, arch
97         | _ ->
98             eprintf "febootstrap: bad output from python script: '%s'" line;
99             exit 1
100     ) lines in
101
102   (* Something of a hack for x86_64: exclude all i[3456]86 packages. *)
103   let pkgs =
104     if Config.host_cpu = "x86_64" then (
105       List.filter (
106         function (_, _, _, _, ("i386"|"i486"|"i586"|"i686")) -> false
107         | _ -> true
108       ) pkgs
109     )
110     else pkgs in
111
112   (* Drop the kernel package to save time. *)
113   let pkgs =
114     List.filter (function ("kernel",_,_,_,_) -> false | _ -> true) pkgs in
115
116   (* Exclude packages matching [--exclude] regexps on the command line. *)
117   let pkgs =
118     List.filter (
119       fun (name, _, _, _, _) ->
120         not (List.exists (fun re -> Str.string_match re name 0) excludes)
121     ) pkgs in
122
123   (* Sort the list of packages, and remove duplicates (by name).
124    * XXX This is not quite right: we really want to keep the latest
125    * package if duplicates are found, but that would require a full
126    * version compare function.
127    *)
128   let pkgs = List.sort (fun a b -> compare b a) pkgs in
129   let pkgs =
130     let cmp (name1, _, _, _, _) (name2, _, _, _, _) = compare name1 name2 in
131     uniq ~cmp pkgs in
132   let pkgs = List.sort compare pkgs in
133
134   (* Construct package names. *)
135   let pkgnames = List.map (
136     function
137     | name, 0, version, release, arch ->
138         sprintf "%s-%s-%s.%s" name version release arch
139     | name, epoch, version, release, arch ->
140         sprintf "%d:%s-%s-%s.%s" epoch name version release arch
141   ) pkgs in
142
143   if pkgnames = [] then (
144     eprintf "febootstrap: yum-rpm: error: no packages to download\n";
145     exit 1
146   );
147
148   let cmd = sprintf "yumdownloader%s%s --destdir %s %s"
149     (if verbose then "" else " --quiet")
150     (match yum_config with None -> ""
151      | Some filename -> sprintf " -c %s" filename)
152     (Filename.quote tmpdir)
153     (String.concat " " (List.map Filename.quote pkgnames)) in
154   run_command cmd;
155
156   (* Return list of package filenames. *)
157   List.map (
158     (* yumdownloader doesn't include epoch in the filename *)
159     fun (name, _, version, release, arch) ->
160       sprintf "%s/%s-%s-%s.%s.rpm" tmpdir name version release arch
161   ) pkgs
162
163 let rec yum_rpm_list_files pkg =
164   (* Run rpm -qlp with some extra magic. *)
165   let cmd =
166     sprintf "rpm -q --qf '[%%{FILENAMES} %%{FILEFLAGS:fflags} %%{FILEMODES}\\n]' -p %s"
167       pkg in
168   let lines = run_command_get_lines cmd in
169
170   let files =
171     filter_map (
172       fun line ->
173         match string_split " " line with
174         | [filename; flags; mode] ->
175             let test_flag = String.contains flags in
176             let mode = int_of_string mode in
177             if test_flag 'd' then None  (* ignore documentation *)
178             else
179               Some (filename, {
180                       ft_dir = mode land 0o40000 <> 0;
181                       ft_ghost = test_flag 'g'; ft_config = test_flag 'c';
182                       ft_mode = mode;
183                     })
184         | _ ->
185             eprintf "febootstrap: bad output from rpm command: '%s'" line;
186             exit 1
187     ) lines in
188
189   (* I've never understood why the base packages like 'filesystem' don't
190    * contain any /dev nodes at all.  This leaves every program that
191    * bootstraps RPMs to create a varying set of device nodes themselves.
192    * This collection was copied from mock/backend.py.
193    *)
194   let files =
195     let b = Filename.basename pkg in
196     if string_prefix "filesystem-" b then (
197       let dirs = [ "/proc"; "/sys"; "/dev"; "/dev/pts"; "/dev/shm";
198                    "/dev/mapper" ] in
199       let dirs =
200         List.map (fun name ->
201                     name, { ft_dir = true; ft_ghost = false;
202                             ft_config = false; ft_mode = 0o40755 }) dirs in
203       let devs = [ "/dev/null"; "/dev/full"; "/dev/zero"; "/dev/random";
204                    "/dev/urandom"; "/dev/tty"; "/dev/console";
205                    "/dev/ptmx"; "/dev/stdin"; "/dev/stdout"; "/dev/stderr" ] in
206       (* No need to set the mode because these will go into hostfiles. *)
207       let devs =
208         List.map (fun name ->
209                     name, { ft_dir = false; ft_ghost = false;
210                             ft_config = false; ft_mode = 0o644 }) devs in
211       dirs @ devs @ files
212     ) else files in
213
214   files
215
216 let yum_rpm_get_file_from_package pkg file =
217   debug "extracting %s from %s ..." file (Filename.basename pkg);
218
219   let outfile = tmpdir // file in
220   let cmd =
221     sprintf "rpm2cpio %s | (cd %s && cpio --quiet -id .%s)"
222       (Filename.quote pkg) (Filename.quote tmpdir) (Filename.quote file) in
223   run_command cmd;
224   outfile
225
226 let () =
227   let ph = {
228     ph_detect = yum_rpm_detect;
229     ph_resolve_dependencies_and_download =
230       yum_rpm_resolve_dependencies_and_download;
231     ph_list_files = yum_rpm_list_files;
232     ph_get_file_from_package = yum_rpm_get_file_from_package;
233   } in
234   register_package_handler "yum-rpm" ph