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