Attachment 'addCopyright.pl'

Download

   1 #!/usr/bin/perl
   2 #
   3 # A program that adds a generic copyright and warranty statement
   4 # to a file.
   5 #
   6 
   7 use File::Basename;
   8 use strict;
   9 use Getopt::Long;
  10 
  11 use vars qw($silent);
  12 
  13 main();
  14 
  15 sub main
  16 {
  17 	my $author = "";
  18 	my $headerFile = "";
  19 	my $force = 0;
  20 	my $extract = 0;
  21 	$silent = 0;
  22 	my $result = GetOptions( "author|a=s" => \$author,
  23 	                         "header|h=s" => \$headerFile,
  24 	                         "force|f" => \$force,
  25 	                         "extract|x" => \$extract,
  26                                  "silent|s" => \$silent);
  27 	
  28 	die "USAGE: addCopyright.pl -a <author> [-h <header file>] [--extract] [--force] [--silent] <file> <file2> ... \n\nHeader files available in ~/bin/copyrights.\n"
  29 		if ( $author eq "" );
  30 
  31 	my $header = "";
  32 	if ( $headerFile ne "" ) 
  33 	{
  34 		open HEAD, "$headerFile" or die "Couldn't open header: '$headerFile'\n";
  35 		while (<HEAD>) { $header .= $_ ; }
  36 		close HEAD;
  37 	}
  38 
  39 	foreach (@ARGV) { addCopy( $_, $author, $header, $force, $extract ); }
  40 }
  41 
  42 sub addCopy
  43 {
  44 	my $filename = shift;
  45 	my $file = basename($filename); 
  46 	my $author = shift;
  47 	my $header = shift;
  48 	my $force = shift;
  49 	my $extract = shift;
  50 
  51 	my $year = (localtime)[5] + 1900;
  52 
  53 	my $startComment = "";
  54 	my $c = "";
  55 	my $endComment = "";
  56 
  57 	my $extractBegin = '';
  58 	my $extractEnd = '';
  59 	my $extractLine = '';
  60 
  61 	my $valid = 0;
  62 
  63 	if ( $filename =~ /.+\.html$/i ||  
  64    		  $filename =~ /.+\.htm$/i  || 
  65    		  $filename =~ /.+\.xml$/i  || 
  66    		  $filename =~ /.+\.dtd$/i  
  67 		 )
  68 	{
  69 		$startComment = "<!--";
  70 		$c            = "  - ";
  71 		$endComment   = "  -->";
  72 		$valid = 1;
  73 
  74 		$extractBegin = '\<\!--';
  75 		$extractEnd   = '--\>';
  76 	}
  77 	elsif ( $filename =~ /.+\.pl$/i ||  
  78 		     $filename =~ /.+\.pm$/i || 
  79 		     $filename =~ /.+\.sh$/i || 
  80 		     $filename =~ /.+\.ksh$/i || 
  81 		     $filename =~ /.+\.bash$/i || 
  82 		     $filename =~ /.+\.csh$/i || 
  83 		     $filename =~ /.+\.tcsh$/i || 
  84 		     $filename =~ /.+\.py$/i || 
  85 		     $filename =~ /^makefile$/i 
  86 		 ) 
  87 	{
  88 		$startComment = "#=============================================================================";
  89 		$c            = "# ";
  90 		$endComment   = "#============================================================================";
  91 		$valid = 1;
  92 
  93 		$extractLine = '\#';
  94 	}
  95 	elsif ( $filename =~ /.+\.c$/i ||  
  96   		     $filename =~ /.+\.h$/i ||
  97 		     $filename =~ /.+\.cpp$/i ||
  98 		     $filename =~ /.+\.cxx$/i ||
  99 		     $filename =~ /.+\.cc$/i ||
 100 		     $filename =~ /.+\.css$/i
 101 		 ) 
 102 	{
 103 		$startComment = "/*****************************************************************************\\ ";
 104 		$c            = " * ";
 105 		$endComment   = "\\*****************************************************************************/";
 106 		$valid = 1;
 107 
 108 		$extractBegin = '\/\*';
 109 		$extractEnd   = '\*\/';
 110 		$extractLine  = '\/\/';
 111 	}
 112 	elsif ( $filename =~ /.+\.java$/i )
 113 	{
 114 #		$startComment = "//============================================================================";
 115 #		$c            = "// ";
 116 #		$endComment   = "//============================================================================";
 117 		$startComment = "/*";
 118 		$c            = " ";
 119 		$endComment   = "*/";
 120 		$valid = 1;
 121 
 122 		$extractBegin = '\/\*';
 123 		$extractEnd   = '\*\/';
 124 		$extractLine  = '\/\/';
 125 	}
 126 	elsif( $filename =~ /.+\.1$/ )
 127 	{
 128 		$startComment = ".\\" . "\"";
 129 		$c            = ".\\" . "\"";
 130 		$endComment   = ".\\" . "\"";
 131 		$valid = 1;
 132 
 133 		$extractLine  = '\.\\\\' . '\"';
 134 	}
 135 	elsif( $filename =~ /.+\.sql$/i )
 136 	{
 137 		$startComment = "------------------------------------------------------------------------------- ";
 138 		$c            = "--";
 139 		$endComment   = "------------------------------------------------------------------------------- ";
 140 		$valid = 1;
 141 
 142 		$extractLine  = '\.\.';
 143 	}
 144 	else
 145 	{
 146 		printMessage( "File suffix for: \'$filename\' doesn't match " . 
 147 		              "any known patterns!\nNot adding copyright!\n\n" );
 148 		return;
 149 	}
 150 
 151 	# if the header isn't set, use this basic default
 152 	if ( $header eq "" ) 
 153 	{
 154 		$header =
 155 "
 156 $startComment
 157 $c
 158 $c file:  $file
 159 $c
 160 $c Copyright (c) $year, $author
 161 $c All rights reverved.
 162 $c
 163 $endComment 
 164 
 165 ";
 166 	}
 167 	else 
 168 	{
 169 		$header = eval "$header";
 170 	}
 171 
 172 
 173 	# actually write add the header to the file 
 174 	if ( $valid )
 175 	{
 176 		my @f = ();
 177 		open FILE, "$filename" or die "couldn't open file \'$filename\'\n$!";
 178 		while (<FILE>) { push @f, $_; }
 179 		close FILE;
 180 
 181 		# First, extract any existing copyright if desired.
 182 		if ( $extract ) 
 183 		{
 184 			@f = extractBlockCopyright($extractBegin,$extractEnd,\@f);
 185 			@f = extractLineCopyright($extractLine,\@f);
 186 		}
 187 
 188 		# Check for an existing copyright. 
 189 		if ( !$force )
 190 		{
 191 			checkExisting($filename, \@f);
 192 		}
 193 
 194 		printMessage( "Adding copyright to file: \'$filename\'\n" ); 
 195 
 196 		# check to see if first line of file is a shebang (e.g. #!/bin/bash)
 197 		my $shebang = 0;
 198 		if ( $f[0] =~ /^\#\!\/.+/ ) { $shebang = 1; }
 199 
 200 		# ok, add the copyright
 201 		open FILE, ">$filename" or die "couldn't open file to write \'$filename\'\n$!";
 202 
 203 		if ( $shebang ) { print FILE shift @f; }
 204 		print FILE $header;
 205 		foreach my $x (@f) { print FILE $x; }
 206 
 207 		close FILE;
 208 	}
 209 }
 210 
 211 sub printMessage
 212 {
 213 	my $message = shift;
 214 
 215 	unless( $silent ) { print $message; }
 216 }
 217 
 218 sub checkExisting
 219 {
 220 	my $filename = shift;
 221 	my $f = shift;
 222 	foreach (@$f) 
 223 	{ 
 224 		if ( $_ =~ /copyright/i || $_ =~ /license/i )
 225 		{
 226 			printMessage( "It appears that file: \'$filename\' may ".
 227 			          "already have a copyright notice.\n".
 228 				  "NOT adding a copyright.\n".
 229 				  "If this is a mistake, use the --force option\n\n" );
 230 			exit 1;
 231 		}
 232 	}
 233 }
 234 
 235 
 236 sub extractBlockCopyright {
 237 	
 238 	my $beginStyle = shift;
 239 	my $endStyle = shift;
 240 	my $lines = shift;
 241 
 242 	if ( $beginStyle eq "" || $endStyle eq "" ) { return @$lines; }
 243 
 244 	my @goodlines = ();
 245 	my @badlines = ();
 246 	my $withinComment = 0;
 247 	my $foundCopyright = 0;
 248 
 249 	foreach (@$lines) {
 250 	
 251 		if ( !$foundCopyright && $_ =~ /$beginStyle.*$/ ) {
 252 			$withinComment = 1;
 253 			push @badlines, $_;
 254 		} elsif ( $foundCopyright && $withinComment && 
 255 		          $_ =~ /.*($endStyle)/ ) {
 256 			$withinComment = 0;
 257 			push @badlines, $_;
 258 		} elsif ( $withinComment ) {
 259 			push @badlines, $_;
 260 		} else {
 261 			push @goodlines, $_;
 262 		}
 263 
 264 		if ( $withinComment && 
 265 		     ($_ =~ /copyright/i || $_ =~ /license/i )) {
 266 			$foundCopyright = 1;
 267 		}
 268 	}
 269 
 270 	if ( $foundCopyright && $#goodlines >=0 && $#badlines >= 0 ) {
 271 		printMessage( "Found block copyright and extracted it.\n" );
 272 		return @goodlines;	
 273 	} else {
 274 		return @$lines;	
 275 	}
 276 }
 277 
 278 
 279 sub extractLineCopyright {
 280 
 281 	my $style = shift;
 282 	my $lines = shift;
 283 
 284 	if ( $style eq "" ) { return @$lines; }
 285 
 286 	my @goodlines = ();
 287 	my @badlines = ();
 288 
 289 	my $foundCopyright = 0;
 290 	my $skip = 0;
 291 
 292 	foreach (@$lines) {
 293 
 294 		if ( !$skip && $_ =~ /^\s*($style.*)$/ ) {
 295 			push @badlines, $_;
 296 		} else {
 297 			push @goodlines, $_;
 298 			if ( $#badlines >= 0 ) { $skip = 1; }
 299 		}
 300 
 301 		if ($_ =~ /copyright/i || $_ =~ /license/i ) {
 302 			$foundCopyright = 1;
 303 		}
 304 	
 305 	}
 306 
 307 	if ( $foundCopyright ) {
 308 		printMessage( "Found line copyright and extracted it.\n");
 309 		return @goodlines;
 310 	} else {
 311 		return @$lines;
 312 	}
 313 }
 314 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux