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.
 
 
 

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