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.
 
 
 

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