summaryrefslogtreecommitdiff
path: root/rpmorphan-lib.pl
blob: 5eacbcc5118ab346c3d2157c936f13836fdc957b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#!/usr/bin/perl 
###############################################################################
#   rpmorphan-lib.pl
#
#    Copyright (C) 2006 by Eric Gerbier
#    Bug reports to: gerbier@users.sourceforge.net
#    $Id: rpmorphan-1.11 | rpmorphan-lib.pl | Wed Aug 1 09:36:43 2012 +0200 | Eric Gerbier  $
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
###############################################################################
use strict;
use warnings;

use English '-no_match_vars';

use Data::Dumper;    # debug
use File::stat;      # get_last_access

## no critic (RequireCheckingReturnValueOfEval)

# the code should use no "print" calls
# but use instead debug, warning, info calls
###############################################################################
sub nodebug {
	return;
}
###############################################################################
sub backend_debug {
	my $text = $_[0];
	print "debug $text\n";
	return;
}
###############################################################################
# change debug subroutine
# this way seems better than previous one as
# - it does not need any "global" verbose variable
# - it suppress any verbose test (quicker)
sub init_debug($) {
	my $verbose = shift @_;

	# to avoid error messages
	## no critic ( NoWarnings );
	no warnings 'redefine';
	## use critic;

	if ($verbose) {
		*debug = \&backend_debug;
	}
	else {
		*debug = \&nodebug;
	}

	use warnings 'all';
	return;
}
###############################################################################
# used to print warning messages
sub warning($) {
	my $text = shift @_;
	warn "WARNING $text\n";
	return;
}
###############################################################################
# used to print normal messages
sub info($) {
	my $text = shift @_;
	print "$text\n";
	return;
}
###############################################################################
sub print_version($) {
	my $version = shift @_;
	info("$PROGRAM_NAME version $version");
	return;
}
#########################################################
# used to check on option
sub is_set($$) {
	my $rh_opt = shift @_;    # hash of program arguments
	my $key    = shift @_;    # name of desired option

	#debug("is_set $key");
	my $r_value = $rh_opt->{$key};
	return ${$r_value};
}
#########################################################
sub is_verbose($) {
	my $rh_opt = shift @_;    # hash of program arguments

	return is_set( $rh_opt, 'verbose' );
}
#########################################################
# apply a filter on package list according program options
sub rpmfilter($$) {
	my $rh_opt      = shift @_;
	my $rh_list_pac = shift @_;

	display_status('apply filters');

	# we just want the list of keys
	my @list = keys %{$rh_list_pac};

	if ( is_set( $rh_opt, 'all' ) ) {
		debug('all');
		return @list;
	}
	else {
		debug('guess');

		my @filtered_list;
		if ( is_set( $rh_opt, 'guess-perl' ) ) {
			debug('guess-perl');
			my @res = grep { /^perl/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-python' ) ) {
			my @res = grep { /^python/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-pike' ) ) {
			my @res = grep { /^pike/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-ruby' ) ) {
			my @res = grep { /^ruby/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-common' ) ) {
			my @res = grep { /-common$/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-data' ) ) {
			my @res = grep { /-data$/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-doc' ) ) {
			my @res = grep { /-doc$/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-dev' ) ) {
			my @res = grep { /-devel$/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-lib' ) ) {
			my @res = grep { /^lib/ } @list;
			push @filtered_list, @res;
		}
		if ( is_set( $rh_opt, 'guess-custom' ) ) {
			my $regex = ${ $rh_opt->{'guess-custom'} };
			my @res = grep { /$regex/ } @list;
			push @filtered_list, @res;
		}
		return @filtered_list;
	}
}
#########################################################
# difference between 2 date in unix format
# return in days
sub diff_date($$) {
	my $now  = shift @_;    # current
	my $time = shift @_;    #

	# convert from seconds to days
	## no critic(ProhibitParensWithBuiltins,ProhibitMagicNumbers)
	return int( ( $now - $time ) / 86_400 );
}
#########################################################
# return the date (unix format) of last access on a package
# (scan all inodes for atime) and the name of the file
sub get_last_access($) {
	my $ra_files = shift @_;    # array of file names

	my $last_date = 0;          # means a very old date for linux : 1970
	my $last_file = q{};
  FILE: foreach my $file ( @{$ra_files} ) {
		next FILE unless ( -e $file );
		my $stat  = stat $file;
		my $atime = $stat->atime();

		if ( $atime > $last_date ) {
			$last_date = $atime;
			$last_file = $file;
		}
	}
	return ( $last_date, $last_file );
}
#########################################################
# depending the cache option, return a command to execute
# - as an rpm query (piped)
# - as a cache file to open
sub init_cache($) {
	my $rh_opt = shift @_;    # hash of program arguments

	# the main idea is to reduce the number of call to rpm in order to gain time
	# the ';' separator is used to separate fields
	# the ',' separator is used to separate data in fields arrays

	# note : we do not ask for PROVIDEFLAGS PROVIDEVERSION
	# REQUIREFLAGS REQUIREVERSION

	my $suggests = ( is_set( $rh_opt, 'suggests' ) ) ? '[%{SUGGESTS},]' : q{};

	my $rpm_cmd =
"rpm -qa --queryformat '%{NAME};[%{REQUIRENAME},]$suggests;[%{PROVIDES},];[%{FILENAMES},];%{INSTALLTIME}\n' 2>/dev/null";
	my $cache_file = '/tmp/rpmorphan.cache';
	my $fh_cache;
	my $cmd;

	if ( is_set( $rh_opt, 'clear-cache' ) ) {
		unlink $cache_file if ( -f $cache_file );
	}

	if ( is_set( $rh_opt, 'use-cache' ) ) {
		if ( -f $cache_file ) {

			# cache exists : use it
			$cmd = $cache_file;
			display_status("use cache file $cache_file");
		}
		else {

			# use rpm command
			$cmd = "$rpm_cmd |";

			# and create cache file
			## no critic (RequireBriefOpen)
			if ( open $fh_cache, '>', $cache_file ) {
				display_status("create cache file $cache_file");
			}
			else {
				warning("can not create cache file $cache_file : $ERRNO");
			}
		}
	}
	else {

		# output may be long, so we use a pipe to avoid to store a big array
		$cmd = "$rpm_cmd |";
		display_status('read rpm data');

		unlink $cache_file if ( -f $cache_file );
	}

	return ( $cmd, $fh_cache );
}
#########################################################
## no critic (ProhibitManyArgs)
sub analyse_rpm_info($$$$$$$$$$) {
	my $name        = shift @_;    # current package name
	my $ra_prov     = shift @_;    # data from database on name
	my $ra_req      = shift @_;    # idem
	my $ra_files    = shift @_;    # idem
	my $rh_objects  = shift @_;
	my $rh_provides = shift @_;
	my $rh_files    = shift @_;
	my $rh_depends  = shift @_;
	my $rh_virtual  = shift @_;
	my $rh_requires = shift @_;

	## use critic;

	# we do not use version in keys
	# so we only keep the last seen data for a package name
	if ( exists $rh_provides->{$name} ) {
		debug("duplicate package $name");
	}

  VIRTUAL: foreach my $p ( @{$ra_prov}, @{$ra_files} ) {

		# do not add files
		#next VIRTUAL if ( $p =~ m!^/! );

		#  some bad package may provide more than on time the same object
		if (   ( exists $rh_objects->{$p} )
			&& ( $rh_objects->{$p} ne $name ) )
		{

			# method 1 (old)
			# we do not use who provide this virtual for now
			#$rh_virtual->{$p} = 1;

			# method 2 (current)
			# to improve the code, we can use virtual as a counter
			# ( as a garbage collector )
			# so we can remove all providing except the last one
			if ( !exists $rh_virtual->{$p} ) {

				# add previous data
				$rh_virtual->{$p}{ $rh_objects->{$p} } = 1;
			}

			# add new virtual
			$rh_virtual->{$p}{$name} = 1;
		}
		else {

			# keep memory of seen "provided"
			$rh_objects->{$p} = $name;
		}
	}

	# $name package provide @prov
	# file are also included in "provide"
	push @{ $rh_provides->{$name} }, @{$ra_prov}, @{$ra_files};

	# list are necessary for access-time option
	push @{ $rh_files->{$name} }, @{$ra_files};

	# this will be helpfull when recursive remove package
	push @{ $rh_requires->{$name} }, @{$ra_req};

	# build a hash for dependencies
	foreach my $require ( @{$ra_req} ) {

		# we have to suppress auto-depends (ex : ark package)
		my $flag_auto = 0;
	  PROVIDE: foreach my $p ( @{$ra_prov} ) {
			if ( $require eq $p ) {
				$flag_auto = 1;

				#debug("skip auto-depency on $name");
				last PROVIDE;
			}
		}

	   # $name depends from $require
	   # exemple : depends { 'afick' } { afick-gui } = 1
	   # push @{ $rh_depends->{$require} }, $name unless $flag_auto;
	   # we use a hash to help when we have to delete data (on recursive remove)
		$rh_depends->{$require}{$name} = 1 unless $flag_auto;
	}
	return;
}
#########################################################
# read rpm information about all installed packages
# can be from database, or from rpmorphan cache
sub read_rpm_data_base($$$$$$$) {
	my $rh_opt          = shift @_;    # hash of program arguments
	my $rh_provides     = shift @_;
	my $rh_install_time = shift @_;
	my $rh_files        = shift @_;
	my $rh_depends      = shift @_;
	my $rh_virtual      = shift @_;
	my $rh_requires     = shift @_;

	display_status('read rpm data using base RPM');
	my ( $cmd, $fh_cache ) = init_cache($rh_opt);

	# because we can open a pipe or a cache, it is not possible to use
	# the 3 arg form of open
	## no critic (ProhibitTwoArgOpen,RequireBriefOpen);
	my $fh;
	if ( !open $fh, $cmd ) {

		# no critic;
		die "can not open $cmd : $ERRNO\n";
		## use critic
	}
	debug('1 : analysis');
	my %objects;
	while (<$fh>) {

		# write cache
		print {$fh_cache} $_ if ($fh_cache);

		my ( $name, $req, $prov, $files, $install_time ) = split /;/, $_;

		# install time are necessary for install-time option
		$rh_install_time->{$name} = $install_time;

		# see rpm query format in init_cache
		my $delim = q{,};
		my @prov  = split /$delim/, $prov;
		my @req   = split /$delim/, $req;
		my @files = split /$delim/, $files;

		analyse_rpm_info(
			$name,       \@prov,       \@req,     \@files,
			\%objects,   $rh_provides, $rh_files, $rh_depends,
			$rh_virtual, $rh_requires
		);

	}
	close $fh or warning("problem to close rpm command : $ERRNO");

	# close cache if necessary
	if ($fh_cache) {
		close $fh_cache or warning("problem to close cache file : $ERRNO");
	}
	return;
}
#########################################################
# read database info by use of RPM2 module
sub read_rpm_data_rpm2($$$$$$$) {
	my $rh_opt          = shift @_;    # hash of program arguments
	my $rh_provides     = shift @_;
	my $rh_install_time = shift @_;
	my $rh_files        = shift @_;
	my $rh_depends      = shift @_;
	my $rh_virtual      = shift @_;
	my $rh_requires     = shift @_;

	# cache ?
	# acces to data by rpm2 should be fast,
	# so do not need cache
	# and init_cache works with a command to exec
	# not with a perl module

	display_status('read rpm data using RPM2');
	import RPM2;
	my $db = RPM2->open_rpm_db();

	my $flag_suggests = is_set( $rh_opt, 'suggests' );

	debug('1 : analysis');
	my %objects;

	my $i = $db->find_all_iter();
	while ( my $pkg = $i->next ) {

		my $name         = $pkg->name;
		my $install_time = $pkg->installtime;
		$rh_install_time->{$name} = $install_time;

		my @req   = $pkg->requires;
		my @prov  = $pkg->provides;
		my @files = $pkg->files;

		if ($flag_suggests) {
			my @suggests = $pkg->tag('SUGGESTS');

			#debug("suggests for $name : " . join ' ', @suggests);
			push @req, @suggests;
		}

		analyse_rpm_info(
			$name,       \@prov,       \@req,     \@files,
			\%objects,   $rh_provides, $rh_files, $rh_depends,
			$rh_virtual, $rh_requires
		);
	}
	return;
}
#########################################################
# URPM return dependencies as perl[ = 5.8]
# we have to suppress the version and only keep the package
sub clean_rel($) {
	my $ra = shift @_;

	foreach my $elem ( @{$ra} ) {
		$elem =~ s/\[.*\]//;
	}
	return;
}
#########################################################
# read database info by use of URPM module
sub read_rpm_data_urpm($$$$$$$) {
	my $rh_opt          = shift @_;    # hash of program arguments
	my $rh_provides     = shift @_;
	my $rh_install_time = shift @_;
	my $rh_files        = shift @_;
	my $rh_depends      = shift @_;
	my $rh_virtual      = shift @_;
	my $rh_requires     = shift @_;

	# cache ?
	# acces to data by rpm2 should be fast,
	# so do not need cache
	# and init_cache works with a command to exec
	# not with a perl module

	display_status('read rpm data using URPM');
	import URPM;
	my $db = URPM::DB::open();
	debug('1 : analysis');

	my $flag_suggests = is_set( $rh_opt, 'suggests' );

	my %objects;
	$db->traverse(
		sub {
			my ($package) = @_;          # this is a URPM::Package object
			my $name = $package->name;
			my $installtime = $package->queryformat('%{INSTALLTIME}');
			$rh_install_time->{$name} = $installtime;
			my @req = $package->requires();
			clean_rel( \@req );
			my @prov = $package->provides();
			clean_rel( \@prov );
			my @files = $package->files();

			if ($flag_suggests) {
				my $suggests = $package->queryformat('[%{SUGGESTS},]');
				my @suggests = split /,/, $suggests;

				#debug("suggests for $name : " . join ' ', @suggests);
				clean_rel( \@suggests );
				push @req, @suggests;
			}

			analyse_rpm_info(
				$name,       \@prov,       \@req,     \@files,
				\%objects,   $rh_provides, $rh_files, $rh_depends,
				$rh_virtual, $rh_requires
			);
		}
	);

	return;
}
#########################################################
sub read_rpm_data($$$$$$$) {
	my $rh_opt          = shift @_;    # hash of program arguments
	my $rh_provides     = shift @_;
	my $rh_install_time = shift @_;
	my $rh_files        = shift @_;
	my $rh_depends      = shift @_;
	my $rh_virtual      = shift @_;
	my $rh_requires     = shift @_;

	# empty all structures
	%{$rh_provides}     = ();
	%{$rh_install_time} = ();
	%{$rh_files}        = ();
	%{$rh_depends}      = ();
	%{$rh_virtual}      = ();
	%{$rh_requires}     = ();

	my %code = (
		'URPM'  => \&read_rpm_data_urpm,
		'RPM2'  => \&read_rpm_data_rpm2,
		'basic' => \&read_rpm_data_base,
	);
	my @def_list = ( 'URPM', 'RPM2', 'basic' );
	my @list;
	if ( exists $ENV{'RPMORPHAN_METHOD'} ) {
		my $method = $ENV{'RPMORPHAN_METHOD'};

		# should be 'URPM', 'RPM2' or 'basic'
		if ( exists $code{$method} ) {
			push @list, $method;

			# for security : basic will allways work
			push @list, 'basic';
		}
		else {
			warning("unknown method $method, use default");
			@list = @def_list;
		}
	}
	else {

		# default list (ordered) of perl packages to test
		@list = @def_list;
	}
	my $ok = 0;
	foreach my $method (@list) {
		debug("try $method");
		eval { require $method . '.pm'; };
		if ($EVAL_ERROR) {
			debug("can not use $method");
		}
		else {
			$ok              = 1;
			*read_rpm_data_m = $code{$method};
			read_rpm_data_m( $rh_opt, $rh_provides, $rh_install_time, $rh_files,
				$rh_depends, $rh_virtual, $rh_requires );
			last;
		}
	}
	if ( !$ok ) {

		# if nothing is working, use default basic method
		read_rpm_data_base( $rh_opt, $rh_provides, $rh_install_time, $rh_files,
			$rh_depends, $rh_virtual, $rh_requires );
	}
	return;
}
## use critic
#########################################################
sub read_one_rc($$$) {
	my $rh_list = shift @_;    # list of available parameters
	my $fh_rc   = shift @_;
	my $rcfile  = shift @_;

	# perl cookbook, 8.16
	my $line = 1;
  RC: while (<$fh_rc>) {
		chomp;
		s/#.*//;               # comments
		s/^\s+//;              # skip spaces
		s/\s+$//;
		next RC unless length;
		my ( $key, $value ) = split /\s*=\s*/, $_, 2;
		if ( defined $key ) {
			if ( exists $rh_list->{$key} ) {

				# the last line wins
				if ( ref( $rh_list->{$key} ) eq 'ARRAY' ) {
					@{ $rh_list->{$key} } = get_from_command_line($value);
				}
				else {
					${ $rh_list->{$key} } = $value;
				}

				# special case : verbose will modify immediately behavior
				init_debug($value) if ( $key eq 'verbose' );

				debug(
"rcfile ($rcfile) : found $key parameter with value : $value"
				);
			}
			else {
				warning("bad $key parameter in line $line in $rcfile file");
			}
		}
		else {
			warning("bad line $line in $rcfile file");
		}
		$line++;
	}

	return;
}
#########################################################
# read all existing rc file from general to local :
# host, home, local directory
sub readrc($) {

	my $rh_list = shift @_;    # list of available parameters

	# can use local rc file, home rc file, host rc file
	my @list_rc =
	  ( '/etc/rpmorphanrc', $ENV{HOME} . '/.rpmorphanrc', '.rpmorphanrc', );

	foreach my $rcfile (@list_rc) {

		if ( -f $rcfile ) {
			debug("read rc from $rcfile");
			if ( open my $fh_rc, '<', $rcfile ) {
				read_one_rc( $rh_list, $fh_rc, $rcfile );
				close $fh_rc
				  or warning("problem to close rc file $rcfile :$ERRNO");
			}
			else {
				warning("can not open rcfile $rcfile : $ERRNO");
			}
		}
		else {
			debug("no rcfile $rcfile found");
		}
	}
	return;
}
#########################################################
# because arg can be given in one or several options :
# --add toto1 --add toto2
# --add toto1,toto2
sub get_from_command_line(@) {
	my @arg = @_;

	my $comma = q{,};
	## no critic (ProhibitParensWithBuiltins);
	return split /$comma/, join( $comma, @arg );
	## use critic;
}
#########################################################
sub is_remove_allowed($) {
	my $opt_dry_run = shift @_;

	return ( ( $EFFECTIVE_USER_ID == 0 ) && ( !$opt_dry_run ) );
}
#########################################################
# analyse command line options for guess
sub ana_guess($) {
	my $rh_opt = shift @_;

	debug('ana_guess');

	my $true = 1;

	# guess-all force all others
	if ( is_set( $rh_opt, 'guess-all' ) ) {
		$rh_opt->{'guess-perl'}   = \$true;
		$rh_opt->{'guess-python'} = \$true;
		$rh_opt->{'guess-pike'}   = \$true;
		$rh_opt->{'guess-ruby'}   = \$true;
		$rh_opt->{'guess-common'} = \$true;
		$rh_opt->{'guess-data'}   = \$true;
		$rh_opt->{'guess-doc'}    = \$true;
		$rh_opt->{'guess-dev'}    = \$true;
		$rh_opt->{'guess-lib'}    = \$true;
	}

	my $is_guess =
	     is_set( $rh_opt, 'guess-perl' )
	  || is_set( $rh_opt, 'guess-python' )
	  || is_set( $rh_opt, 'guess-pike' )
	  || is_set( $rh_opt, 'guess-ruby' )
	  || is_set( $rh_opt, 'guess-common' )
	  || is_set( $rh_opt, 'guess-data' )
	  || is_set( $rh_opt, 'guess-doc' )
	  || is_set( $rh_opt, 'guess-dev' )
	  || is_set( $rh_opt, 'guess-lib' )
	  || is_set( $rh_opt, 'guess-custom' );

	# test if a target is set
	if (   ( !@{ $rh_opt->{'package'} } )
		&& ( !is_set( $rh_opt, 'all' ) )
		&& ( !$is_guess ) )
	{

		# default behavior if no targets selected
		debug('set target to guess-lib (default)');
		$rh_opt->{'guess-lib'} = \$true;
	}
	return;
}
#########################################################
# parse command line exclude option
sub build_excluded($$) {
	my $rh_opt      = shift @_;
	my $rh_excluded = shift @_;

	debug('build_excluded');
	if ( $rh_opt->{'exclude'} ) {

		# allow comma separated options on tags option
		# and build a hash for faster access
		my @liste_ex = get_from_command_line( @{ $rh_opt->{'exclude'} } );
		foreach my $ex (@liste_ex) {
			$rh_excluded->{$ex} = 1;
			debug("build_excluded : exclude $ex");
		}
	}

   # to be passed to load_data;
   # be carefull : we use here a new key : excluded , not exclude (command line)
	$rh_opt->{'excluded'} = $rh_excluded;
	return;
}
#########################################################
sub get_package_info($) {
	my $pac = shift @_;

	my $cmd = "rpm -qil $pac";
	## no critic ( ProhibitBacktickOperators );
	my @res = `$cmd`;
	## use critic
	chomp @res;

	return @res;
}
#########################################################
sub get_package_summary($) {
	my $pac = shift @_;

	my $cmd = "rpm -q --queryformat '%{SUMMARY}' $pac";
	## no critic ( ProhibitBacktickOperators );
	my $res = `$cmd`;
	## use critic

	return $res;
}
#########################################################
# return true is rpm allow SUGGEST tags
sub is_suggests() {

	my $cmd = 'rpm -q --querytags ';
	## no critic ( ProhibitBacktickOperators );
	my @res = `$cmd`;
	## use critic
	foreach my $tag (@res) {
		chomp $tag;
		if ( $tag =~ m/^SUGGESTS/ ) {
			return 1;
		}
	}
	return 0;
}
#########################################################

1;