summaryrefslogtreecommitdiff
path: root/Tools/scripts/addport
blob: 101f6e952e62687e2fb68b6c116c2e34167ed3a2 (plain) (blame)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl
#
# addport - perl script that adds new ports to the
# FreeBSD Ports Collection.  Replaces easy-import.
#
# Copyright (c) 2000 Will Andrews and Michael Haro
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Original shell script & idea: Will Andrews <will@FreeBSD.org>
# Original conversion to Perl : Michael Haro <mharo@FreeBSD.org>
#
# $Id: addport,v 1.2 2000/04/02 06:21:13 will Exp $ (original shell script)
# $Id: addport,v 1.5 2000/04/22 22:19:43 mharo Exp $ (perl conversion)
# $FreeBSD$
#
# MAINTAINER=	will@FreeBSD.org
#

use Cwd "abs_path";
use Getopt::Std;
use Sys::Hostname;
use strict;

my %opts;

getopts('ad:fgh:imns:tu:', \%opts);

my $nomodules = $opts{'g'};
my $distdir = $opts{'s'} if ($opts{'s'} ne "");
my $dir = $opts{'d'};
my $h = "freefall.FreeBSD.org";
$h = $opts{'h'} if ($opts{'h'} ne "");
my $n = ""; $n = "-n" if $opts{'n'};
my $u = $ENV{USER};
$u = $opts{'u'} if ($opts{'u'} ne "");
my $more_testing = $opts{'t'};
my $interactive = $opts{'i'};
my $nomkdir = $opts{'m'};
my $addlchk = $opts{'a'};
my $nofetch = $opts{'f'};
my $currentdir = abs_path(".");

my $tmpdir;
my $repo;
my $ssh;
if( !defined $ENV{"CVS_RSH"} ) {
	$ENV{CVS_RSH} = "ssh";
}
my $make = "make";
my $portlint = `which portlint`; chomp $portlint;
my $plint_args = "-N -a -c -t";
my $perl = "perl";
my $cp = "cp";
my $mv = "mv";
my $rm = "rm";

chomp(my $myhost = lc(hostname()));
if ($myhost ne lc($h)) {
	$ssh = "$ENV{CVS_RSH} $u\@$h";
	$repo = "$u\@$h:/home/ncvs" if !$ENV{CVSROOT};
} else {
	$ssh = "";
	$repo = "/home/ncvs" if !$ENV{CVSROOT};
}
$repo = "$ENV{CVSROOT}" if $ENV{CVSROOT};
my $cvs = "cvs -d $repo";

# stuff that always happens when we start
BEGIN {
	$tmpdir=`mktemp -d -t ap`;
	chomp $tmpdir;
	if ($tmpdir eq "") {
		errx(1,"making random tmpdir didn't work, aborting.");
	}
}

# stuff that always happens when we exit
END {
	# only remove $tmpdir if it points to something in /tmp
	# this is a silly little security thing
	if (defined($rm) && defined($tmpdir)) {
		system("$rm -rf $tmpdir") if ($tmpdir =~ m,/tmp/,);
	}
}

# setup the list of commands to run on the new port(s).
my @commands;
my $passenv = "";
if ($addlchk && -f $portlint) {
	$passenv = "DISTDIR=\"$distdir\"" if -d $distdir && $myhost ne "freefall.freebsd.org";
	$passenv = "DISTDIR=\"$tmpdir\"" if $myhost eq "freefall.freebsd.org";
	$passenv = $passenv . " PORTSDIR=\"$tmpdir\"" if !$nomkdir;
	push(@commands, "$make clean check-categories");
	push(@commands, "$portlint $plint_args");
	push(@commands, "$make $passenv FETCH_BEFORE_ARGS='-btA' checksum") if !$nofetch;
	if ($more_testing) {
		push(@commands, "$make distclean");
		push(@commands, "$make build");
	}
	if (!$nomkdir) {
		chdir $tmpdir;
		print "Checking out Mk directory to ensure portlint correctness.\n";
		system("$cvs co ports/Mk") && errx(1, "Could not checkout Mk directory");
		system("mv ports/Mk Mk") && errx(1, "Could not set up Mk directory");
		chdir $currentdir;
	}
}

if ($dir eq "") {
	warnx("Need to specify a directory with -d argument!");
	usage();
	exit 1;
}

# make sure we're in the right place.
chdir $currentdir;
my @dirs = split(/\,/, $dir);
foreach my $i (@dirs) { $i = abs_path($i); }
my $portname; my $module;
foreach my $thisdir (@dirs) {
	# make double sure where we are..
	chdir $thisdir;
	# do some dir sanity checking first
	errx(1, "Please specify valid directories to import new ports from.") if $thisdir eq "";
	errx(1, "$thisdir is either not a directory or does not exist.") if (! -d $thisdir);

	print "Working with port directory $thisdir.\n";

	$portname = `basename $thisdir`;	# avoid problems with dirs containing `/' in cvs
	chomp $portname;
	if ($interactive) {
		if (prompt("Port directory name will be $portname in CVS Repo.  OK? ")) {
			do {
				$portname = query("Preferred name for port directory? ");
			} while (prompt("Is the new name $portname OK? "));
		}
	}

	chdir $thisdir or err(1, "$thisdir");

	# now run the tests on this baby.
	for (@commands) {
		system("$_") && errx(1, "'$_' had problems. aborting.");
	}

	# Get the category name and make it suitable for use with cvs
	my $category;
	$_ = `grep CATEGORIES Makefile`;
	m/\w+\W+([\w-]+)/;
	$category = $1;
	chomp $category;
	if ($interactive) {
		if (prompt("Port $portname will be put in category $category.  OK? " )) {
			do {
				$category = query("Preferred category for $portname? ");
			} while (prompt("Is the new category $category OK? "));
		}
	}
	chomp(my $cvs_category = $category);
	$cvs_category =~ s/-/_/g;
 
 	$module = $portname;
	if ($interactive) {
		if (prompt("Port will be added as module $portname.  OK? ")) {
			do {
				$module = query("Preferred module name for $module? ");
			} while (prompt("Is the new module name $module OK? "));
		}
	}
	print "We're ready to commit.\n";
	print "Source directory: $thisdir\n";
	print "Target CVS Repo directory: ports/$category/$portname\n";
	print "Modules entry: $module --> ports/$category/$portname\n";
	prompt("Adding port $portname to $category OK? ") && errx(1, "user abort requested");
	chdir $tmpdir or err(1, "$tmpdir");

	# let's get our hands dirty.
	if (! -d $category) {
		system("$cvs co -l ports_$cvs_category") && errx(1, "can't get temporary category directory, aborting.");
		system("$mv ports_$cvs_category $category");
	}
	chdir $category or err(1,"$category");
	system("$cp -PRp $thisdir .");
	system("$cvs add `find $portname -type d | grep -v CVS`") && errx(1, "cvs add for dirs failed, aborting.");
	system("$cvs add `find $portname -type f | grep -v CVS`") && errx(1, "cvs add for files failed, aborting.");

	# figure out where the port name belongs in category Makefile
	my @ports = &lsports;
	errx(1, "Error: $portname already exists in $category\'s Makefile") if (&contains($portname, @ports));
	my $port = "";
	foreach my $tmp (sort(@ports)) {
		if ($tmp gt $portname) {
			$port = $tmp;
			last;
		}
	}

	# now let's insert it
	my $cmd;
	if ($port eq "") {
		# there were no previous SUBDIR += lines, so we're going to
		# put ourselves after the last comment (we can't be after a
		# .include <bsd.port.subdir.mk> for example).
		my $lastcommentnum = &lastcomment;
		$cmd = "$lastcommentnum\n+\ni\n";
	} else {
		# OK, append ourselves in the right place, so things *stay* sorted.
		$cmd = "/^    SUBDIR += $port/\ni\n";
	}
	print "Inserting new port into $category/Makefile...\n";
	open(ED, "|ed Makefile") || die "Cannot start ed to actually insert module\n";
	print ED "$cmd    SUBDIR += $portname\n.\nw\nq\n";
	close(ED);

	# commit the actual port.
	chdir "$tmpdir/$category" or err(1, "$tmpdir/$category");
	system("$cvs $n ci Makefile $portname") && errx(1, "cvs commit failed, aborting.");
	if (!$nomodules) {
		system("$ssh $perl /usr/local/bin/modulesupdate $module ports/$category/$portname") && errx(1, "adding port to modules failed, aborting.");
	}
}

print <<EOF;
You're done! The new port $portname has been completely imported in
the tree. Don't forget to add the creator's name and email address to
the Contributors' List if they are not already there.
EOF

sub warnx {
	my ($msg) = @_;
	print STDERR $0 . ": " . $msg . "\n";
}

sub err {
	my ($ex, $msg) = @_;

	warnx("WARNING: err called incorrectly") if (($ex !~ m/^\d+/) || ($msg eq ""));
	print STDERR $0 . ": " . $msg . ": $!\n";
	exit $ex;
}

sub errx {
	my ($ex,$msg) = @_;

	warnx("WARNING: errx called incorrectly") if (($ex !~ m/^\d+/) || ($msg eq ""));
	print STDERR $0 . ": " . $msg . "\n";
	exit $ex;
}

sub prompt {
	my ($msg) = @_;
	my $reply = query($msg);
	return 0 if ($reply =~ m/^[Yy]/);
	return 1 if ($reply =~ m/^[Nn]/);
}

sub query {
	my ($msg) = @_;

	print "$msg";
	my $reply = <>;
	chomp $reply;
	return $reply;
}

sub usage {
#addport,v \$Revision: 1.5 $
print <<EOF;
authors: <will\@FreeBSD.org>, <mharo\@FreeBSD.org>

SYNOPSIS
	$0 [-h host] [-u user] [-s distdir] [-afimnt] -d directory

	Where "directory" contains the comma-delimited list
	of root directories of new ports that you wish to
	add to the Ports Collection.  The name of this directory
	*WILL* matter in regards to the repository!

OPTIONS
	-a		Perform checks on the port to make sure
			there are no problems.  Recommended.
	-f		Do not fetch the distfile.
	-h host		Use a cvshost besides freefall.FreeBSD.org.
	-i		Interactive mode; allow more control over
			where things are placed.  This is required in
			order to change things like module names etc.
	-m		Do not checkout ports/Mk (needed for support
			of portlinting etc).
	-n		Do not actually commit anything.
	-s distdir	Use a different directory besides the default,
			for downloading distfiles.  This defaults to the
			temporary directory set up on freefall.
	-t		Do more port testing.  Requires -a.
	-u user		Use a different username (default: $u).

ENVIRONMENT VARIABLES
	$0 supports the following environment variables:

	CVS_RSH - Command to use when connecting to CVS host.
	CVSROOT - Location of CVS repository.
	USER	- Username of user invoking $0.

EXAMPLES
	% addport -n -d greatgame,helpfuldev,shoot
		Will show what happens but not actually commit ports
	named "greatgame", "helpfuldev", and "shoot".

	% addport
		Displays this message.  :-)

EOF
}

sub contains {
    # look if the first parameter is contained in the list following it
    my ($item, @list) = @_;

    foreach my $i (@list) {
	return 1 if $i eq $item;
    }
    return 0;
}

sub lsports {
    my @rv = ();

    open(F, "Makefile") || die "can't open Makefile: $!";
    while(<F>) {
	chomp;
	chomp;
	next if $_ !~ m/SUBDIR/;
	s/^[ \t]+SUBDIR[ \t]+\+?=[\ \t]+//;
	push(@rv, $_);
    }
    close(F);

    return @rv;
}

# this finds the last comment in the Makefile
sub lastcomment {
	my $num = 0;
	my $diff = 0;

	open(F, "Makefile");
	while(<F>) {
		chomp;
		if ($_ =~ m/^#/) {
			$num += $diff;
			$num++;
			$diff = 0;
		} else {
			$diff += 1;
		}
		next;
	}
	return $num;
}