| 1 | # Generate a distribution tarball for a package. | ||
| 2 | # | ||
| 3 | # This is the implementation of the docknot dist command, which determines and | ||
| 4 | # runs the commands necessary to build a distribution tarball for a given | ||
| 5 | # package. | ||
| 6 | # | ||
| 7 | # SPDX-License-Identifier: MIT | ||
| 8 | |||
| 9 | ############################################################################## | ||
| 10 | # Modules and declarations | ||
| 11 | ############################################################################## | ||
| 12 | |||
| 13 | package App::DocKnot::Dist v8.0.1; | ||
| 14 | |||
| 15 | 6 | ▶ | use 5.024; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 6 | ▶ | use autodie; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 6 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | 6 | ▶ | use App::DocKnot::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 6 | ▶ | use App::DocKnot::Util qw(latest_tarball print_checked); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 6 | ▶ | use Archive::Tar (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 6 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 6 | ▶ | use File::Find qw(find); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 6 | ▶ | use Git::Repository (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 0 | ▶ | use IO::Compress::Xz (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 26 | use IO::Uncompress::Gunzip (); | ||
| 27 | use IPC::Run qw(run); | ||
| 28 | use IPC::System::Simple qw(systemx); | ||
| 29 | use List::SomeUtils qw(lastval); | ||
| 30 | use List::Util qw(any first); | ||
| 31 | use Path::Tiny qw(path); | ||
| 32 | |||
| 33 | # Base commands to run for various types of distributions. Additional | ||
| 34 | # variations may be added depending on additional configuration parameters. | ||
| 35 | #<<< | ||
| 36 | our %COMMANDS = ( | ||
| 37 | 'Autoconf' => [ | ||
| 38 | ['./bootstrap'], | ||
| 39 | ['./configure', 'CC=clang'], | ||
| 40 | ['make', 'warnings'], | ||
| 41 | ['make', 'check'], | ||
| 42 | ['make', 'clean'], | ||
| 43 | ['./configure', 'CC=gcc'], | ||
| 44 | ['make', 'warnings'], | ||
| 45 | ['make', 'check'], | ||
| 46 | ['make', 'clean'], | ||
| 47 | ['make', 'check-cppcheck'], | ||
| 48 | ['make', 'distcheck'], | ||
| 49 | ], | ||
| 50 | 'ExtUtils::MakeMaker' => [ | ||
| 51 | ['perl', 'Makefile.PL'], | ||
| 52 | ['make', 'disttest'], | ||
| 53 | ['make', 'dist'], | ||
| 54 | ], | ||
| 55 | 'Module::Build' => [ | ||
| 56 | ['perl', 'Build.PL'], | ||
| 57 | ['./Build', 'disttest'], | ||
| 58 | ['./Build', 'dist'], | ||
| 59 | ], | ||
| 60 | 'make' => [ | ||
| 61 | ['make', 'dist'], | ||
| 62 | ], | ||
| 63 | ); | ||
| 64 | #>>> | ||
| 65 | |||
| 66 | # Regexes matching files or directories in the source tree to ignore when | ||
| 67 | # comparing it against the generated distribution (in other words, we don't | ||
| 68 | # care whether these files or any files in these directories are included in | ||
| 69 | # the distribution). These should match the full file path relative to the | ||
| 70 | # top directory. | ||
| 71 | # | ||
| 72 | # Include all of the build-generated files for docknot itself so that we can | ||
| 73 | # use the new version to release the new version. | ||
| 74 | ## no critic (RegularExpressions::ProhibitFixedStringMatches) | ||
| 75 | our @DIST_IGNORE = ( | ||
| 76 | qr{ \A [.]git \z }xms, | ||
| 77 | qr{ \A autom4te[.]cache \z }xms, | ||
| 78 | qr{ \A Build \z }xms, | ||
| 79 | qr{ \A MANIFEST[.]bak \z }xms, | ||
| 80 | qr{ \A MYMETA [.] (?:json (?:[.]lock)? | yml) \z }xms, | ||
| 81 | qr{ \A _build \z }xms, | ||
| 82 | qr{ \A blib \z }xms, | ||
| 83 | qr{ \A config[.]h[.]in~ \z }xms, | ||
| 84 | qr{ \A configure~ \z }xms, | ||
| 85 | qr{ \A cover_db \z }xms, | ||
| 86 | qr{ \A tests/config \z }xms, | ||
| 87 | qr{ [.]tar[.][gx]z \z }xms, | ||
| 88 | ); | ||
| 89 | ## use critic | ||
| 90 | |||
| 91 | ############################################################################## | ||
| 92 | # Helper methods | ||
| 93 | ############################################################################## | ||
| 94 | |||
| 95 | # Given the path to the source tree, generate a list of files that we expect | ||
| 96 | # to find in the distribution tarball. | ||
| 97 | # | ||
| 98 | # $path - The directory path | ||
| 99 | # | ||
| 100 | # Returns: A list of files (no directories) that the distribution tarball | ||
| 101 | # should contain. | ||
| 102 | sub _expected_dist_files { | ||
| 103 | my ($self, $path) = @_; | ||
| 104 | my @files; | ||
| 105 | |||
| 106 | # Supplemental ignore rules from the package configuration. | ||
| 107 | my @ignore; | ||
| 108 | if ($self->{config}{distribution}{ignore}) { | ||
| 109 | @ignore = map { qr{$_}xms } $self->{config}{distribution}{ignore}->@*; | ||
| 110 | } | ||
| 111 | |||
| 112 | # Find all files in the source directory, stripping its path from the file | ||
| 113 | # name and excluding (and pruning) anything matching @DIST_IGNORE or in | ||
| 114 | # the distribution/ignore key of the package configuration. | ||
| 115 | # | ||
| 116 | # This uses File::Find rather than Path::Iterator::Rule like other parts | ||
| 117 | # of DocKnot because the ignore patterns are based on the whole path | ||
| 118 | # relative to the top of the distribution, and that's more annoying to do | ||
| 119 | # with Path::Iterator::Rule. | ||
| 120 | my $wanted = sub { | ||
| 121 | my $name = $File::Find::name; | ||
| 122 | $name =~ s{ \A \Q$path\E / }{}xms; | ||
| 123 | return if !$name; | ||
| 124 | if (any { $name =~ $_ } @DIST_IGNORE, @ignore) { | ||
| 125 | $File::Find::prune = 1; | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | return if -d; | ||
| 129 | push(@files, $name); | ||
| 130 | }; | ||
| 131 | |||
| 132 | # Generate and return the list of files. | ||
| 133 | find($wanted, "$path"); | ||
| 134 | return @files; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Find the tarball compressed with gzip given a directory and a prefix. | ||
| 138 | # | ||
| 139 | # $path - The directory path | ||
| 140 | # $prefix - The tarball file prefix | ||
| 141 | # | ||
| 142 | # Returns: The path to the gzip tarball | ||
| 143 | # Throws: Text exception if no gzip tarball was found | ||
| 144 | sub _find_gzip_tarball { | ||
| 145 | my ($self, $path, $prefix) = @_; | ||
| 146 | my $files_ref = latest_tarball($path, $prefix)->{files}; | ||
| 147 | my $gzip_file = lastval { m{ [.]tar [.]gz \z }xms } $files_ref->@*; | ||
| 148 | if (!defined($gzip_file)) { | ||
| 149 | die "cannot find gzip tarball for $prefix in $path\n"; | ||
| 150 | } | ||
| 151 | return $path->child($gzip_file); | ||
| 152 | } | ||
| 153 | |||
| 154 | # Given a directory and a prefix for tarballs in that directory, ensure that | ||
| 155 | # all the desired compression formats exist. Currently this only handles | ||
| 156 | # generating the xz version of a gzip tarball. | ||
| 157 | # | ||
| 158 | # $path - The directory path | ||
| 159 | # $prefix - The tarball file prefix | ||
| 160 | # | ||
| 161 | # Throws: Text exception on failure to read or write compressed files. | ||
| 162 | sub _generate_compression_formats { | ||
| 163 | my ($self, $path, $prefix) = @_; | ||
| 164 | my $files_ref = latest_tarball($path, $prefix)->{files}; | ||
| 165 | if (!any { m{ [.]tar [.]xz \z }xms } $files_ref->@*) { | ||
| 166 | my $gzip_file = lastval { m{ [.]tar [.]gz \z }xms } $files_ref->@*; | ||
| 167 | my $gzip_path = $path->child($gzip_file); | ||
| 168 | my $xz_path = $path->child($gzip_path->basename('.gz') . '.xz'); | ||
| 169 | |||
| 170 | # Open the input and output files. | ||
| 171 | my $gzip_fh = IO::Uncompress::Gunzip->new("$gzip_path"); | ||
| 172 | my $xz_fh = IO::Compress::Xz->new("$xz_path"); | ||
| 173 | |||
| 174 | # Read from the gzip file and write to the xz-compressed file. | ||
| 175 | my $buffer; | ||
| 176 | while (my $bytes = read($gzip_fh, $buffer, 1024 * 1024)) { | ||
| 177 | syswrite($xz_fh, $buffer); | ||
| 178 | } | ||
| 179 | close($xz_fh); | ||
| 180 | close($gzip_fh); | ||
| 181 | } | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | |||
| 185 | # Given a source directory, a prefix for tarballs and related files (such as | ||
| 186 | # signatures), and a destination directory, move all matching files from the | ||
| 187 | # source directory to the destination directory. | ||
| 188 | # | ||
| 189 | # $source_path - The source directory path | ||
| 190 | # $prefix - The tarball file prefix | ||
| 191 | # $dest_path - The destination directory path | ||
| 192 | # | ||
| 193 | # Throws: Text exception if no files are found | ||
| 194 | # Text exception on failure to move a file | ||
| 195 | sub _move_tarballs { | ||
| 196 | my ($self, $source_path, $prefix, $dest_path) = @_; | ||
| 197 | my $files_ref = latest_tarball($source_path, $prefix)->{files}; | ||
| 198 | for my $file ($files_ref->@*) { | ||
| 199 | $source_path->child($file)->move($dest_path->child($file)); | ||
| 200 | } | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Given a command with arguments, replace a command of "perl" with the | ||
| 205 | # configured path to Perl, if any. Assumes that the perl configuration | ||
| 206 | # parameter is set in the object and should not be called if this is not true. | ||
| 207 | # | ||
| 208 | # $command_ref - Reference to an array representing a command with arguments | ||
| 209 | # | ||
| 210 | # Returns: Reference to an array representing a command with arguments, with | ||
| 211 | # the command replaced with the configured path to Perl if it was | ||
| 212 | # "perl" | ||
| 213 | sub _replace_perl_path { | ||
| 214 | my ($self, $command_ref) = @_; | ||
| 215 | if ($command_ref->[0] ne 'perl') { | ||
| 216 | return $command_ref; | ||
| 217 | } | ||
| 218 | my @command = $command_ref->@*; | ||
| 219 | $command[0] = $self->{perl}; | ||
| 220 | return [@command]; | ||
| 221 | } | ||
| 222 | |||
| 223 | # Given a directory and a prefix for tarballs in that directory, sign the | ||
| 224 | # tarballs in that directory. | ||
| 225 | # | ||
| 226 | # $path - The directory path | ||
| 227 | # $prefix - The tarball file prefix | ||
| 228 | # | ||
| 229 | # Throws: Text exception on failure to sign the file | ||
| 230 | sub _sign_tarballs { | ||
| 231 | my ($self, $path, $prefix) = @_; | ||
| 232 | my $files_ref = latest_tarball($path, $prefix)->{files}; | ||
| 233 | for my $file (grep { m{ [.]tar [.][xg]z }xms } $files_ref->@*) { | ||
| 234 | my $tarball_path = $path->child($file); | ||
| 235 | my $sig_path = $path->child($tarball_path->basename() . '.asc'); | ||
| 236 | if ($sig_path->exists()) { | ||
| 237 | $sig_path->remove(); | ||
| 238 | } | ||
| 239 | systemx( | ||
| 240 | $self->{gpg}, '--detach-sign', '--armor', '-u', | ||
| 241 | $self->{pgp_key}, $tarball_path, | ||
| 242 | ); | ||
| 243 | } | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | |||
| 247 | ############################################################################## | ||
| 248 | # Public interface | ||
| 249 | ############################################################################## | ||
| 250 | |||
| 251 | # Create a new App::DocKnot::Dist object, which will be used for subsequent | ||
| 252 | # calls. | ||
| 253 | # | ||
| 254 | # $args_ref - Anonymous hash of arguments with the following keys: | ||
| 255 | # distdir - Path to the directory for distribution tarball | ||
| 256 | # metadata - Path to the package metadata | ||
| 257 | # perl - Path to Perl to use (default: search the user's PATH) | ||
| 258 | # | ||
| 259 | # Returns: Newly created object | ||
| 260 | # Throws: Text exceptions on invalid metadata directory path | ||
| 261 | # Text exception on missing or invalid distdir argument | ||
| 262 | sub new { | ||
| 263 | my ($class, $args_ref) = @_; | ||
| 264 | |||
| 265 | # Create the config reader. | ||
| 266 | my %config_args; | ||
| 267 | if ($args_ref->{metadata}) { | ||
| 268 | $config_args{metadata} = $args_ref->{metadata}; | ||
| 269 | } | ||
| 270 | my $config_reader = App::DocKnot::Config->new(\%config_args); | ||
| 271 | |||
| 272 | # Load the global configuration. | ||
| 273 | my $global_config_ref = $config_reader->global_config(); | ||
| 274 | |||
| 275 | # Ensure we were given a valid distdir argument if it was not set in the | ||
| 276 | # global configuration. | ||
| 277 | my $distdir = $args_ref->{distdir} // $global_config_ref->{distdir}; | ||
| 278 | if (!defined($distdir)) { | ||
| 279 | croak('distdir path not given'); | ||
| 280 | } elsif (!-d $distdir) { | ||
| 281 | croak("distdir path $distdir does not exist or is not a directory"); | ||
| 282 | } | ||
| 283 | |||
| 284 | # Create and return the object. | ||
| 285 | my $self = { | ||
| 286 | config => $config_reader->config(), | ||
| 287 | distdir => path($distdir), | ||
| 288 | gpg => $args_ref->{gpg} // 'gpg', | ||
| 289 | perl => $args_ref->{perl}, | ||
| 290 | pgp_key => $args_ref->{pgp_key} // $global_config_ref->{pgp_key}, | ||
| 291 | }; | ||
| 292 | bless($self, $class); | ||
| 293 | return $self; | ||
| 294 | } | ||
| 295 | |||
| 296 | # Given a distribution tarball compressed with gzip, ensure that every file | ||
| 297 | # from the source directory that is expected to be there is in the | ||
| 298 | # distribution tarball. Assumes that it is run from the root of the source | ||
| 299 | # directory. | ||
| 300 | # | ||
| 301 | # $source - Path to the source directory | ||
| 302 | # $tarball - Path to a gzip-compressed distribution tarball | ||
| 303 | # | ||
| 304 | # Returns: A list of files missing from the distribution (so an empty list | ||
| 305 | # means all expected files were found) | ||
| 306 | sub check_dist { | ||
| 307 | my ($self, $source, $tarball) = @_; | ||
| 308 | my @expected = $self->_expected_dist_files(path(q{.})); | ||
| 309 | my %expected = map { ("$_", 1) } @expected; | ||
| 310 | my $archive = Archive::Tar->new($tarball); | ||
| 311 | for my $file ($archive->list_files()) { | ||
| 312 | $file =~ s{ \A [^/]* / }{}xms; | ||
| 313 | delete $expected{$file}; | ||
| 314 | } | ||
| 315 | my @missing = sort(keys(%expected)); | ||
| 316 | return @missing; | ||
| 317 | } | ||
| 318 | |||
| 319 | # Analyze a source directory and return the list of commands to run to | ||
| 320 | # generate a distribution tarball. | ||
| 321 | # | ||
| 322 | # Returns: List of commands, each of which is a list of strings representing | ||
| 323 | # a command and its arguments | ||
| 324 | sub commands { | ||
| 325 | my ($self) = @_; | ||
| 326 | my $type = $self->{config}{build}{type}; | ||
| 327 | my @commands = map { [@$_] } $COMMANDS{$type}->@*; | ||
| 328 | |||
| 329 | # Special-case: If a specific path to Perl was configured, use that path | ||
| 330 | # rather than searching for perl in the user's PATH. This is used | ||
| 331 | # primarily by the test suite, which wants to run a Module::Build Build.PL | ||
| 332 | # and thus has to use the same perl binary as the one running the tests. | ||
| 333 | if (defined($self->{perl})) { | ||
| 334 | @commands = map { $self->_replace_perl_path($_) } @commands; | ||
| 335 | } | ||
| 336 | |||
| 337 | # Special-case: Autoconf packages with C++ support should also attempt a | ||
| 338 | # build with a C++ compiler. | ||
| 339 | if ($type eq 'Autoconf' && $self->{config}{build}{cplusplus}) { | ||
| 340 | #<<< | ||
| 341 | my @extra = ( | ||
| 342 | ['./configure', 'CC=g++'], | ||
| 343 | ['make', 'check'], | ||
| 344 | ['make', 'clean'], | ||
| 345 | ); | ||
| 346 | #>>> | ||
| 347 | splice(@commands, 1, 0, @extra); | ||
| 348 | } | ||
| 349 | |||
| 350 | # Special-case: Autoconf packages with Valgrind support should also run | ||
| 351 | # make check-valgrind. | ||
| 352 | if ($type eq 'Autoconf' && $self->{config}{build}{valgrind}) { | ||
| 353 | splice(@commands, -3, 0, ['make', 'check-valgrind']); | ||
| 354 | } | ||
| 355 | |||
| 356 | return @commands; | ||
| 357 | } | ||
| 358 | |||
| 359 | # Generate a distribution tarball. This assumes it is run from the root | ||
| 360 | # directory of the package to release and that it is a Git repository. It | ||
| 361 | # exports the Git repository, runs the commands to generate the tarball, and | ||
| 362 | # then removes the working tree. | ||
| 363 | # | ||
| 364 | # Throws: Text exception if any of the commands fail | ||
| 365 | # Text exception if the distribution is missing files | ||
| 366 | sub make_distribution { | ||
| 367 | my ($self) = @_; | ||
| 368 | |||
| 369 | # Determine the source directory and the distribution directory name. | ||
| 370 | my $source = path(q{.})->realpath(); | ||
| 371 | my $prefix = $self->{config}{distribution}{tarname}; | ||
| 372 | |||
| 373 | # If the distribution directory name already exists, remove it. Automake | ||
| 374 | # may have made parts of it read-only, so be forceful in the removal. | ||
| 375 | # Note that this disables safe mode and therefore should not be called on | ||
| 376 | # attacker-controlled directories. | ||
| 377 | chdir($self->{distdir}); | ||
| 378 | my $workdir = path($prefix); | ||
| 379 | if ($workdir->is_dir()) { | ||
| 380 | $workdir->remove_tree({ safe => 0 }); | ||
| 381 | } | ||
| 382 | |||
| 383 | # Export the Git repository into a new directory. | ||
| 384 | my $repo = Git::Repository->new(work_tree => "$source"); | ||
| 385 | my @branches = $repo->run( | ||
| 386 | 'for-each-ref' => '--format=%(refname:short)', 'refs/heads/', | ||
| 387 | ); | ||
| 388 | my $head = first { $_ eq 'main' || $_ eq 'master' } @branches; | ||
| 389 | my $archive = $repo->command(archive => "--prefix=${prefix}/", $head); | ||
| 390 | run([qw(tar xf -)], '<', $archive->stdout) | ||
| 391 | or die "git archive | tar xf - failed with status $?\n"; | ||
| 392 | $archive->close(); | ||
| 393 | |||
| 394 | if ($archive->exit != 0) { | ||
| 395 | die 'git archive failed with status ' . $archive->exit . "\n"; | ||
| 396 | } | ||
| 397 | |||
| 398 | # Change to that directory and run the configured commands. | ||
| 399 | chdir($workdir); | ||
| 400 | for my $command_ref ($self->commands()) { | ||
| 401 | systemx($command_ref->@*); | ||
| 402 | } | ||
| 403 | |||
| 404 | # Generate additional compression formats if needed. | ||
| 405 | $self->_generate_compression_formats(path(q{.}), $prefix); | ||
| 406 | |||
| 407 | # Move the generated tarballs to the parent directory. | ||
| 408 | $self->_move_tarballs(path(q{.}), $prefix, $self->{distdir}); | ||
| 409 | |||
| 410 | # Remove the working tree. | ||
| 411 | chdir($self->{distdir}); | ||
| 412 | $workdir->remove_tree(); | ||
| 413 | |||
| 414 | # Check the distribution for any missing files. If there are any, report | ||
| 415 | # them and then fail with an error. | ||
| 416 | my $tarball = $self->_find_gzip_tarball($self->{distdir}, $prefix); | ||
| 417 | chdir($source); | ||
| 418 | my @missing = $self->check_dist($source, $tarball); | ||
| 419 | if (@missing) { | ||
| 420 | print_checked("Files found in local tree but not in distribution:\n"); | ||
| 421 | print_checked(q{ }, join(qq{\n }, @missing), "\n"); | ||
| 422 | my $count = scalar(@missing); | ||
| 423 | my $files = ($count == 1) ? '1 file' : "$count files"; | ||
| 424 | die "$files missing from distribution\n"; | ||
| 425 | } | ||
| 426 | |||
| 427 | # Sign the tarballs if configured to do so. | ||
| 428 | if (defined($self->{pgp_key})) { | ||
| 429 | $self->_sign_tarballs($self->{distdir}, $prefix); | ||
| 430 | } | ||
| 431 | |||
| 432 | return; | ||
| 433 | } | ||
| 434 | |||
| 435 | ############################################################################## | ||
| 436 | # Module return value and documentation | ||
| 437 | ############################################################################## | ||
| 438 | |||
| 439 | 1; | ||
| 440 | __END__ | ||