findC.pl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/perl
  2. # Copyright (C) 2016 Martin Thomas <mtdev@hamtam.de>
  3. # LGPL version 2 or later.
  4. use strict;
  5. use warnings;
  6. use IO::Dir;
  7. use File::Find qw(find);
  8. use Encode::Guess;
  9. my ($dir, $directory, $f, $w, $tmp);
  10. my (@files, @dirs, $file, $filename);
  11. my $header;
  12. my $files;
  13. my $encoding;
  14. my @copyright;
  15. my @copyrightout;
  16. my @uniqcpr;
  17. my @output;
  18. my $i;
  19. $encoding = ":encoding(UTF-8)";
  20. $encoding = "";
  21. $directory="./";
  22. $header = "Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\n";
  23. $header .= "Upstream-Name: uclibc-ng\n";
  24. $header .= 'Upstream-Contact: Waldemar Brodkorb <wbx@uclibc-ng.org>'."\n";
  25. $header .= "Source: git://uclibc-ng.org/git/uclibc-ng\n\n";
  26. # my $emailregex='\b[[:alnum:]._%+-]+@[[:alnum:].-]+.[[:alpha:]]{2,6}\b';
  27. sub list_dirs {
  28. my @dirs = @_;
  29. my @files;
  30. find({ wanted => sub { push @files, $_ } , no_chdir => 1 }, @dirs);
  31. return @files;
  32. }
  33. @files=list_dirs($directory);
  34. foreach $file (@files) {
  35. if ( -f $file ){
  36. # $encoding = guess_encoding($file);
  37. open(my $fh, "< $encoding", $file)
  38. or die "Could not open file '$file' $!";
  39. while (my $row = <$fh>) {
  40. chomp $row;
  41. if ($row =~ m/[Cc]opyright / )
  42. {
  43. $row =~ s/^[\s\/\*#!;.\"\\]*//; #remove leading
  44. $row =~ s/\s+$//; #remove trailing
  45. push @copyright, { file => $file, raw => $row};
  46. last;
  47. }
  48. }
  49. close $fh
  50. }
  51. }
  52. #sort raw
  53. @copyrightout = sort { $a->{raw} cmp $b->{raw} } @copyright;
  54. $tmp="";
  55. $i=-1;
  56. foreach (@copyrightout) {
  57. if ( $tmp eq $_->{'raw'} )
  58. {
  59. $output[$i]{"files"} .= "\n"." $_->{'file'}";
  60. }
  61. else
  62. {
  63. ++$i;
  64. $output[$i]{"header"} .= "Copyright: $_->{'raw'}\n";
  65. $output[$i]{"header"} .= "License: GNU Lesser General Public License 2.1\n";
  66. $output[$i]{"files"} .= "Files: ".$_->{'file'};
  67. }
  68. $tmp=$_->{'raw'};
  69. }
  70. print "$header";
  71. $i=0;
  72. foreach (@output) {
  73. print "$output[$i]->{'files'}\n";
  74. print "$output[$i]->{'header'}\n";
  75. ++$i;
  76. }