| 1 | package OPM::Maker::Command::build; | ||||||||||
| 2 | 22 | $OPM::Maker::Command::build::VERSION = '1.17'; | |||||||||
| 3 | 22 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: Build OPM packages | ||||||||||
| 7 | |||||||||||
| 8 | 22 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 22 | ▶ | use MIME::Base64 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 22 | ▶ | use Sys::Hostname; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 22 | ▶ | use Path::Class (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 22 | ▶ | use XML::LibXML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 22 | ▶ | use OPM::Maker -command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 22 | ▶ | use OPM::Maker::Utils qw(reformat_size check_args_sopm); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | sub abstract { | ||||||||
|
Subroutine
abstract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | 1 | return "build package files for Znuny, OTOBO or ((OTRS)) Community Edition"; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | sub usage_desc { | ||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | 1 | return "opmbuild build [--version <version>] [--basedir <output_path>] [--output <output_path>] <path_to_sopm>"; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | sub opt_spec { | ||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | return ( | ||||||||||
| 27 | 1 | [ "output=s", "Output path for OPM file" ], | |||||||||
| 28 | [ "basedir=s", "Base directory of SOPM files" ], | ||||||||||
| 29 | [ "version=s", "Version to be used (override the one from the sopm file)" ], | ||||||||||
| 30 | ); | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 16 | ▶ | sub validate_args { | ||||||||
|
Subroutine
validate_args: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 16 | my ($self, $opt, $args) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 16 | my $sopm = check_args_sopm( $args ); | |||||||||
| 37 | |||||||||||
| 38 | 16 | ▶ | $self->usage_error( 'need path to .sopm' ) if | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | !$sopm; | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 12 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||
| 43 | 12 | my ($self, $opt, $args) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 12 | my $file = check_args_sopm( $args ); | |||||||||
| 46 | |||||||||||
| 47 | 12 | my $hostname = hostname; | |||||||||
| 48 | 12 | my @time = localtime; | |||||||||
| 49 | 12 | my $timestamp = sprintf "%04d-%02d-%02d %02d:%02d:%02d", | |||||||||
| 50 | $time[5]+1900, $time[4]+1, $time[3], | ||||||||||
| 51 | $time[2], $time[1], $time[0]; | ||||||||||
| 52 | |||||||||||
| 53 | 12 | my %opts; | |||||||||
| 54 | 12 | ▶ | if ( !$ENV{OPM_UNSECURE} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 12 | %opts = ( | |||||||||
| 56 | no_network => 1, | ||||||||||
| 57 | expand_entities => 0, | ||||||||||
| 58 | ); | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 12 | my $size = -s $file; | |||||||||
| 62 | |||||||||||
| 63 | # if file is big, but not "too big" | ||||||||||
| 64 | 12 | my $max_size = 31_457_280; | |||||||||
| 65 | 12 | ▶ | if ( $ENV{OPM_MAX_SIZE} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 7 | $max_size = reformat_size( $ENV{OPM_MAX_SIZE} ); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 12 | ▶ | if ( $size > $max_size ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | croak "$file too big (max size: $max_size bytes)"; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 11 | ▶ | if ( $size > 10_000_000 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | $opts{huge} = 1; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 11 | my $parser = XML::LibXML->new( %opts ); | |||||||||
| 78 | 11 | my $tree = $parser->parse_file( $file ); | |||||||||
| 79 | |||||||||||
| 80 | 11 | my $sopm_path = Path::Class::File->new( $file ); | |||||||||
| 81 | 11 | my $path = $sopm_path->dir; | |||||||||
| 82 | |||||||||||
| 83 | 11 | my $root_elem = $tree->getDocumentElement; | |||||||||
| 84 | |||||||||||
| 85 | # retrieve file information | ||||||||||
| 86 | 11 | my @files = $root_elem->findnodes( 'Filelist/File' ); | |||||||||
| 87 | |||||||||||
| 88 | FILE: | ||||||||||
| 89 | 11 | for my $file ( @files ) { | |||||||||
| 90 | 22 | my $name = $file->findvalue( '@Location' ); | |||||||||
| 91 | my $file_path = Path::Class::File->new( | ||||||||||
| 92 | 22 | ▶ | $opt->{basedir} ? $opt->{basedir} : $path, $name ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 22 | my $file_content = $file_path->slurp; | |||||||||
| 94 | 22 | my $base64 = MIME::Base64::encode( $file_content ); | |||||||||
| 95 | |||||||||||
| 96 | 22 | $file->setAttribute( 'Encode', 'Base64' ); | |||||||||
| 97 | 22 | $file->appendText( $base64 ); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 11 | my $build_date = XML::LibXML::Element->new( 'BuildDate' ); | |||||||||
| 101 | 11 | $build_date->appendText( $timestamp ); | |||||||||
| 102 | |||||||||||
| 103 | 11 | my $build_host = XML::LibXML::Element->new( 'BuildHost' ); | |||||||||
| 104 | 11 | $build_host->appendText( $hostname ); | |||||||||
| 105 | |||||||||||
| 106 | 11 | $root_elem->addChild( $build_date ); | |||||||||
| 107 | 11 | $root_elem->addChild( $build_host ); | |||||||||
| 108 | |||||||||||
| 109 | 11 | my $version = $root_elem->find( 'Version' )->[0]; | |||||||||
| 110 | 11 | ▶ | if ( $opt->{version} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | $version->removeChildNodes(); | |||||||||
| 112 | 1 | $version->appendText( $opt->{version} ); | |||||||||
| 113 | } | ||||||||||
| 114 | 11 | my $package_name = $root_elem->findvalue( 'Name' ); | |||||||||
| 115 | 11 | my $file_name = sprintf "%s-%s.opm", $package_name, $version->textContent; | |||||||||
| 116 | |||||||||||
| 117 | 11 | my $output_path = $opt->{output}; | |||||||||
| 118 | 11 | ▶ | $output_path = $path if !$output_path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 11 | my $opm_path = Path::Class::File->new( $output_path, $file_name ); | |||||||||
| 121 | 11 | my $fh = $opm_path->openw; | |||||||||
| 122 | 11 | $fh->print( $tree->toString ); | |||||||||
| 123 | |||||||||||
| 124 | 11 | return $opm_path->stringify; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 22 | 1; | |||||||||
| 128 | |||||||||||
| 129 | __END__ | ||||||||||