File: cleanup.pl

package info (click to toggle)
libopengl-perl 0.66%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,932 kB
  • sloc: perl: 9,796; ansic: 6,279; makefile: 101; sh: 48
file content (91 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl -w

our $exts =
{
  h => 1,
  c => 1,
  xs => 1,
  pl => 1,
  pm => 1,
  pod => 1,
  ppd => 1,
  ppm => 1,
  mak => 1,
  nff => 1,
  yml => 1,
  arb => 1,
  cg => 1,
  glsl => 1,
  txt => 1
};

my($path) = @ARGV;
die qq
{
  USAGE: cleanup.pl FILE_PATH | DIRECTORY

} if (!$path);

clean_path($path);
exit 0;


sub clean_path
{
  my($dir) = @_;

  if (!-d $dir)
  {
    clean_file($dir);
    return;
  }
  return if (!opendir(DIR,$dir));

  foreach my $file (readdir(DIR))
  {
    next if ($file =~ m|^\.|);
    my $path = "$dir/$file";
    clean_path($path);
  }
  closedir(DIR);
}

sub clean_file
{
  my($path) = @_;

  if ($path =~ m|~$|)
  {
    unlink($path);
    return;
  }

  if ($path =~ m|\.([^/\.]+)$|)
  {
    my $ext = lc($1);
    return if (!$exts->{$ext} &&
      $path !~ m|Makefile$| &&
      $path !~ m|readme\.$ext|i);
  }

  if (!open(FILE,$path))
  {
    print "Unable to read '$path'\n";
    return;
  }

  my @data = <FILE>;
  close(FILE);

  my $data;
  foreach my $line (@data)
  {
    $line =~ s|[\n\r]+||gs;
    $data .= "$line\n";
  }

  die "Unable to write to $path\n" if (!open(FILE,">$path"));
  print FILE $data;
  close(FILE);
  print "Cleaned '$path'\n";
}