helper: Print /modules when verbose >= 2
[febootstrap.git] / src / febootstrap_pacman.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 (* ArchLinux 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 pacman_detect () =
32   file_exists "/etc/arch-release" &&
33     Config.pacman <> "no"
34
35 let pacman_init () =
36   if use_installed then
37     failwith "pacman driver doesn't support --use-installed"
38
39 let pacman_resolve_dependencies_and_download names =
40   let cmd =
41     sprintf "(for p in %s; do pactree -u $p; done) | awk '{print $1}' | sort -u"
42       (String.concat " " (List.map Filename.quote names)) in
43   let pkgs = run_command_get_lines cmd in
44
45   (* Exclude packages matching [--exclude] regexps on the command line. *)
46   let pkgs =
47     List.filter (
48       fun name ->
49         not (List.exists (fun re -> Str.string_match re name 0) excludes)
50     ) pkgs in
51
52   (* Download the packages. I could use wget `pacman -Sp`, but this
53    * narrows the pacman -Sy window
54    *)
55   List.iter (
56     fun pkg ->
57       let cmd =
58         sprintf "umask 0000; cd %s && mkdir -p var/lib/pacman && fakeroot pacman -Syw --noconfirm --cachedir=$(pwd) --root=$(pwd) %s"
59         (Filename.quote tmpdir)
60         pkg in
61       if Sys.command cmd <> 0 then (
62           (* The package is not in the main repos, check the aur *)
63           let cmd =
64             sprintf "umask 0000; cd %s && wget http://aur.archlinux.org/packages/%s/%s.tar.gz && tar xf %s.tar.gz && cd %s && makepkg && mv %s-*.pkg.tar.xz %s"
65             (Filename.quote tmpdir)
66             pkg
67             pkg
68             pkg
69             pkg
70             pkg
71             (Filename.quote tmpdir) in
72           run_command cmd;
73         )
74   ) pkgs;
75
76   List.sort compare pkgs
77
78 let pacman_list_files pkg =
79   debug "unpacking %s ..." pkg;
80
81   (* We actually need to extract the file in order to get the
82    * information about modes etc.
83    *)
84   let pkgdir = tmpdir // pkg ^ ".d" in
85   mkdir pkgdir 0o755;
86   let cmd =
87     sprintf "ls -1 %s/%s-*.pkg.* | awk '/\\/%s-[^/-]*-[^/-]*-[^/-]*$/ { print $0 }'"
88       tmpdir pkg pkg in
89   let pkgfile = List.hd (run_command_get_lines cmd) in
90     let cmd = sprintf "umask 0000; fakeroot tar -xf %s -C %s"
91               (Filename.quote pkgfile) (Filename.quote pkgdir) in
92   run_command cmd;
93
94   let cmd = sprintf "cd %s && find ." pkgdir in
95   let lines = run_command_get_lines cmd in
96
97   let files = List.map (
98     fun path ->
99       assert (path.[0] = '.');
100       (* No leading '.' *)
101       let path =
102         if path = "." then "/"
103         else String.sub path 1 (String.length path - 1) in
104
105       (* Find out what it is and get the canonical filename. *)
106       let statbuf = lstat (pkgdir // path) in
107       let is_dir = statbuf.st_kind = S_DIR in
108
109       (* No per-file metadata like in RPM, but we can synthesize it
110        * from the path.
111        *)
112       let config = statbuf.st_kind = S_REG && string_prefix "/etc/" path in
113
114       let mode = statbuf.st_perm in
115
116       (path, { ft_dir = is_dir; ft_config = config; ft_mode = mode;
117                ft_ghost = false; ft_size = statbuf.st_size })
118   ) lines in
119
120   files
121
122 (* Easy because we already unpacked the archive above. *)
123 let pacman_get_file_from_package pkg file =
124   tmpdir // pkg ^ ".d" // file
125
126 let () =
127   let ph = {
128     ph_detect = pacman_detect;
129     ph_init = pacman_init;
130     ph_resolve_dependencies_and_download =
131       pacman_resolve_dependencies_and_download;
132     ph_list_files = pacman_list_files;
133     ph_get_file_from_package = pacman_get_file_from_package;
134   } in
135   register_package_handler "pacman" ph