You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

670 lines
16 KiB

  1. #!/usr/bin/perl
  2. # Debian task selector, mark II.
  3. # Copyright 2004-2011 by Joey Hess <joeyh@debian.org>.
  4. # Licensed under the GPL, version 2 or higher.
  5. use Locale::gettext;
  6. use Getopt::Long;
  7. use warnings;
  8. use strict;
  9. textdomain('tasksel');
  10. my $debconf_helper="/usr/lib/tasksel/tasksel-debconf";
  11. my $testdir="/usr/lib/tasksel/tests";
  12. my $packagesdir="/usr/lib/tasksel/packages";
  13. my $descdir="/usr/share/tasksel/descs";
  14. my $localdescdir="/usr/local/share/tasksel/descs";
  15. my $statusfile="/var/lib/dpkg/status";
  16. my $infodir="/usr/lib/tasksel/info";
  17. my $testmode=0;
  18. my $taskpackageprefix="task-";
  19. sub warning {
  20. print STDERR "tasksel: @_\n";
  21. }
  22. sub error {
  23. print STDERR "tasksel: @_\n";
  24. exit 1;
  25. }
  26. # Run a shell command except in test mode, and returns its exit code.
  27. # Prints the command in test mode. Parameters should be pre-split for
  28. # system.
  29. sub run {
  30. if ($testmode) {
  31. print join(" ", @_)."\n";
  32. return 0;
  33. }
  34. else {
  35. return system(@_) >> 8;
  36. }
  37. }
  38. # A list of all available task desc files.
  39. sub list_task_descs {
  40. return glob("$descdir/*.desc"), glob("$localdescdir/*.desc");
  41. }
  42. # Returns a list of hashes; hash values are arrays for multi-line fields.
  43. sub read_task_desc {
  44. my $desc=shift;
  45. my @ret;
  46. open (DESC, "<$desc") || die "read $desc\: $!";
  47. local $/="\n\n";
  48. while (<DESC>) {
  49. my %data;
  50. my @lines=split("\n");
  51. while (@lines) {
  52. my $line=shift(@lines);
  53. if ($line=~/^([^ ]+):(?: (.*))?/) {
  54. my ($key, $value)=($1, $2);
  55. $key=lc($key);
  56. if (@lines && $lines[0] =~ /^\s+/) {
  57. # multi-line field
  58. my @values;
  59. if (defined $value && length $value) {
  60. push @values, $value;
  61. }
  62. while (@lines && $lines[0] =~ /^\s+(.*)/) {
  63. push @values, $1;
  64. shift @lines;
  65. }
  66. $data{$key}=[@values];
  67. }
  68. else {
  69. $data{$key}=$value;
  70. }
  71. }
  72. else {
  73. warning "parse error in stanza $. of $desc";
  74. }
  75. }
  76. if (%data) {
  77. $data{relevance}=5 unless exists $data{relevance};
  78. push @ret, \%data;
  79. }
  80. }
  81. close DESC;
  82. return @ret;
  83. }
  84. # Loads info for all tasks, and returns a set of task structures.
  85. sub all_tasks {
  86. my %seen;
  87. grep { $seen{$_->{task}}++; $seen{$_->{task}} < 2 }
  88. map { read_task_desc($_) } list_task_descs();
  89. }
  90. # Returns a list of all available packages.
  91. sub list_avail {
  92. my @list;
  93. # Might be better to use the perl apt bindings, but they are not
  94. # currently in base.
  95. open (AVAIL, "apt-cache dumpavail|");
  96. local $_;
  97. while (<AVAIL>) {
  98. chomp;
  99. if (/^Package: (.*)/) {
  100. push @list, $1;
  101. }
  102. }
  103. close AVAIL;
  104. return @list;
  105. }
  106. # Returns a list of all installed packages.
  107. sub list_installed {
  108. my @list;
  109. open (LIST, q{LANG=C dpkg-query -W -f='${Package} ${Status}\n' |});
  110. while (<LIST>) {
  111. if (/^([^ ]+) .* installed$/m) {
  112. push @list, $1;
  113. }
  114. }
  115. close LIST;
  116. return @list;
  117. }
  118. my %avail_pkgs;
  119. # Given a package name, checks to see if it's available. Memoised.
  120. sub package_avail {
  121. my $package=shift;
  122. if (! %avail_pkgs) {
  123. foreach my $pkg (list_avail()) {
  124. $avail_pkgs{$pkg} = 1;
  125. }
  126. }
  127. return $avail_pkgs{$package} || package_installed($package);
  128. }
  129. my %installed_pkgs;
  130. # Given a package name, checks to see if it's installed. Memoised.
  131. sub package_installed {
  132. my $package=shift;
  133. if (! %installed_pkgs) {
  134. foreach my $pkg (list_installed()) {
  135. $installed_pkgs{$pkg} = 1;
  136. }
  137. }
  138. return $installed_pkgs{$package};
  139. }
  140. # Given a task hash, checks if its key packages are available.
  141. sub task_avail {
  142. local $_;
  143. my $task=shift;
  144. if (! ref $task->{key}) {
  145. return 1;
  146. }
  147. else {
  148. foreach my $pkg (@{$task->{key}}) {
  149. if (! package_avail($pkg)) {
  150. return 0;
  151. }
  152. }
  153. return 1;
  154. }
  155. }
  156. # Given a task hash, checks to see if it is already installed.
  157. # (All of its key packages must be installed.)
  158. sub task_installed {
  159. local $_;
  160. my $task=shift;
  161. if (! ref $task->{key}) {
  162. return 0; # can't tell with no key packages
  163. }
  164. else {
  165. foreach my $pkg (@{$task->{key}}) {
  166. if (! package_installed($pkg)) {
  167. return 0;
  168. }
  169. }
  170. return 1;
  171. }
  172. }
  173. # Given task hash, returns a list of all available packages in the task.
  174. # If the aptitude_tasks parameter is true, then it does not expand tasks
  175. # that aptitude knows about, and just returns aptitude task syntax for
  176. # those.
  177. sub task_packages {
  178. my $task=shift;
  179. my $aptitude_tasks=shift;
  180. my %list;
  181. # key packages are always included
  182. if (ref $task->{key}) {
  183. map { $list{$_}=1 } @{$task->{key}};
  184. }
  185. if (! defined $task->{packages}) {
  186. # only key
  187. }
  188. elsif ($task->{packages} eq 'task-fields') {
  189. # task-fields method is built-in for speed and to support
  190. # aptitude task definitions
  191. if ($aptitude_tasks) {
  192. return '~t^'.$task->{task}.'$';
  193. }
  194. else {
  195. local $/="\n\n";
  196. open (AVAIL, "apt-cache dumpavail|");
  197. while (<AVAIL>) {
  198. if (/^Task: (.*)/m) {
  199. my @tasks=split(", ", $1);
  200. if (grep { $_ eq $task->{task} } @tasks) {
  201. $list{$1}=1 if /^Package: (.*)/m;
  202. }
  203. }
  204. }
  205. close AVAIL;
  206. }
  207. }
  208. elsif ($task->{packages} eq 'standard') {
  209. # standard method is built in since it cannot easily be
  210. # implemented externally.
  211. return "~pstandard", "~prequired", "~pimportant";
  212. }
  213. elsif ($task->{packages} eq 'manual') {
  214. # manual package selection is a special case
  215. return;
  216. }
  217. else {
  218. # external method
  219. my ($method, @params);
  220. if (ref $task->{packages}) {
  221. @params=@{$task->{packages}};
  222. $method=shift @params;
  223. }
  224. else {
  225. $method=$task->{packages};
  226. }
  227. map { $list{$_}=1 }
  228. grep { package_avail($_) }
  229. split(' ', `$packagesdir/$method $task->{task} @params`);
  230. }
  231. return keys %list;
  232. }
  233. # Given a task hash, runs any test program specified in its data, and sets
  234. # the _display and _install fields to 1 or 0 depending on its result.
  235. sub task_test {
  236. my $task=shift;
  237. my $new_install=shift;
  238. $task->{_display} = shift; # default
  239. $task->{_install} = shift; # default
  240. $ENV{NEW_INSTALL}=$new_install if defined $new_install;
  241. foreach my $test (grep /^test-.*/, keys %$task) {
  242. $test=~s/^test-//;
  243. if (-x "$testdir/$test") {
  244. my $ret=system("$testdir/$test", $task->{task}, split " ", $task->{"test-$test"}) >> 8;
  245. if ($ret == 0) {
  246. $task->{_display} = 0;
  247. $task->{_install} = 1;
  248. }
  249. elsif ($ret == 1) {
  250. $task->{_display} = 0;
  251. $task->{_install} = 0;
  252. }
  253. elsif ($ret == 2) {
  254. $task->{_display} = 1;
  255. $task->{_install} = 1;
  256. }
  257. elsif ($ret == 3) {
  258. $task->{_display} = 1;
  259. $task->{_install} = 0;
  260. }
  261. }
  262. }
  263. delete $ENV{NEW_INSTALL};
  264. return $task;
  265. }
  266. # Hides a task and marks it not to be installed if it enhances other
  267. # tasks.
  268. sub hide_enhancing_tasks {
  269. my $task=shift;
  270. if (exists $task->{enhances} && length $task->{enhances}) {
  271. $task->{_display} = 0;
  272. $task->{_install} = 0;
  273. }
  274. return $task;
  275. }
  276. # Looks up the descriptions of a set of tasks, returning a new list
  277. # with the shortdesc fields filled in.
  278. sub getdescriptions {
  279. my @tasks=@_;
  280. # If the task has a description field in the task desc file,
  281. # just use it, looking up a translation in gettext.
  282. @tasks = map {
  283. if (defined $_->{description}) {
  284. $_->{shortdesc}=dgettext("debian-tasks", $_->{description}->[0]);
  285. }
  286. $_;
  287. } @tasks;
  288. # Otherwise, a more expensive apt-cache query is done,
  289. # to use the descriptions of task packages.
  290. my @todo = grep { ! defined $_->{shortdesc} } @tasks;
  291. if (@todo) {
  292. open(APT_CACHE, "apt-cache show ".join(" ", map { $taskpackageprefix.$_->{task} } @todo)." |") || die "apt-cache show: $!";
  293. local $/="\n\n";
  294. while (<APT_CACHE>) {
  295. my ($name)=/^Package: $taskpackageprefix(.*)$/m;
  296. my ($description)=/^Description-(?:[a-z][a-z](?:_[A-Z][A-Z])?): (.*)$/m;
  297. ($description)=/^Description: (.*)$/m
  298. unless defined $description;
  299. if (defined $name && defined $description) {
  300. @tasks = map {
  301. if ($_->{task} eq $name) {
  302. $_->{shortdesc}=$description;
  303. }
  304. $_;
  305. } @tasks;
  306. }
  307. }
  308. close APT_CACHE;
  309. }
  310. return @tasks;
  311. }
  312. # Converts a list of tasks into a debconf list of the task short
  313. # descriptions.
  314. sub task_to_debconf {
  315. join ", ", map { my $d=$_->{shortdesc}; $d=~s/,/\\,/g; $d } getdescriptions(@_);
  316. }
  317. # Converts a list of tasks into a debconf list of the task names.
  318. sub task_to_debconf_C {
  319. join ", ", map { $_->{task} } @_;
  320. }
  321. # Given a first parameter that is a string listing task names, and then a
  322. # list of task hashes, returns a list of hashes for all the tasks
  323. # in the list.
  324. sub list_to_tasks {
  325. my $list=shift;
  326. my %lookup = map { $_->{task} => $_ } @_;
  327. return grep { defined } map { $lookup{$_} } split /[, ]+/, $list;
  328. }
  329. # Orders a list of tasks for display.
  330. sub order_for_display {
  331. sort {
  332. $b->{relevance} <=> $a->{relevance}
  333. || 0 ||
  334. $a->{section} cmp $b->{section}
  335. || 0 ||
  336. $a->{task} cmp $b->{task}
  337. } @_;
  338. }
  339. # Given a set of tasks and a name, returns the one with that name.
  340. sub name_to_task {
  341. my $name=shift;
  342. return (grep { $_->{task} eq $name } @_)[0];
  343. }
  344. sub task_script {
  345. my $task=shift;
  346. my $script=shift;
  347. my $path="$infodir/$task.$script";
  348. if (-e $path && -x _) {
  349. my $ret=run($path);
  350. if ($ret != 0) {
  351. warning("$path exited with nonzero code $ret");
  352. return 0;
  353. }
  354. }
  355. return 1;
  356. }
  357. sub usage {
  358. print STDERR gettext(q{Usage:
  359. tasksel install <task>
  360. tasksel remove <task>
  361. tasksel [options]
  362. -t, --test test mode; don't really do anything
  363. --new-install automatically install some tasks
  364. --list-tasks list tasks that would be displayed and exit
  365. --task-packages list available packages in a task
  366. --task-desc returns the description of a task
  367. });
  368. }
  369. # Process command line options and return them in a hash.
  370. sub getopts {
  371. my %ret;
  372. Getopt::Long::Configure ("bundling");
  373. if (! GetOptions(\%ret, "test|t", "new-install", "list-tasks",
  374. "task-packages=s@", "task-desc=s",
  375. "debconf-apt-progress=s")) {
  376. usage();
  377. exit(1);
  378. }
  379. # Special case apt-like syntax.
  380. if (@ARGV && $ARGV[0] eq "install") {
  381. shift @ARGV;
  382. $ret{install} = shift @ARGV;
  383. }
  384. if (@ARGV && $ARGV[0] eq "remove") {
  385. shift @ARGV;
  386. $ret{remove} = shift @ARGV;
  387. }
  388. if (@ARGV) {
  389. usage();
  390. exit 1;
  391. }
  392. $testmode=1 if $ret{test}; # set global
  393. return %ret;
  394. }
  395. sub main {
  396. my %options=getopts();
  397. my @tasks_remove;
  398. my @tasks_install;
  399. # Options that output stuff and don't need a full processed list of
  400. # tasks.
  401. if (exists $options{"task-packages"}) {
  402. my @tasks=all_tasks();
  403. foreach my $taskname (@{$options{"task-packages"}}) {
  404. my $task=name_to_task($taskname, @tasks);
  405. if ($task) {
  406. print "$_\n" foreach task_packages($task);
  407. }
  408. }
  409. exit(0);
  410. }
  411. elsif ($options{"task-desc"}) {
  412. my $task=name_to_task($options{"task-desc"}, all_tasks());
  413. if ($task) {
  414. my $extdesc=join(" ", @{$task->{description}}[1..$#{$task->{description}}]);
  415. print dgettext("debian-tasks", $extdesc)."\n";
  416. exit(0);
  417. }
  418. else {
  419. exit(1);
  420. }
  421. }
  422. # This is relatively expensive, get the full list of available tasks and
  423. # mark them.
  424. my @tasks=map { hide_enhancing_tasks($_) } map { task_test($_, $options{"new-install"}, 1, 0) }
  425. grep { task_avail($_) } all_tasks();
  426. if ($options{"list-tasks"}) {
  427. map { $_->{_installed} = task_installed($_) } @tasks;
  428. @tasks=getdescriptions(@tasks);
  429. print "".($_->{_installed} ? "i" : "u")." ".$_->{task}."\t".$_->{shortdesc}."\n"
  430. foreach order_for_display(grep { $_->{_display} } @tasks);
  431. exit(0);
  432. }
  433. if (! $options{"new-install"}) {
  434. # Don't install hidden tasks if this is not a new install.
  435. map { $_->{_install} = 0 } grep { $_->{_display} == 0 } @tasks;
  436. }
  437. if ($options{"install"}) {
  438. my $task=name_to_task($options{"install"}, @tasks);
  439. $task->{_install} = 1 if $task;
  440. }
  441. if ($options{"remove"}) {
  442. my $task=name_to_task($options{"remove"}, @tasks);
  443. push @tasks_remove, $task;
  444. }
  445. # The interactive bit.
  446. my $interactive=0;
  447. my @list = order_for_display(grep { $_->{_display} == 1 } @tasks);
  448. if (@list && ! $options{install} && ! $options{remove}) {
  449. $interactive=1;
  450. if (! $options{"new-install"}) {
  451. # Find tasks that are already installed.
  452. map { $_->{_installed} = task_installed($_) } @list;
  453. # Don't install new tasks unless manually selected.
  454. map { $_->{_install} = 0 } @list;
  455. }
  456. else {
  457. # Assume that no tasks are installed, to ensure
  458. # that complete tasks get installed on new
  459. # installs.
  460. map { $_->{_installed} = 0 } @list;
  461. }
  462. my $question="tasksel/tasks";
  463. if ($options{"new-install"}) {
  464. $question="tasksel/first";
  465. }
  466. my @default = grep { $_->{_display} == 1 && ($_->{_install} == 1 || $_->{_installed} == 1) } @tasks;
  467. my $tmpfile=`tempfile`;
  468. chomp $tmpfile;
  469. my $ret=system($debconf_helper, $tmpfile,
  470. task_to_debconf_C(@list),
  471. task_to_debconf(@list),
  472. task_to_debconf_C(@default),
  473. $question) >> 8;
  474. if ($ret == 30) {
  475. exit 10; # back up
  476. }
  477. elsif ($ret != 0) {
  478. error "debconf failed to run";
  479. }
  480. open(IN, "<$tmpfile");
  481. $ret=<IN>;
  482. if (! defined $ret) {
  483. die "tasksel canceled\n";
  484. }
  485. chomp $ret;
  486. close IN;
  487. unlink $tmpfile;
  488. # Set _install flags based on user selection.
  489. map { $_->{_install} = 0 } @list;
  490. foreach my $task (list_to_tasks($ret, @tasks)) {
  491. if (! $task->{_installed}) {
  492. $task->{_install} = 1;
  493. }
  494. $task->{_selected} = 1;
  495. }
  496. foreach my $task (@list) {
  497. if (! $task->{_selected} && $task->{_installed}) {
  498. push @tasks_remove, $task;
  499. }
  500. }
  501. }
  502. # If an enhancing task is already marked for
  503. # install, probably by preseeding, mark the tasks
  504. # it enhances for install.
  505. foreach my $task (grep { $_->{_install} && exists $_->{enhances} &&
  506. length $_->{enhances} } @tasks) {
  507. map { $_->{_install}=1 } list_to_tasks($task->{enhances}, @tasks);
  508. }
  509. # Select enhancing tasks for install.
  510. # XXX FIXME ugly hack -- loop until enhances settle to handle
  511. # chained enhances. This is ugly and could loop forever if
  512. # there's a cycle.
  513. my $enhances_needswork=1;
  514. my %tested;
  515. while ($enhances_needswork) {
  516. $enhances_needswork=0;
  517. foreach my $task (grep { ! $_->{_install} && exists $_->{enhances} &&
  518. length $_->{enhances} } @tasks) {
  519. my %tasknames = map { $_->{task} => $_ } @tasks;
  520. my @deps=map { $tasknames{$_} } split ", ", $task->{enhances};
  521. if (grep { ! defined $_ } @deps) {
  522. # task enhances an unavailable or
  523. # uninstallable task
  524. next;
  525. }
  526. if (@deps) {
  527. my $orig_state=$task->{_install};
  528. # Mark enhancing tasks for install if their
  529. # dependencies are met and their test fields
  530. # mark them for install.
  531. if (! exists $tested{$task->{task}}) {
  532. $ENV{TESTING_ENHANCER}=1;
  533. task_test($task, $options{"new-install"}, 0, 1);
  534. delete $ENV{TESTING_ENHANCER};
  535. $tested{$task->{task}}=$task->{_install};
  536. }
  537. else {
  538. $task->{_install}=$tested{$task->{task}};
  539. }
  540. foreach my $dep (@deps) {
  541. if (! $dep->{_install}) {
  542. $task->{_install} = 0;
  543. }
  544. }
  545. if ($task->{_install} != $orig_state) {
  546. $enhances_needswork=1;
  547. }
  548. }
  549. }
  550. }
  551. # Add tasks to install and see if any selected task requires manual
  552. # selection.
  553. my $manual_selection=0;
  554. foreach my $task (grep { $_->{_install} } @tasks) {
  555. push @tasks_install, $task;
  556. if (defined $task->{packages} && $task->{packages} eq 'manual') {
  557. $manual_selection=1;
  558. }
  559. }
  560. my @aptitude;
  561. if ($manual_selection) {
  562. # Manaul selection and task installs, as best
  563. # aptitude can do it currently. Disables use of
  564. # debconf-apt-progress.
  565. @aptitude="aptitude";
  566. }
  567. elsif (-x "/usr/bin/debconf-apt-progress") {
  568. @aptitude="debconf-apt-progress";
  569. push @aptitude, split(' ', $options{'debconf-apt-progress'})
  570. if exists $options{'debconf-apt-progress'};
  571. push @aptitude, qw{-- aptitude -q};
  572. }
  573. else {
  574. @aptitude="aptitude";
  575. }
  576. # And finally, act on selected tasks.
  577. if (@tasks_install || @tasks_remove || $manual_selection) {
  578. my @args;
  579. foreach my $task (@tasks_remove) {
  580. push @args, map { "$_-" } task_packages($task, 0);
  581. task_script($task->{task}, "prerm");
  582. }
  583. foreach my $task (@tasks_install) {
  584. push @args, task_packages($task, 1);
  585. task_script($task->{task}, "preinst");
  586. }
  587. # If the user selected no other tasks and manual package
  588. # selection, run aptitude w/o the --visual-preview parameter.
  589. if (! @args && $manual_selection) {
  590. my $ret=run("aptitude");
  591. if ($ret != 0) {
  592. error gettext("aptitude failed")." ($ret)";
  593. }
  594. }
  595. else {
  596. if ($manual_selection) {
  597. unshift @args, "--visual-preview";
  598. }
  599. my $ret=run(@aptitude, "-y", "install", @args);
  600. if ($ret != 0) {
  601. error gettext("aptitude failed")." ($ret)";
  602. }
  603. }
  604. foreach my $task (@tasks_remove) {
  605. task_script($task->{task}, "postrm");
  606. }
  607. foreach my $task (@tasks_install) {
  608. task_script($task->{task}, "postinst");
  609. }
  610. }
  611. }
  612. main();