-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemoduledeb.pl
executable file
·548 lines (516 loc) · 15.7 KB
/
makemoduledeb.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/perl
# makemoduledeb.pl
# Create a Debian package for a webmin or usermin module or theme
use strict;
use warnings;
use POSIX;
use Term::ANSIColor qw(:constants);
use 5.010;
my $licence = "BSD";
my $email = "Jamie Cameron <jcameron\@webmin.com>";
my $target_dir = "/tmp";
my $tmp_dir = "/tmp/debian-module";
my $debian_dir = "$tmp_dir/DEBIAN";
my $control_file = "$debian_dir/control";
my $preinstall_file = "$debian_dir/preinst";
my $postinstall_file = "$debian_dir/postinst";
my $preuninstall_file = "$debian_dir/prerm";
my $postuninstall_file = "$debian_dir/postrm";
my $copyright_file = "$debian_dir/copyright";
my $changelog_file = "$debian_dir/changelog";
my $files_file = "$debian_dir/files";
#-r "/etc/debian_version" ||
# die RED, "makemoduledeb.pl must be run on Debian", RESET;
# Parse command-line args
my ($force_theme, $url, $upstream, $debdepends, $no_prefix, $force_usermin,
$release, $allow_overwrite, $final_mod, $dsc_file, $dir, $ver, @exclude);
while(@ARGV) {
my $a = shift(@ARGV);
if ($a eq "--force-theme") {
$force_theme = 1;
}
elsif ($a eq "--licence" || $a eq "--license") {
$licence = shift(@ARGV);
}
elsif ($a eq "--email") {
$email = shift(@ARGV);
}
elsif ($a eq "--url") {
$url = shift(@ARGV);
}
elsif ($a eq "--upstream") {
$upstream = shift(@ARGV);
}
elsif ($a eq "--deb-depends") {
$debdepends = 1;
}
elsif ($a eq "--no-prefix") {
$no_prefix = 1;
}
elsif ($a eq "--usermin") {
$force_usermin = 1;
}
elsif ($a eq "--target-dir") {
$target_dir = shift(@ARGV);
}
elsif ($a eq "--dir") {
$final_mod = shift(@ARGV);
}
elsif ($a eq "--release") {
$release = shift(@ARGV);
}
elsif ($a eq "--allow-overwrite") {
$allow_overwrite = 1;
}
elsif ($a eq "--dsc-file") {
$dsc_file = shift(@ARGV);
}
elsif ($a eq "--exclude") {
push(@exclude, shift(@ARGV));
}
elsif ($a =~ /^\-\-/) {
print STDERR "Unknown option $a\n";
exit(1);
}
else {
if (!defined($dir)) {
$dir = $a;
}
else {
$ver = $a;
}
}
}
# Validate args
if (!$dir) {
print "usage: ", CYAN, "makemoduledeb.pl ";
print YELLOW, "[--force-theme]\n";
print " [--deb-depends]\n";
print " [--no-prefix]\n";
print " [--licence name]\n";
print " [--email 'name <address>']\n";
print " [--upstream 'name <address>']\n";
print " [--provides provides]\n";
print " [--usermin]\n";
print " [--target-dir directory]\n";
print " [--dir directory-in-package]\n";
print " [--allow-overwrite]\n";
print " [--dsc-file file.dsc]\n";
print CYAN, " <module> ";
print YELLOW, "[version]\n", RESET;
exit(1);
}
chop(my $par = `dirname $dir`);
chop(my $source_mod = `basename $dir`);
my $source_dir = "$par/$source_mod";
my $mod = $final_mod || $source_mod;
if ($mod eq "." || $mod eq "..") {
die "directory must be an actual directory (module) name, not \"$mod\"";
}
# Is this actually a module or theme directory?
my ($depends, $prefix, $desc, $product, $iver, $istheme, $post_config);
-d $source_dir || die RED, "$source_dir is not a directory", RESET;
my (%minfo, %tinfo);
if (read_file("$source_dir/module.info", \%minfo) && exists($minfo{'desc'})) {
if (exists($minfo{'depends'})) {
$depends = join(" ", map { s/\/[0-9\.]+//; $_ }
grep { !/^[0-9\.]+$/ }
split(/\s+/, $minfo{'depends'}));
}
if ($minfo{'usermin'} && (!$minfo{'webmin'} || $force_usermin)) {
$prefix = "usermin-";
$desc = "Usermin module $minfo{'desc'}";
$product = "usermin";
}
else {
$prefix = "webmin-";
$desc = "Webmin module $minfo{'desc'}";
$product = "webmin";
}
$iver = $minfo{'version'};
$post_config = 1;
}
elsif (read_file("$source_dir/theme.info", \%tinfo) && $tinfo{'desc'}) {
if ($tinfo{'usermin'} && (!$tinfo{'usermin'} || $force_usermin)) {
$prefix = "usermin-";
$desc = "Usermin theme $tinfo{'desc'}";
$product = "usermin";
}
else {
$prefix = "webmin-";
$desc = "Webmin theme $tinfo{'desc'}";
$product = "webmin";
}
$iver = $tinfo{'version'};
$istheme = 1;
$post_config = 0;
}
else {
die "$source_dir does not appear to be a webmin module or theme";
}
$prefix = "" if ($no_prefix);
my $usr_dir = "$tmp_dir/usr/share/$product";
my $ucproduct = ucfirst($product);
$upstream ||= $email;
# Create the base directories
system("rm -rf $tmp_dir");
mkdir($tmp_dir, 0755);
chmod(0755, $tmp_dir);
mkdir($debian_dir, 0755);
chmod(0755, $debian_dir);
system("mkdir -p $usr_dir");
# Copy the directory to a temp directory
system("cp -r -p -L $source_dir $usr_dir/$mod");
system("echo deb >$usr_dir/$mod/install-type");
system("cd $usr_dir && chmod -R og-w .");
if ($< == 0) {
system("cd $usr_dir && chown -R root:bin .");
}
system("find $usr_dir -name .git | xargs rm -rf");
system("find $usr_dir -name RELEASE | xargs rm -rf");
system("find $usr_dir -name RELEASE.sh | xargs rm -rf");
if (-r "$usr_dir/$mod/EXCLUDE") {
system("cd $usr_dir/$mod && cat EXCLUDE | xargs rm -rf");
system("rm -f $usr_dir/$mod/EXCLUDE");
}
foreach my $e (@exclude) {
system("find $usr_dir -name ".quotemeta($e)." | xargs rm -rf");
}
my $out = `du -sk $tmp_dir`;
my $size = $out =~ /^\s*(\d+)/ ? $1 : undef;
# Set version in .info file to match command line, if given
if ($ver) {
if ($minfo{'desc'}) {
$minfo{'version'} = $ver;
&write_file("$usr_dir/$mod/module.info", \%minfo);
}
elsif ($tinfo{'desc'}) {
$tinfo{'version'} = $ver;
&write_file("$usr_dir/$mod/theme.info", \%tinfo);
}
}
else {
$ver ||= $iver; # Use module.info version, or 1
$ver ||= 1;
}
$ver .= "-".$release if ($release);
# Fix up Perl paths
system("(find $usr_dir -name '*.cgi' ; find $usr_dir -name '*.pl') | perl -ne 'chop; open(F,\$_); \@l=<F>; close(F); \$l[0] = \"#\!/usr/bin/perl\$1\n\" if (\$l[0] =~ /#\!\\S*perl\\S*(.*)/); open(F,\">\$_\"); print F \@l; close(F)'");
system("(find $usr_dir -name '*.cgi' ; find $usr_dir -name '*.pl') | xargs chmod +x");
# Build list of dependencies on other Debian packages, for inclusion as a
# Requires: header
my @rdeps = ( "base", "perl", $product );
if ($debdepends && exists($minfo{'depends'})) {
foreach my $d (split(/\s+/, $minfo{'depends'})) {
my ($dwebmin, $dmod, $dver);
if ($d =~ /^[0-9\.]+$/) {
# Depends on a version of Webmin
$dwebmin = $d;
}
elsif ($d =~ /^(\S+)\/([0-9\.]+)$/) {
# Depends on some version of a module
$dmod = $1;
$dver = $2;
}
else {
# Depends on any version of a module
$dmod = $d;
}
# If the module is part of Webmin, we don't need to depend on it
if ($dmod) {
my %dinfo;
read_file("$dmod/module.info", \%dinfo);
next if ($dinfo{'longdesc'});
}
push(@rdeps, $dwebmin ? ("$product (>= $dwebmin)") :
$dver ? ("$prefix$dmod (>= $dver)") :
($prefix.$dmod));
}
}
my $rdeps = join(", ", @rdeps);
# Create the control file
my $kbsize = int(($size-1) / 1024)+1;
open(my $CONTROL, ">", "$control_file");
print $CONTROL <<EOF;
Package: $prefix$mod
Version: $ver
Section: admin
Priority: optional
Architecture: all
Essential: no
Depends: $rdeps
Pre-Depends: bash, perl
Installed-Size: $kbsize
Maintainer: $email
Provides: $prefix$mod
Description: $desc
EOF
close($CONTROL);
# Create the copyright file
my $nowstr = strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time()));
open(my $COPY, ">", "$copyright_file");
print $COPY "This package was debianized by $email on\n";
print $COPY "$nowstr.\n";
print $COPY "\n";
if ($url) {
print $COPY "It was downloaded from: $url\n";
print $COPY "\n";
}
print $COPY "Upstream author: $upstream\n";
print $COPY "\n";
print $COPY "Copyright: $licence\n";
close($COPY);
# Read the module's CHANGELOG file
my $changes = { };
my $f = "$usr_dir/$mod/CHANGELOG";
if (-r $f) {
# Read its change log file
my $inversion;
open(my $LOG, $f);
while(<$LOG>) {
s/\r|\n//g;
if (/^----\s+Changes\s+since\s+(\S+)\s+----/) {
$inversion = $1;
}
elsif ($inversion && /\S/) {
push(@{$changes->{$inversion}}, $_);
}
}
close($LOG);
}
# Create the changelog file from actual changes
if (%$changes) {
open(my $CHANGELOG, ">", "$changelog_file");
my $forv;
my $clver = $ver;
$clver =~ s/\.[^0-9\.]*$//;
foreach my $v (sort { $a <=> $b } (keys %$changes)) {
if ($clver > $v && sprintf("%.2f0", $clver) == $v) {
$forv = $clver;
}
else {
$forv = sprintf("%.2f0", $v+0.01);
}
print $CHANGELOG "$prefix$mod ($forv) stable; urgency=low\n";
print $CHANGELOG "\n";
foreach my $c (@{$changes->{$v}}) {
my @lines = wrap_lines($c, 65);
print $CHANGELOG " * $lines[0]\n";
foreach my $l (@lines[1 .. $#lines]) {
print $CHANGELOG " $l\n";
}
}
print $CHANGELOG "\n";
print $CHANGELOG "-- $email\n";
print $CHANGELOG "\n";
}
close($CHANGELOG);
}
$depends //= "";
$force_theme //= "";
$istheme //= "";
# Create the pre-install script, which checks if Webmin is installed
open(my $PREINSTALL, ">", "$preinstall_file");
no warnings "uninitialized";
print $PREINSTALL <<EOF;
#!/bin/sh
if [ "$debdepends" != 1 ]; then
if [ ! -r /etc/$product/config -o ! -d /usr/share/$product ]; then
echo "$ucproduct does not appear to be installed on your system."
echo "This package cannot be installed unless the Debian version of $ucproduct"
echo "is installed first."
exit 1
fi
fi
if [ "$depends" != "" -a "$debdepends" != 1 ]; then
# Check if depended webmin/usermin modules are installed
for d in $depends; do
if [ ! -r /usr/share/$product/\$d/module.info ]; then
echo "This $ucproduct module depends on the module \$d, which is"
echo "not installed on your system."
exit 1
fi
done
fi
# Check if this module is already installed
if [ -d /usr/share/$product/$mod -a "\$1" != "upgrade" -a "$allow_overwrite" != "1" ]; then
echo "This $ucproduct module is already installed on your system."
exit 1
fi
EOF
close($PREINSTALL);
system("chmod 755 $preinstall_file");
# Create the post-install script
open(my $POSTINSTALL, ">", "$postinstall_file");
print $POSTINSTALL <<EOF;
#!/bin/sh
if [ "$post_config" = "1" ]; then
# Copy config file to /etc/webmin or /etc/usermin
os_type=`grep "^os_type=" /etc/$product/config | sed -e 's/os_type=//g'`
os_version=`grep "^os_version=" /etc/$product/config | sed -e 's/os_version=//g'`
/usr/bin/perl /usr/share/$product/copyconfig.pl \$os_type \$os_version /usr/share/$product /etc/$product $mod
# Update the ACL for the root user, or the first user in the ACL
grep "^root:" /etc/$product/webmin.acl >/dev/null
if [ "\$?" = "0" ]; then
user=root
else
user=`head -1 /etc/$product/webmin.acl | cut -f 1 -d :`
fi
mods=`grep \$user: /etc/$product/webmin.acl | cut -f 2 -d :`
echo \$mods | grep " $mod" >/dev/null
if [ "\$?" != "0" ]; then
grep -v ^\$user: /etc/$product/webmin.acl > /tmp/webmin.acl.tmp
echo \$user: \$mods $mod > /etc/$product/webmin.acl
cat /tmp/webmin.acl.tmp >> /etc/$product/webmin.acl
rm -f /tmp/webmin.acl.tmp
fi
fi
if [ "$force_theme" != "" -a "$istheme" = "1" ]; then
# Activate this theme
grep -v "^preroot=" /etc/$product/miniserv.conf >/etc/$product/miniserv.conf.tmp
(cat /etc/$product/miniserv.conf.tmp ; echo preroot=$mod) > /etc/$product/miniserv.conf
rm -f /etc/$product/miniserv.conf.tmp
grep -v "^theme=" /etc/$product/config >/etc/$product/config.tmp
(cat /etc/$product/config.tmp ; echo theme=$mod) > /etc/$product/config
rm -f /etc/$product/config.tmp
(/etc/$product/stop && /etc/$product/start) >/dev/null 2>&1
fi
rm -f /etc/$product/module.infos.cache
rm -f /var/$product/module.infos.cache
# Run post-install function
if [ "$product" = "webmin" ]; then
cd /usr/share/$product
WEBMIN_CONFIG=/etc/$product WEBMIN_VAR=/var/$product /usr/share/$product/run-postinstalls.pl $mod
fi
# Run post-install shell script
if [ -r "/usr/share/$product/$mod/postinstall.sh" ]; then
cd /usr/share/$product
WEBMIN_CONFIG=/etc/$product WEBMIN_VAR=/var/$product /usr/share/$product/$mod/postinstall.sh
fi
EOF
close($POSTINSTALL);
system("chmod 755 $postinstall_file");
# Create the pre-uninstall script
open(my $PREUNINSTALL, ">", "$preuninstall_file");
print $PREUNINSTALL <<EOF;
#!/bin/sh
# De-activate this theme, if in use and if we are not upgrading
if [ "$istheme" = "1" -a "\$1" != "upgrade" ]; then
grep "^preroot=$mod" /etc/$product/miniserv.conf >/dev/null
if [ "\$?" = "0" ]; then
grep -v "^preroot=$mod" /etc/$product/miniserv.conf >/etc/$product/miniserv.conf.tmp
(cat /etc/$product/miniserv.conf.tmp) > /etc/$product/miniserv.conf
rm -f /etc/$product/miniserv.conf.tmp
grep -v "^theme=$mod" /etc/$product/config >/etc/$product/config.tmp
(cat /etc/$product/config.tmp) > /etc/$product/config
rm -f /etc/$product/config.tmp
(/etc/$product/stop && /etc/$product/start) >/dev/null 2>&1
fi
fi
# Run the pre-uninstall script, if we are not upgrading
if [ "$product" = "webmin" -a "\$1" = "0" -a -r "/usr/share/$product/$mod/uninstall.pl" ]; then
cd /usr/share/$product
WEBMIN_CONFIG=/etc/$product WEBMIN_VAR=/var/$product /usr/share/$product/run-uninstalls.pl $mod
fi
/bin/true
EOF
close($PREUNINSTALL);
system("chmod 755 $preuninstall_file");
# Run the actual build command
system("fakeroot dpkg --build $tmp_dir $target_dir/${prefix}${mod}_${ver}_all.deb") &&
die "dpkg failed";
print "Wrote $target_dir/${prefix}${mod}_${ver}_all.deb\n";
# Create the .dsc file, if requested
if ($dsc_file) {
# Create the .diff file, which just contains the debian directory
my $diff_file = $dsc_file;
$diff_file =~ s/[^\/]+$//; $diff_file .= "$prefix$mod-$ver.diff";
my $diff_orig_dir = "$tmp_dir/$prefix$mod-$ver-orig";
my $diff_new_dir = "$tmp_dir/$prefix$mod-$ver";
mkdir($diff_orig_dir, 0755);
mkdir($diff_new_dir, 0755);
system("cp -r $debian_dir $diff_new_dir");
system("cd $tmp_dir && diff -r -N -u $prefix$mod-$ver-orig $prefix$mod-$ver >$diff_file");
my $diffmd5 = `md5sum $diff_file`;
$diffmd5 =~ s/\s+.*\n//g;
my @diffst = stat($diff_file);
# Create a tar file of the module directory
my $tar_file = $dsc_file;
$tar_file =~ s/[^\/]+$//; $tar_file .= "$prefix$mod-$ver.tar.gz";
system("cd $par ; tar czf $tar_file $source_mod");
my $md5 = `md5sum $tar_file`;
$md5 =~ s/\s+.*\n//g;
my @st = stat($tar_file);
# Finally create the .dsc
open(my $DSC, ">", "$dsc_file");
print $DSC <<EOF;
Format: 1.0
Source: $prefix$mod
Version: $ver
Binary: $prefix$mod
Maintainer: $email
Architecture: all
Standards-Version: 3.6.1
Build-Depends-Indep: debhelper (>= 4.1.16), debconf (>= 0.5.00), perl
Uploaders: Jamie Cameron <jcameron\@webmin.com>
Files:
$md5 $st[7] ${prefix}${mod}-$ver.tar.gz
$diffmd5 $diffst[7] ${prefix}${mod}-${ver}.diff
EOF
close($DSC);
}
# Clean up
# XXX Dangerous! Fix me.
system("rm -rf $tmp_dir");
# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
open(my $ARFILE, "<", "$_[0]") || return 0;
while(<$ARFILE>) {
s/\r|\n//g;
if (!/^#/ && /^([^=]+)=(.*)$/) {
$_[1]->{$_[3] ? lc($1) : $1} = $2;
push(@{$_[2]}, $1) if ($_[2]);
}
}
close($ARFILE);
return 1;
}
# write_file(file, array)
# Write out the contents of an associative array as name=value lines
sub write_file
{
my (%old, @order);
&read_file($_[0], \%old, \@order);
open(ARFILE, ">$_[0]");
foreach my $k (@order) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (exists($_[1]->{$k}));
}
foreach my $k (keys %{$_[1]}) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
}
close(ARFILE);
}
# wrap_lines(text, width)
# Given a multi-line string, return an array of lines wrapped to
# the given width
sub wrap_lines
{
my @rv;
my $w = $_[1];
my $rest;
foreach my $rest (split(/\n/, $_[0])) {
if ($rest =~ /\S/) {
while($rest =~ /^(.{1,$w}\S*)\s*([\0-\377]*)$/) {
push(@rv, $1);
$rest = $2;
}
}
else {
# Empty line .. keep as it is
push(@rv, $rest);
}
}
return @rv;
}