forked from OTRS/module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-linker.pl
executable file
·256 lines (212 loc) · 7.43 KB
/
module-linker.pl
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl
# --
# module-tools/module-linker.pl
# - script for linking OTRS modules into framework root
# Copyright (C) 2001-2012 OTRS AG, http://otrs.org/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
use strict;
use warnings;
use File::Find;
use Cwd;
my $Action = shift || '';
if ( $Action !~ m{^(install|uninstall)$} ) {
Usage("ERROR: action (ARG0) must be either 'install' or 'uninstall'!");
exit 2;
}
if ( $^O =~ 'MSWin' ) {
require Win32;
# mklink is only supported on Vista, Win2008 or later.
my ( $VersionString, $VersionMajor, $VersionMinor, $VersionBuild, $VersionID )
= Win32::GetOSVersion();
if ( $VersionID < 2 || ( $VersionID = 2 && $VersionMajor < 6 ) ) {
print
"If you want to use the module-linker script on Windows, you should use Vista, Win2008 or later";
exit 2;
}
# in order for mklink to work, UAC must be elevated.
if ( !Win32::IsAdminUser() ) {
print "To be able to create symlinks, you'll have to start the script with UAC enabled.\n";
print "(right-click CMD, select \'Run as administrator\').\n";
exit 2;
}
}
my $Directory = getcwd();
my $Source = shift;
if ( !defined $Source || !-d $Source ) {
Usage("ERROR: invalid source module path '$Source'");
exit 2;
}
if ( substr( $Source, 0, 1 ) ne '/' ) {
$Source = "$Directory/$Source";
print "NOTICE: using absolute module path '$Source'\n";
}
my $Dest = shift;
if ( !defined $Dest || !-d $Dest ) {
Usage("ERROR: invalid framework-root path '$Dest'");
exit 2;
}
if ( substr( $Dest, 0, 1 ) ne '/' ) {
$Dest = "$Directory/$Dest";
print "NOTICE: using absolute framework root path '$Dest'\n";
}
# remove any trailing slashes from source and destination paths
if ( substr( $Source, -1, 1 ) eq '/' ) {
chop $Source;
}
if ( substr( $Dest, -1, 1 ) eq '/' ) {
chop $Dest;
}
if ( $Action eq 'install' ) {
find( \&InstallHandler, $Source );
}
elsif ( $Action eq 'uninstall' ) {
find( \&UninstallHandler, $Source );
}
sub Usage {
my ($Message) = @_;
print STDERR <<"End-of-Here";
$Message
USAGE:
$0 install <source-module-path> <otrs-framework-root>
or
$0 uninstall <source-module-path> <otrs-framework-root>
End-of-Here
return;
}
sub InstallHandler {
# skip (i.e. do not enter) folders named 'CVS' or '.svn'
if ( m{^(CVS|\.svn)$} && -d ) {
$File::Find::prune = 1;
return;
}
# skip anything that starts with a dot (except for '.')
# [this e.g. bypasses Eclipse project files like '.includepath' and '.project']
if (m{^\..+$}) {
$File::Find::prune = 1;
return;
}
# print "handling '$File::Find::name'\n";
# compute full target name (by replacing source- with destination-path)
my $Target = $File::Find::name;
$Target =~ s{^$Source}{$Dest};
if ( -d $File::Find::name ) {
return if -d $Target;
print "NOTICE: mkdir $Target\n";
mkdir($Target);
}
else {
if ( -l $Target ) {
# skip if already linked correctly
if ( readlink($Target) eq $File::Find::name ) {
print "NOTICE: link from $Target is ok\n";
return;
}
# remove link to some different file
unlink($Target) or die "ERROR: Can't unlink symlink: $Target";
}
# backup target if it already exists as a file
if ( -f $Target ) {
if ( rename( $Target, "$Target.old" ) ) {
print "NOTICE: created backup for original file: $Target.old\n";
}
else {
die "ERROR: Can't rename $Target to $Target.old: $!";
}
}
# link source into target
if ( $^O =~ 'MSWin' ) {
$Target =~ s/\//\\/g;
my $Source = $File::Find::name;
$Source =~ s/\//\\/g;
if ( !-e $File::Find::name ) {
die "ERROR: No such source file: ${File::Find::name}";
}
system("mklink $Target $Source");
print "NOTICE: Link: $Source\n";
print "NOTICE: -> $Target\n";
}
else { # not Win32
if ( !-e $File::Find::name ) {
die "ERROR: No such source file: ${File::Find::name}";
}
elsif ( !symlink( $File::Find::name, $Target ) ) {
die "ERROR: Can't link ${File::Find::name} to $Target: $!";
}
else {
print "NOTICE: Link: ${File::Find::name}\n";
print "NOTICE: -> $Target\n";
}
}
}
return 1;
}
sub UninstallHandler {
# skip (i.e. do not enter) folders named 'CVS' or '.svn'
if ( m{^(CVS|\.svn)$} && -d ) {
$File::Find::prune = 1;
return;
}
# skip anything that starts with a dot (except for '.')
# [this e.g. bypasses Eclipse project files like '.includepath' and '.project']
if (m{^\..+$}) {
$File::Find::prune = 1;
return;
}
# print "handling '$File::Find::name'\n";
# compute full target name (by replacing source- with destination-path)
my $Target = $File::Find::name;
$Target =~ s{^$Source}{$Dest};
return if -d $File::Find::name;
if ( -l $Target ) {
# remove link only if it points to our current source
if ( readlink($Target) eq $File::Find::name ) {
unlink($Target) or die "ERROR: Can't unlink symlink: $Target";
print "NOTICE: link from $Target removed\n";
}
# restore target if there is a backup
if ( -f "$Target.old" ) {
if ( rename( "$Target.old", $Target ) ) {
print "NOTICE: Restored original file: $Target\n";
}
else {
die "ERROR: Can't rename $Target.old to $Target: $!";
}
}
}
return 1;
}
=head1 NAME
module-linker.pl - simplified reimplementation of link.pl and remove-links.pl
based on File::Find
=head1 SYNOPSIS
module-linker.pl install <source-module-folder> <otrs-folder>
or
module-linker.pl uninstall <source-module-folder> <otrs-folder>
This script either installs a given OTRS module into the OTRS framework (by creating
appropriate links) or uninstalls the module (by removing those links again).
The intention of this reimplementation is to have simpler code and to make the
script slightly more robust against unintended inclusion of unwanted files,
e.g. files contained in repository meta folders ('CVS' or '.svn') and IDE-specific
files (.project and the like).
=head1 TERMS AND CONDITIONS
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=head1 VERSION
$Revision: 1.3 $
=cut