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.
 
 
 

556 lines
13 KiB

  1. #!/usr/bin/perl
  2. # Debian task selector, mark II.
  3. # Copyright 2004 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. sub warning {
  17. print STDERR "tasksel: @_\n";
  18. }
  19. sub error {
  20. print STDERR "tasksel: @_\n";
  21. exit 1;
  22. }
  23. # A list of all available task desc files.
  24. sub list_task_descs {
  25. return glob("$descdir/*.desc"), glob("$localdescdir/*.desc");
  26. }
  27. # Returns a list of hashes; hash values are arrays for multi-line fields.
  28. sub read_task_desc {
  29. my $desc=shift;
  30. my @ret;
  31. open (DESC, "<$desc") || die "read $desc\: $!";
  32. local $/="\n\n";
  33. while (<DESC>) {
  34. my %data;
  35. my @lines=split("\n");
  36. while (@lines) {
  37. my $line=shift(@lines);
  38. if ($line=~/^([^ ]+):(?: (.*))?/) {
  39. my ($key, $value)=($1, $2);
  40. $key=lc($key);
  41. if (@lines && $lines[0] =~ /^\s+/) {
  42. # multi-line field
  43. my @values;
  44. if (defined $value && length $value) {
  45. push @values, $value;
  46. }
  47. while (@lines && $lines[0] =~ /^\s+(.*)/) {
  48. push @values, $1;
  49. shift @lines;
  50. }
  51. $data{$key}=[@values];
  52. }
  53. else {
  54. $data{$key}=$value;
  55. }
  56. }
  57. else {
  58. warning "parse error in stanza $. of $desc";
  59. }
  60. }
  61. if (%data) {
  62. $data{relevance}=5 unless exists $data{relevance};
  63. $data{shortdesc}=$data{description}->[0];
  64. $data{shortdesctrans}=dgettext("debian-tasks", $data{shortdesc});
  65. push @ret, \%data;
  66. }
  67. }
  68. close DESC;
  69. return @ret;
  70. }
  71. # Loads info for all tasks, and returns a set of task structures.
  72. sub all_tasks {
  73. my %seen;
  74. grep { $seen{$_->{task}}++; $seen{$_->{task}} < 2 }
  75. map { read_task_desc($_) } list_task_descs();
  76. }
  77. # Returns a list of all available packages.
  78. sub list_avail {
  79. my @list;
  80. # Might be better to use the perl apt bindings, but they are not
  81. # currently in base.
  82. open (AVAIL, "apt-cache dumpavail|");
  83. local $_;
  84. while (<AVAIL>) {
  85. chomp;
  86. if (/^Package: (.*)/) {
  87. push @list, $1;
  88. }
  89. }
  90. close AVAIL;
  91. return @list;
  92. }
  93. # Returns a list of all installed packages.
  94. sub list_installed {
  95. my @list;
  96. local $/="\n\n";
  97. open (STATUS, $statusfile);
  98. local $_;
  99. while (<STATUS>) {
  100. if (/^Status: .* installed$/m && /Package: (.*)$/m) {
  101. push @list, $1;
  102. }
  103. }
  104. close STATUS;
  105. return @list;
  106. }
  107. my %avail_pkgs;
  108. # Given a package name, checks to see if it's available. Memoised.
  109. sub package_avail {
  110. my $package=shift;
  111. if (! %avail_pkgs) {
  112. foreach my $pkg (list_avail()) {
  113. $avail_pkgs{$pkg} = 1;
  114. }
  115. }
  116. return $avail_pkgs{$package} || package_installed($package);
  117. }
  118. my %installed_pkgs;
  119. # Given a package name, checks to see if it's installed. Memoised.
  120. sub package_installed {
  121. my $package=shift;
  122. if (! %installed_pkgs) {
  123. foreach my $pkg (list_installed()) {
  124. $installed_pkgs{$pkg} = 1;
  125. }
  126. }
  127. return $installed_pkgs{$package};
  128. }
  129. # Given a task hash, checks if its key packages are available.
  130. sub task_avail {
  131. local $_;
  132. my $task=shift;
  133. if (! ref $task->{key}) {
  134. return 1;
  135. }
  136. else {
  137. foreach my $pkg (@{$task->{key}}) {
  138. if (! package_avail($pkg)) {
  139. return 0;
  140. }
  141. }
  142. return 1;
  143. }
  144. }
  145. # Given a task hash, checks to see if it is already installed.
  146. # (All of its key packages must be installed.)
  147. sub task_installed {
  148. local $_;
  149. my $task=shift;
  150. if (! ref $task->{key}) {
  151. return 0; # can't tell with no key packages
  152. }
  153. else {
  154. foreach my $pkg (@{$task->{key}}) {
  155. if (! package_installed($pkg)) {
  156. return 0;
  157. }
  158. }
  159. return 1;
  160. }
  161. }
  162. # Given task hash, returns a list of all available packages in the task.
  163. # If the aptitude_tasks parameter is true, then it does not expand tasks
  164. # that aptitude knows about, and just returns aptitude task syntax for
  165. # those.
  166. sub task_packages {
  167. my $task=shift;
  168. my $aptitude_tasks=shift;
  169. my %list;
  170. # key packages are always included
  171. if (ref $task->{key}) {
  172. map { $list{$_}=1 } @{$task->{key}};
  173. }
  174. if ($task->{packages} eq 'task-fields') {
  175. # task-fields method is built-in for speed and to support
  176. # aptitude task definitions
  177. if ($aptitude_tasks) {
  178. return '~t^'.$task->{task}.'$';
  179. }
  180. else {
  181. local $/="\n\n";
  182. open (AVAIL, "apt-cache dumpavail|");
  183. while (<AVAIL>) {
  184. if (/^Task: (.*)/m) {
  185. my @tasks=split(", ", $1);
  186. if (grep { $_ eq $task->{task} } @tasks) {
  187. $list{$1}=1 if /^Package: (.*)/m;
  188. }
  189. }
  190. }
  191. close AVAIL;
  192. }
  193. }
  194. elsif ($task->{packages} eq 'standard') {
  195. # standard method is built in since it cannot easily be
  196. # implemented externally.
  197. return "~pstandard", "~prequired", "~pimportant";
  198. }
  199. elsif ($task->{packages} eq 'manual') {
  200. # manual package selection is a special case
  201. return;
  202. }
  203. else {
  204. # external method
  205. my ($method, @params);
  206. if (ref $task->{packages}) {
  207. @params=@{$task->{packages}};
  208. $method=shift @params;
  209. }
  210. else {
  211. $method=$task->{packages};
  212. }
  213. map { $list{$_}=1 }
  214. grep { package_avail($_) }
  215. split(' ', `$packagesdir/$method $task->{task} @params`);
  216. }
  217. return keys %list;
  218. }
  219. # Given a task hash, runs any test program specified in its data, and sets
  220. # the _display and _install fields to 1 or 0 depending on its result.
  221. sub task_test {
  222. my $task=shift;
  223. my $new_install=shift;
  224. $ENV{NEW_INSTALL}=$new_install if defined $new_install;
  225. $task->{_display} = 1;
  226. $task->{_install} = 0;
  227. foreach my $test (grep /^test-.*/, keys %$task) {
  228. $test=~s/^test-//;
  229. if (-x "$testdir/$test") {
  230. my $ret=system("$testdir/$test", $task->{task}, split " ", $task->{"test-$test"}) >> 8;
  231. if ($ret == 0) {
  232. $task->{_display} = 0;
  233. $task->{_install} = 1;
  234. }
  235. elsif ($ret == 1) {
  236. $task->{_display} = 0;
  237. $task->{_install} = 0;
  238. }
  239. elsif ($ret == 2) {
  240. $task->{_display} = 1;
  241. $task->{_install} = 1;
  242. }
  243. elsif ($ret == 3) {
  244. $task->{_display} = 1;
  245. $task->{_install} = 0;
  246. }
  247. }
  248. }
  249. delete $ENV{NEW_INSTALL};
  250. return $task;
  251. }
  252. # Hides a task and marks it not to be installed if it depends on other
  253. # tasks.
  254. sub hide_dependent_tasks {
  255. my $task=shift;
  256. if (exists $task->{depends} && length $task->{depends}) {
  257. $task->{_display} = 0;
  258. $task->{_install} = 0;
  259. }
  260. return $task;
  261. }
  262. # Converts a list of tasks into a debconf list of their short descriptions.
  263. sub task_to_debconf {
  264. my $field = shift;
  265. join ", ", map {
  266. my $desc=$_->{$field};
  267. if ($desc=~/, /) {
  268. warning("task ".$_->{task}." contains a comma in its short description: \"$desc\"");
  269. }
  270. $desc;
  271. } @_;
  272. }
  273. # Given a first parameter that is a debconf list of short descriptions of
  274. # tasks, and then a list of task hashes, returns a list of hashes for all
  275. # the tasks in the debconf list.
  276. sub debconf_to_task {
  277. my $list=shift;
  278. my %desc_to_task = map { $_->{shortdesc} => $_ } @_;
  279. return grep { defined } map { $desc_to_task{$_} } split ", ", $list;
  280. }
  281. # Orders a list of tasks for display.
  282. sub order_for_display {
  283. sort {
  284. $b->{relevance} <=> $a->{relevance}
  285. || 0 ||
  286. $a->{section} cmp $b->{section}
  287. || 0 ||
  288. $a->{shortdesc} cmp $b->{shortdesc}
  289. } @_;
  290. }
  291. # Given a set of tasks and a name, returns the one with that name.
  292. sub name_to_task {
  293. my $name=shift;
  294. return (grep { $_->{task} eq $name } @_)[0];
  295. }
  296. sub usage {
  297. print STDERR gettext(q{Usage:
  298. tasksel install <task>
  299. tasksel remove <task>
  300. tasksel [options]
  301. -t, --test test mode; don't really do anything
  302. --new-install automatically install some tasks
  303. --list-tasks list tasks that would be displayed and exit
  304. --task-packages list available packages in a task
  305. --task-desc returns the description of a task
  306. });
  307. }
  308. # Process command line options and return them in a hash.
  309. sub getopts {
  310. my %ret;
  311. Getopt::Long::Configure ("bundling");
  312. if (! GetOptions(\%ret, "test|t", "new-install", "list-tasks",
  313. "task-packages=s@", "task-desc=s")) {
  314. usage();
  315. exit(1);
  316. }
  317. # Special case apt-like syntax.
  318. if (@ARGV && $ARGV[0] eq "install") {
  319. shift @ARGV;
  320. $ret{install} = shift @ARGV;
  321. }
  322. if (@ARGV && $ARGV[0] eq "remove") {
  323. shift @ARGV;
  324. $ret{remove} = shift @ARGV;
  325. }
  326. if (@ARGV) {
  327. usage();
  328. exit 1;
  329. }
  330. return %ret;
  331. }
  332. sub main {
  333. my %options=getopts();
  334. # Options that output stuff and don't need a full processed list of
  335. # tasks.
  336. if (exists $options{"task-packages"}) {
  337. my @tasks=all_tasks();
  338. foreach my $taskname (@{$options{"task-packages"}}) {
  339. my $task=name_to_task($taskname, @tasks);
  340. if ($task) {
  341. print "$_\n" foreach task_packages($task);
  342. }
  343. }
  344. exit(0);
  345. }
  346. elsif ($options{"task-desc"}) {
  347. my $task=name_to_task($options{"task-desc"}, all_tasks());
  348. if ($task) {
  349. my $extdesc=join(" ", @{$task->{description}}[1..$#{$task->{description}}]);
  350. print dgettext("debian-tasks", $extdesc)."\n";
  351. exit(0);
  352. }
  353. else {
  354. exit(1);
  355. }
  356. }
  357. # This is relatively expensive, get the full list of available tasks and
  358. # mark them.
  359. my @tasks=map { hide_dependent_tasks($_) } map { task_test($_, $options{"new-install"}) }
  360. grep { task_avail($_) } all_tasks();
  361. if ($options{"list-tasks"}) {
  362. map { $_->{_installed} = task_installed($_) } @tasks;
  363. print "".($_->{_installed} ? "i" : "u")." ".$_->{task}."\t".$_->{shortdesc}."\n"
  364. foreach order_for_display(grep { $_->{_display} } @tasks);
  365. exit(0);
  366. }
  367. if (! $options{"new-install"}) {
  368. # Don't install hidden tasks if this is not a new install.
  369. map { $_->{_install} = 0 } grep { $_->{_display} == 0 } @tasks;
  370. }
  371. if ($options{"install"}) {
  372. my $task=name_to_task($options{"install"}, @tasks);
  373. $task->{_install} = 1 if $task;
  374. }
  375. my @aptitude_remove;
  376. if ($options{"remove"}) {
  377. my $task=name_to_task($options{"remove"}, @tasks);
  378. push @aptitude_remove, task_packages($task, 0);
  379. }
  380. # The interactive bit.
  381. my $interactive=0;
  382. my @list = order_for_display(grep { $_->{_display} == 1 } @tasks);
  383. if (@list && ! $options{install} && ! $options{remove}) {
  384. $interactive=1;
  385. if (! $options{"new-install"}) {
  386. # Find tasks that are already installed.
  387. map { $_->{_installed} = task_installed($_) } @list;
  388. # Don't install new tasks unless manually selected.
  389. map { $_->{_install} = 0 } @list;
  390. }
  391. else {
  392. # Assume that no tasks are installed, to ensure
  393. # that complete tasks get installed on new
  394. # installs.
  395. map { $_->{_installed} = 0 } @list;
  396. }
  397. my $question="tasksel/tasks";
  398. if ($options{"new-install"}) {
  399. $question="tasksel/first";
  400. }
  401. my @default = grep { $_->{_display} == 1 && ($_->{_install} == 1 || $_->{_installed} == 1) } @tasks;
  402. my $tmpfile=`tempfile`;
  403. chomp $tmpfile;
  404. system($debconf_helper, $tmpfile,
  405. task_to_debconf("shortdesc", @list),
  406. task_to_debconf("shortdesctrans", @list),
  407. task_to_debconf("shortdesc", @default),
  408. $question);
  409. open(IN, "<$tmpfile");
  410. my $ret=<IN>;
  411. if (! defined $ret) {
  412. die "tasksel canceled\n";
  413. }
  414. chomp $ret;
  415. close IN;
  416. unlink $tmpfile;
  417. # Set _install flags based on user selection.
  418. map { $_->{_install} = 0 } @list;
  419. foreach my $task (debconf_to_task($ret, @tasks)) {
  420. if (! $task->{_installed}) {
  421. $task->{_install} = 1;
  422. }
  423. $task->{_selected} = 1;
  424. }
  425. foreach my $task (@list) {
  426. if (! $task->{_selected} && $task->{_installed}) {
  427. push @aptitude_remove, task_packages($task, 0);
  428. }
  429. }
  430. }
  431. # Mark dependnent packages for install if their dependencies are met.
  432. foreach my $task (@tasks) {
  433. if (! $task->{_install} && exists $task->{depends} && length $task->{depends} ) {
  434. $task->{_install} = 1;
  435. foreach my $dep (split(', ', $task->{depends})) {
  436. if (! grep { $_->{task} eq $dep && $_->{_install} } @tasks) {
  437. $task->{_install} = 0;
  438. }
  439. }
  440. }
  441. }
  442. # Add tasks to install and see if any selected task requires manual
  443. # selection.
  444. my @aptitude_install;
  445. my $manual_selection=0;
  446. foreach my $task (grep { $_->{_install} } @tasks) {
  447. push @aptitude_install, task_packages($task, 1);
  448. if ($task->{packages} eq 'manual') {
  449. $manual_selection=1;
  450. }
  451. }
  452. # Clear screen before running aptitude unless we're in a debconf
  453. # frontend already.
  454. if ($interactive && (@aptitude_remove || @aptitude_install)
  455. && ! $options{test} && ! $ENV{DEBIAN_HAS_FRONTEND}) {
  456. system("clear");
  457. }
  458. my $aptitude;
  459. if ($manual_selection) {
  460. # Manaul selection and task installs, as best
  461. # aptitude can do it currently. Disables use of
  462. # debconf-apt-progress.
  463. unshift @aptitude_install, "--visual-preview";
  464. $aptitude="aptitude";
  465. }
  466. else {
  467. $aptitude="debconf-apt-progress -- aptitude";
  468. }
  469. # Remove any packages we were asked to.
  470. if (@aptitude_remove) {
  471. if ($options{test}) {
  472. print "$aptitude -y remove ".join(" ", @aptitude_remove)."\n";
  473. }
  474. else {
  475. my $ret=system(split(' ', $aptitude), "-y", "remove", @aptitude_remove) >> 8;
  476. if ($ret != 0) {
  477. error gettext("aptitude failed")." ($ret)";
  478. }
  479. }
  480. }
  481. # And finally, act on selected tasks.
  482. if (@aptitude_install || $manual_selection) {
  483. # If the user selected no tasks and manual package
  484. # selection, run aptitude w/o the --visual-preview parameter.
  485. if (! @aptitude_install && $manual_selection) {
  486. if ($options{test}) {
  487. print "aptitude\n";
  488. }
  489. else {
  490. my $ret=system("aptitude") >> 8;
  491. if ($ret != 0) {
  492. error gettext("aptitude failed")." ($ret)";
  493. }
  494. }
  495. }
  496. else {
  497. if ($options{test}) {
  498. print "$aptitude --without-recommends -y install ".join(" ", @aptitude_install)."\n";
  499. }
  500. else {
  501. my $ret=system(split(' ', $aptitude), "--without-recommends", "-y", "install", @aptitude_install) >> 8;
  502. if ($ret != 0) {
  503. error gettext("aptitude failed");
  504. }
  505. }
  506. }
  507. }
  508. }
  509. main();