-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonLicenseStrategy.pm
145 lines (95 loc) · 2.61 KB
/
PythonLicenseStrategy.pm
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
#!/usr/bin/perl
use warnings;
use strict;
package PythonLicenseStrategy;
use LicenseStrategy;
our @ISA = ( "LicenseStrategy" );
sub new()
{
my ( $class ) = @_;
my $self = $class->SUPER::new();
bless( $self , $class );
return $self;
}
sub replaceHeader
{
my $self = shift;
my $file = shift;
Logger->new()->log( 3 , "replace header: " . $file->{ _filePath } );
##.*?Copyright.*Wind River.*?##
if ( $file->{ _fileContentsString } =~ m/#*[^\n]*Copyright.*Wind River[^\n]*/is )
{
my $license = $self->prepareLicense( $file->{ _filePath } );
$file->{ _fileContentsString } =~ s/(#*[^\n]*Copyright.*Wind River[^\n]*)/$license/is;
Logger->new()->log( 1 , "REPLACED HEADER: " . $file->{ _filePath } . "\n" );
return 1;
}
#if ( $file->{ _fileContentsString } =~ m/\"\"\".*?copyright.*?\"\"\"/is )
# {
# my $license = $self->prepareLicense( $file->{ _filePath } );
#
# $file->{ _fileContentsString } =~ s/(\"\"\".*?copyright.*?\"\"\")/$license/is;
# Logger->new()->log( 1 , "REPLACED HEADER: " . $file->{ _filePath } . "\n" );
#
# return 1;
# }
return 0;
}
sub addHeader
{
my $self = shift;
my $file = shift;
Logger->new()->log( 3 , "add header: " . $file->{ _filePath } );
my $license = $self->prepareLicense( $file->{ _filePath } );
if( $file->{ _fileContentsString } =~ m/\Q$license/s )
{
return 0;
}
if( ${$file->{ _fileContentsArray }}[0] =~ m/(^#!.*)/)
{
my $search = ${$file->{ _fileContentsArray }}[0];
my $replace = ${$file->{ _fileContentsArray }}[0] . "\n\n" . $license;
if ( $file->{ _fileContentsString } =~ s/$search/$replace/s )
{
Logger->new()->log( 1 , "ADDED HEADER: " . $file->{ _filePath } . "\n" );
}
}
else
{
$file->{ _fileContentsString } = $license . $file->{ _fileContentsString };
Logger->new()->log( 1 , "ADDED HEADER: " . $file->{ _filePath } . "\n" );
}
return 1;
}
sub replaceDefine
{
return 0;
}
sub prepareLicense
{
my $self = shift;
my $filename = shift;
my $licensePath = LicenseStrategy::getLicense($filename);
Logger->new()->log( 3 , "use license: $licensePath" );
my @licenseText = FileManager->new()->readFile( $licensePath );
my $start_year = $self->{ _StartYear };
my $curr_year = $self->{ _CurrYear };
my $retVal = "";
foreach my $line( @licenseText )
{
$line =~ s/STARTDATE/\Q$start_year/g;
$line =~ s/ENDDATE/\Q$curr_year/g;
$retVal .= "## " . $line . "\n";
}
#my $retVal = "\"\"\"\n";
#foreach my $line( @licenseText )
#{
# $line =~ s/STARTDATE/\Q$start_year/g;
# $line =~ s/ENDDATE/\Q$curr_year/g;
#
# $retVal .= $line . "\n";
#}
#$retVal .= "\"\"\"\n\n";
return $retVal;
}
1;