File indexing completed on 2024-07-21 04:36:26

0001 #!/usr/bin/perl
0002 #
0003 # SPDX-FileCopyrightText: 2019 Thomas Baumgart <tbaumgart@kde.org>
0004 # SPDX-License-Identifier: GPL-2.0-or-later
0005 #
0006 # ---
0007 #
0008 # Figure out the -devel packages needed to compile KMyMoney
0009 #
0010 # Must be started in a git repo in the base directory
0011 
0012 
0013 use Getopt::Long;
0014 
0015 my $packageManager;
0016 my %headerNames;
0017 my %packageNames;
0018 my %nonPackagedFiles;
0019 my @blacklistedPatterns;
0020 my @packageManagers;
0021 
0022 # don't report old stuff and our own files
0023 @blacklistedPatterns = ("KDELibs4Support", "mymoney");
0024 
0025 # the directories where to search for header files
0026 @packageIncludeDirectory = ("/usr/include", "/usr/local/include");
0027 
0028 # the package managers supported
0029 %packageManagers = ("rpm" => \&findPackage_rpm);
0030 
0031 sub printAvailablePackageManagers()
0032 {
0033     print "The following package managers are available:\n\n";
0034     for $p (keys %packageManagers) {
0035         print "$p ";
0036     }
0037     print "\n";
0038 }
0039 
0040 sub isBlackListed($)
0041 {
0042     my $absoluteFilename = shift @_;
0043     for $pattern (@blacklistedPatterns) {
0044         # print "Check blacklisted $pattern in $absoluteFilename\n";
0045         if ($absoluteFilename =~ /$pattern/) {
0046             # print "$absoluteFilename is blacklisted\n";
0047             return 1;
0048         }
0049     }
0050     return 0;
0051 }
0052 
0053 sub getAbsoluteFilenames()
0054 {
0055     for $h (keys %headerNames) {
0056         print "Searching $h\n";
0057         for $incDir (@packageIncludeDirectory) {
0058             if ($h =~ /^(.+)\/([^\/]+)$/) {
0059                 my $name = $2;
0060                 # include contains a (partial) path
0061                 open(ABS, "find $incDir -name $name |") or die;
0062                 while (<ABS>) {
0063                     chomp($_);
0064                     if (index($_, $h) != -1) {
0065                         next  if (isBlackListed($_) == 1);
0066                         $headerNames{$h} = $_;
0067                     }
0068                 }
0069                 close ABS;
0070 
0071             } else {
0072                 open(ABS, "find $incDir -name $h |") or die;
0073                 while (<ABS>) {
0074                     chomp($_);
0075                     next  if (isBlackListed($_) == 1);
0076                     $headerNames{$h} = $_;
0077                 }
0078                 close ABS;
0079             }
0080         }
0081     }
0082 }
0083 
0084 # a package manager must return an empty string ""
0085 # in case it did not find a package or the package name
0086 sub findPackage_rpm($)
0087 {
0088     my $absoluteFilename = shift @_;
0089     my $packageName = "";
0090     open(RPM, "rpm -qf $absoluteFilename |") or die();
0091     while (<RPM>) {
0092         chomp($_);
0093         if ($_ !~ /^file /) {
0094             $packageName = $_;
0095         }
0096     }
0097     close RPM;
0098     return $packageName
0099 }
0100 
0101 
0102 # add more package managers above this line and don't forget to add
0103 # them to the packageManagers hash in the config section above
0104 
0105 # --------------------------------------------
0106 # Here starts main()
0107 # --------------------------------------------
0108 
0109 GetOptions('packageManager=s' => \$packageManager);
0110 
0111 if ($packageManager eq "") {
0112     print "No package manager selected: Please specify with option -p (e.g. -p rpm)\n";
0113     printAvailablePackageManagers();
0114     exit 1;
0115 }
0116 
0117 for $p (keys %packageManagers) {
0118     if ($p eq $packageManager) {
0119         $usePackageManager = $p;
0120         last;
0121     }
0122 }
0123 
0124 if ($usePackageManager eq "") {
0125     print "Selected package manager '$packageManager' is not supported. Please implement.\n";
0126     printAvailablePackageManagers();
0127     exit 1;
0128 }
0129 
0130 
0131 open(INCLUDES, "git grep -h '#include <' | sort | uniq |") or die;
0132 # the next line is for debugging only
0133 # open(INCLUDES, "cat includes |") or die;
0134 while(<INCLUDES>) {
0135     # skip commented includes
0136     next if ($_ =~ /^\s*\/\/\s*#inc/);
0137     $_ =~ /<([^>]+)>/;
0138     $headerNames{$1} = $1;
0139 }
0140 close INCLUDES;
0141 
0142 getAbsoluteFilenames();
0143 
0144 for $h (keys %headerNames) {
0145     my $pkg;
0146     print "Locate package for $h - ";
0147     $pkg = $packageManagers{$usePackageManager}->($headerNames{$h});
0148     print "$pkg\n";
0149     # print "$h - $headerNames{$h}- $pkg\n";
0150     if ($pkg eq "") {
0151         $nonPackagedFiles{$headerNames{$h}} = $pkg;
0152     } else {
0153         $packageNames{$pkg} = $pkg;
0154     }
0155 
0156 }
0157 
0158 print "\n\n";
0159 print "List of required packages:\n";
0160 print "--------------------------\n";
0161 
0162 for $p (sort keys %packageNames) {
0163     print "$p\n";
0164 }
0165 
0166 print "\n\n";
0167 print "List of non-packaged files:\n";
0168 print "---------------------------\n";
0169 
0170 for $p (sort keys %nonPackagedFiles) {
0171     print "$p\n";
0172 }