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.
 
 
 

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