helper: Print /modules when verbose >= 2
[febootstrap.git] / src / 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 verbose = %d
44
45 if verbose:
46     print \"febootstrap_yum_rpm: running python code to query yum and resolve deps\"
47
48 yb = yum.YumBase ()
49 yb.preconf.debuglevel = verbose
50 yb.preconf.errorlevel = verbose
51 if %s:
52     yb.preconf.fn = %S
53 yb.setCacheDir ()
54
55 if verbose:
56     print \"febootstrap_yum_rpm: looking up the base packages from the command line\"
57 deps = dict ()
58 pkgs = yb.pkgSack.returnPackages (patterns=sys.argv[1:])
59 for pkg in pkgs:
60     deps[pkg] = False
61
62 if verbose:
63     print \"febootstrap_yum_rpm: recursively finding all the dependencies\"
64 stable = False
65 while not stable:
66     stable = True
67     for pkg in deps.keys():
68         if deps[pkg] == False:
69             deps[pkg] = []
70             stable = False
71             if verbose:
72                 print (\"febootstrap_yum_rpm: examining deps of %%s\" %%
73                        pkg.name)
74             for r in pkg.requires:
75                 ps = yb.whatProvides (r[0], r[1], r[2])
76                 best = yb._bestPackageFromList (ps.returnPackages ())
77                 if best.name != pkg.name:
78                     deps[pkg].append (best)
79                     if not deps.has_key (best):
80                         deps[best] = False
81             deps[pkg] = yum.misc.unique (deps[pkg])
82
83 # Write it to a file because yum spews garbage on stdout.
84 f = open (%S, \"w\")
85 for pkg in deps.keys ():
86     f.write (\"%%s %%s %%s %%s %%s\\n\" %%
87              (pkg.name, pkg.epoch, pkg.version, pkg.release, pkg.arch))
88 f.close ()
89
90 if verbose:
91     print \"febootstrap_yum_rpm: finished python code\"
92 "
93     (if verbose then 1 else 0)
94     (match yum_config with None -> "False" | Some _ -> "True")
95     (match yum_config with None -> "" | Some filename -> filename)
96     tmpfile in
97   run_python py names;
98   let chan = open_in tmpfile in
99   let lines = input_all_lines chan in
100   close_in chan;
101
102   (* Get fields. *)
103   let pkgs =
104     List.map (
105       fun line ->
106         match string_split " " line with
107         | [name; epoch; version; release; arch] ->
108             name, int_of_string epoch, version, release, arch
109         | _ ->
110             eprintf "febootstrap: bad output from python script: '%s'" line;
111             exit 1
112     ) lines in
113
114   (* Something of a hack for x86_64: exclude all i[3456]86 packages. *)
115   let pkgs =
116     if Config.host_cpu = "x86_64" then (
117       List.filter (
118         function (_, _, _, _, ("i386"|"i486"|"i586"|"i686")) -> false
119         | _ -> true
120       ) pkgs
121     )
122     else pkgs in
123
124   (* Drop the kernel package to save time. *)
125   let pkgs =
126     List.filter (function ("kernel",_,_,_,_) -> false | _ -> true) pkgs in
127
128   (* Exclude packages matching [--exclude] regexps on the command line. *)
129   let pkgs =
130     List.filter (
131       fun (name, _, _, _, _) ->
132         not (List.exists (fun re -> Str.string_match re name 0) excludes)
133     ) pkgs in
134
135   (* Sort the list of packages, and remove duplicates (by name).
136    * XXX This is not quite right: we really want to keep the latest
137    * package if duplicates are found, but that would require a full
138    * version compare function.
139    *)
140   let pkgs = List.sort (fun a b -> compare b a) pkgs in
141   let pkgs =
142     let cmp (name1, _, _, _, _) (name2, _, _, _, _) = compare name1 name2 in
143     uniq ~cmp pkgs in
144   let pkgs = List.sort compare pkgs in
145
146   (* Construct package names. *)
147   let pkgnames = List.map (
148     function
149     | name, 0, version, release, arch ->
150         sprintf "%s-%s-%s.%s" name version release arch
151     | name, epoch, version, release, arch ->
152         sprintf "%d:%s-%s-%s.%s" epoch name version release arch
153   ) pkgs in
154
155   if pkgnames = [] then (
156     eprintf "febootstrap: yum-rpm: error: no packages to download\n";
157     exit 1
158   );
159
160   let cmd = sprintf "yumdownloader%s%s --destdir %s %s"
161     (if verbose then "" else " --quiet")
162     (match yum_config with None -> ""
163      | Some filename -> sprintf " -c %s" filename)
164     (Filename.quote tmpdir)
165     (String.concat " " (List.map Filename.quote pkgnames)) in
166   run_command cmd;
167
168   (* Return list of package filenames. *)
169   List.map (
170     (* yumdownloader doesn't include epoch in the filename *)
171     fun (name, _, version, release, arch) ->
172       sprintf "%s/%s-%s-%s.%s.rpm" tmpdir name version release arch
173   ) pkgs
174
175 let rec yum_rpm_list_files pkg =
176   (* Run rpm -qlp with some extra magic. *)
177   let cmd =
178     sprintf "rpm -q --qf '[%%{FILENAMES} %%{FILEFLAGS:fflags} %%{FILEMODES} %%{FILESIZES}\\n]' -p %s"
179       pkg in
180   let lines = run_command_get_lines cmd in
181
182   let files =
183     filter_map (
184       fun line ->
185         match string_split " " line with
186         | [filename; flags; mode; size] ->
187             let test_flag = String.contains flags in
188             let mode = int_of_string mode in
189             let size = int_of_string size in
190             if test_flag 'd' then None  (* ignore documentation *)
191             else
192               Some (filename, {
193                       ft_dir = mode land 0o40000 <> 0;
194                       ft_ghost = test_flag 'g'; ft_config = test_flag 'c';
195                       ft_mode = mode; ft_size = size;
196                     })
197         | _ ->
198             eprintf "febootstrap: bad output from rpm command: '%s'" line;
199             exit 1
200     ) lines in
201
202   (* I've never understood why the base packages like 'filesystem' don't
203    * contain any /dev nodes at all.  This leaves every program that
204    * bootstraps RPMs to create a varying set of device nodes themselves.
205    * This collection was copied from mock/backend.py.
206    *)
207   let files =
208     let b = Filename.basename pkg in
209     if string_prefix "filesystem-" b then (
210       let dirs = [ "/proc"; "/sys"; "/dev"; "/dev/pts"; "/dev/shm";
211                    "/dev/mapper" ] in
212       let dirs =
213         List.map (fun name ->
214                     name, { ft_dir = true; ft_ghost = false;
215                             ft_config = false; ft_mode = 0o40755;
216                             ft_size = 0 }) dirs in
217       let devs = [ "/dev/null"; "/dev/full"; "/dev/zero"; "/dev/random";
218                    "/dev/urandom"; "/dev/tty"; "/dev/console";
219                    "/dev/ptmx"; "/dev/stdin"; "/dev/stdout"; "/dev/stderr" ] in
220       (* No need to set the mode because these will go into hostfiles. *)
221       let devs =
222         List.map (fun name ->
223                     name, { ft_dir = false; ft_ghost = false;
224                             ft_config = false; ft_mode = 0o644;
225                             ft_size = 0 }) devs in
226       dirs @ devs @ files
227     ) else files in
228
229   files
230
231 let yum_rpm_get_file_from_package pkg file =
232   debug "extracting %s from %s ..." file (Filename.basename pkg);
233
234   let outfile = tmpdir // file in
235   let cmd =
236     sprintf "umask 0000; rpm2cpio %s | (cd %s && cpio --quiet -id .%s)"
237       (Filename.quote pkg) (Filename.quote tmpdir) (Filename.quote file) in
238   run_command cmd;
239   outfile
240
241 let () =
242   let ph = {
243     ph_detect = yum_rpm_detect;
244     ph_resolve_dependencies_and_download =
245       yum_rpm_resolve_dependencies_and_download;
246     ph_list_files = yum_rpm_list_files;
247     ph_get_file_from_package = yum_rpm_get_file_from_package;
248   } in
249   register_package_handler "yum-rpm" ph