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.
 
 
 

543 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. print STDERR "parse error in stanza $. of $desc\n";
  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. else {
  200. # external method
  201. my ($method, @params);
  202. if (ref $task->{packages}) {
  203. @params=@{$task->{packages}};
  204. $method=shift @params;
  205. }
  206. else {
  207. $method=$task->{packages};
  208. }
  209. map { $list{$_}=1 }
  210. grep { package_avail($_) }
  211. split(' ', `$packagesdir/$method $task->{task} @params`);
  212. }
  213. return keys %list;
  214. }
  215. # Given a task hash, runs any test program specified in its data, and sets
  216. # the _display and _install fields to 1 or 0 depending on its result.
  217. sub task_test {
  218. my $task=shift;
  219. my $new_install=shift;
  220. $ENV{NEW_INSTALL}=$new_install if defined $new_install;
  221. $task->{_display} = 1;
  222. $task->{_install} = 0;
  223. foreach my $test (grep /^test-.*/, keys %$task) {
  224. $test=~s/^test-//;
  225. if (-x "$testdir/$test") {
  226. my $ret=system("$testdir/$test", $task->{task}, split " ", $task->{"test-$test"}) >> 8;
  227. if ($ret == 0) {
  228. $task->{_display} = 0;
  229. $task->{_install} = 1;
  230. }
  231. elsif ($ret == 1) {
  232. $task->{_display} = 0;
  233. $task->{_install} = 0;
  234. }
  235. elsif ($ret == 2) {
  236. $task->{_display} = 1;
  237. $task->{_install} = 1;
  238. }
  239. elsif ($ret == 3) {
  240. $task->{_display} = 1;
  241. $task->{_install} = 0;
  242. }
  243. }
  244. }
  245. delete $ENV{NEW_INSTALL};
  246. return $task;
  247. }
  248. # Hides a task and marks it not to be installed if it depends on other
  249. # tasks.
  250. sub hide_dependent_tasks {
  251. my $task=shift;
  252. if (exists $task->{depends} && length $task->{depends}) {
  253. $task->{_display} = 0;
  254. $task->{_install} = 0;
  255. }
  256. return $task;
  257. }
  258. # Converts a list of tasks into a debconf list of their short descriptions.
  259. sub task_to_debconf {
  260. my $field = shift;
  261. join ", ", map {
  262. my $desc=$_->{$field};
  263. if ($desc=~/, /) {
  264. warning("task ".$_->{task}." contains a comma in its short description: \"$desc\"");
  265. }
  266. $desc;
  267. } @_;
  268. }
  269. # Given a first parameter that is a debconf list of short descriptions of
  270. # tasks, and then a list of task hashes, returns a list of hashes for all
  271. # the tasks in the debconf list.
  272. sub debconf_to_task {
  273. my $list=shift;
  274. my %desc_to_task = map { $_->{shortdesc} => $_ } @_;
  275. return grep { defined } map { $desc_to_task{$_} } split ", ", $list;
  276. }
  277. # Orders a list of tasks for display.
  278. sub order_for_display {
  279. sort {
  280. $b->{relevance} <=> $a->{relevance}
  281. || 0 ||
  282. $a->{section} cmp $b->{section}
  283. || 0 ||
  284. $a->{shortdesc} cmp $b->{shortdesc}
  285. } @_;
  286. }
  287. # Given a set of tasks and a name, returns the one with that name.
  288. sub name_to_task {
  289. my $name=shift;
  290. return (grep { $_->{task} eq $name } @_)[0];
  291. }
  292. sub usage {
  293. print STDERR gettext(q{Usage:
  294. tasksel install <task>
  295. tasksel remove <task>
  296. tasksel [options]
  297. -t, --test test mode; don't really do anything
  298. --new-install automatically install some tasks
  299. --list-tasks list tasks that would be displayed and exit
  300. --task-packages list available packages in a task
  301. --task-desc returns the description of a task
  302. });
  303. }
  304. # Process command line options and return them in a hash.
  305. sub getopts {
  306. my %ret;
  307. Getopt::Long::Configure ("bundling");
  308. if (! GetOptions(\%ret, "test|t", "new-install", "list-tasks",
  309. "task-packages=s@", "task-desc=s")) {
  310. usage();
  311. exit(1);
  312. }
  313. # Special case apt-like syntax.
  314. if (@ARGV && $ARGV[0] eq "install") {
  315. shift @ARGV;
  316. $ret{install} = shift @ARGV;
  317. }
  318. if (@ARGV && $ARGV[0] eq "remove") {
  319. shift @ARGV;
  320. $ret{remove} = shift @ARGV;
  321. }
  322. if (@ARGV) {
  323. usage();
  324. exit 1;
  325. }
  326. return %ret;
  327. }
  328. sub main {
  329. my %options=getopts();
  330. # Options that output stuff and don't need a full processed list of
  331. # tasks.
  332. if (exists $options{"task-packages"}) {
  333. my @tasks=all_tasks();
  334. foreach my $taskname (@{$options{"task-packages"}}) {
  335. my $task=name_to_task($taskname, @tasks);
  336. if ($task) {
  337. print "$_\n" foreach task_packages($task);
  338. }
  339. }
  340. exit(0);
  341. }
  342. elsif ($options{"task-desc"}) {
  343. my $task=name_to_task($options{"task-desc"}, all_tasks());
  344. if ($task) {
  345. my $extdesc=join(" ", @{$task->{description}}[1..$#{$task->{description}}]);
  346. print dgettext("debian-tasks", $extdesc)."\n";
  347. exit(0);
  348. }
  349. else {
  350. exit(1);
  351. }
  352. }
  353. # This is relatively expensive, get the full list of available tasks and
  354. # mark them.
  355. my @tasks=map { hide_dependent_tasks($_) } map { task_test($_, $options{"new-install"}) }
  356. grep { task_avail($_) } all_tasks();
  357. if ($options{"list-tasks"}) {
  358. map { $_->{_installed} = task_installed($_) } @tasks;
  359. print "".($_->{_installed} ? "i" : "u")." ".$_->{task}."\t".$_->{shortdesc}."\n"
  360. foreach order_for_display(grep { $_->{_display} } @tasks);
  361. exit(0);
  362. }
  363. # Now work out what to tell aptitude to install.
  364. my @aptitude_install;
  365. if (! $options{"new-install"}) {
  366. # Don't install hidden tasks if this is not a new install.
  367. map { $_->{_install} = 0 } grep { $_->{_display} == 0 } @tasks;
  368. }
  369. if ($options{"install"}) {
  370. my $task=name_to_task($options{"install"}, @tasks);
  371. $task->{_install} = 1 if $task;
  372. }
  373. my @aptitude_remove;
  374. if ($options{"remove"}) {
  375. my $task=name_to_task($options{"remove"}, @tasks);
  376. push @aptitude_remove, task_packages($task, 0);
  377. }
  378. # The interactive bit.
  379. my $interactive=0;
  380. my $manual_selection=0;
  381. my @list = order_for_display(grep { $_->{_display} == 1 } @tasks);
  382. if (@list && ! $options{install} && ! $options{remove}) {
  383. $interactive=1;
  384. if (! $options{"new-install"}) {
  385. # Find tasks that are already installed.
  386. map { $_->{_installed} = task_installed($_) } @list;
  387. # Don't install new tasks unless manually selected.
  388. map { $_->{_install} = 0 } @list;
  389. }
  390. else {
  391. # Assume that no tasks are installed, to ensure
  392. # that complete tasks get installed on new
  393. # installs.
  394. map { $_->{_installed} = 0 } @list;
  395. }
  396. my $question="tasksel/tasks";
  397. if ($options{"new-install"}) {
  398. $question="tasksel/first";
  399. }
  400. my @default = grep { $_->{_display} == 1 && ($_->{_install} == 1 || $_->{_installed} == 1) } @tasks;
  401. my $tmpfile=`tempfile`;
  402. chomp $tmpfile;
  403. system($debconf_helper, $tmpfile,
  404. task_to_debconf("shortdesc", @list),
  405. task_to_debconf("shortdesctrans", @list),
  406. task_to_debconf("shortdesc", @default),
  407. $question);
  408. open(IN, "<$tmpfile");
  409. my $ret=<IN>;
  410. if (! defined $ret) {
  411. die "tasksel canceled\n";
  412. }
  413. chomp $ret;
  414. close IN;
  415. unlink $tmpfile;
  416. if ($ret=~/manual package selection/) {
  417. $manual_selection=1;
  418. }
  419. # Set _install flags based on user selection.
  420. map { $_->{_install} = 0 } @list;
  421. foreach my $task (debconf_to_task($ret, @tasks)) {
  422. if (! $task->{_installed}) {
  423. $task->{_install} = 1;
  424. }
  425. $task->{_selected} = 1;
  426. }
  427. foreach my $task (@list) {
  428. if (! $task->{_selected} && $task->{_installed}) {
  429. push @aptitude_remove, task_packages($task, 0);
  430. }
  431. }
  432. }
  433. # Mark dependnent packages for install if their dependencies are met.
  434. foreach my $task (@tasks) {
  435. if (! $task->{_install} && exists $task->{depends} && length $task->{depends} ) {
  436. $task->{_install} = 1;
  437. foreach my $dep (split(', ', $task->{depends})) {
  438. if (! grep { $_->{task} eq $dep && $_->{_install} } @tasks) {
  439. $task->{_install} = 0;
  440. }
  441. }
  442. }
  443. }
  444. # Add tasks to install.
  445. my @to_install=map { task_packages($_, 1) } grep { $_->{_install} } @tasks;
  446. push @aptitude_install, @to_install;
  447. # Clear screen before running aptitude.
  448. if ($interactive && (@aptitude_remove || @aptitude_install) && ! $options{test}) {
  449. system("clear");
  450. }
  451. # Remove any packages we were asked to.
  452. if (@aptitude_remove) {
  453. if ($options{test}) {
  454. print "aptitude remove ".join(" ", @aptitude_remove)."\n";
  455. }
  456. else {
  457. my $ret=system("aptitude", "remove", @aptitude_remove) >> 8;
  458. if ($ret != 0) {
  459. error gettext("aptitude failed")." ($ret)";
  460. }
  461. }
  462. }
  463. # And finally, act on selected tasks.
  464. if (@aptitude_install || $manual_selection) {
  465. # If the user selected no tasks and manual package
  466. # selection, run aptitude w/o the --visual-preview parameter.
  467. if (! @to_install && $manual_selection) {
  468. if ($options{test}) {
  469. print "aptitude\n";
  470. }
  471. else {
  472. my $ret=system("aptitude") >> 8;
  473. if ($ret != 0) {
  474. error gettext("aptitude failed")." ($ret)";
  475. }
  476. }
  477. }
  478. else {
  479. # Manaul selection and task installs, as best
  480. # aptitude can do it currently.
  481. if ($manual_selection) {
  482. unshift @aptitude_install, "--visual-preview";
  483. }
  484. if ($options{test}) {
  485. print "aptitude --without-recommends -y install ".join(" ", @aptitude_install)."\n";
  486. }
  487. else {
  488. my $ret=system("aptitude", "--without-recommends", "-y", "install", @aptitude_install) >> 8;
  489. if ($ret != 0) {
  490. error gettext("aptitude failed");
  491. }
  492. }
  493. }
  494. }
  495. }
  496. main();