Advertisement
rockdrilla

git-debexport

Nov 20th, 2018
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.33 KB | None | 0 0
  1. #!/bin/sh
  2. readonly D=/dev/null
  3. export TAR_OPTIONS='--blocking-factor=1 --format=gnu --sort=name --numeric-owner --owner=0 --group=0 --no-selinux --no-xattrs --exclude-vcs --sparse'
  4.  
  5. G=$(git rev-parse --show-toplevel)
  6. [ -n "$G" ] || exit 1
  7. G=$(readlink -e "$G")
  8. [ -n "$G" ] || exit 1
  9.  
  10. cd "$G/" || exit 1
  11. dpkg-parsechangelog >$D 2>$D || exit 1
  12.  
  13. if git config --get commit.gpgsign 2>$D | grep -Fq true ; then
  14.     NO_SIGN_OPT=''
  15.     _sign_file() { _sign_file_do "$@"; }
  16. else
  17.     NO_SIGN_OPT='--no-sign'
  18.     _sign_file() { :; }
  19.  
  20.     unset DEBEMAIL DEBFULLNAME DEB_SIGN_KEYID
  21. fi
  22.  
  23. a=source
  24. n=$(dpkg-parsechangelog -S Source)
  25. v=$(dpkg-parsechangelog -S Version)
  26.  
  27. ts=$(dpkg-parsechangelog -S Timestamp)
  28. export SOURCE_DATE_EPOCH=${ts}
  29. export TAR_OPTIONS="${TAR_OPTIONS} --mtime @${ts}"
  30.  
  31. ## match pattern "epoch:version-revision"
  32. mp0='([0-9]+):([0-9][0-9a-zA-Z.+~:-]*)-([0-9a-zA-Z+.~]+)'
  33. ## match pattern "epoch:debian_version"
  34. mp1='([0-9]+):([0-9][0-9a-zA-Z.+~:]*)'
  35. ## match pattern "version-revision"
  36. mp2='([0-9][0-9a-zA-Z.+~-]*)-([0-9a-zA-Z+.~]+)'
  37. ## match pattern "debian_version"
  38. mp3='([0-9][0-9a-zA-Z.+~]*)'
  39. ## replace pattern "epoch|version|revision or debian_version"
  40. rp0='\2\5|\3\7|\4\6\8\9'
  41.  
  42. pv_sed='/^('${mp0}'|'${mp1}'|'${mp2}'|'${mp3}')$/{s##'${rp0}'#;p;}'
  43.  
  44. pv=$(echo "$v" | sed -En "${pv_sed}")
  45. [ -n "${pv}" ] || exit 1
  46.  
  47. # v0=$(echo "${pv}" | cut -d '|' -f 1)
  48. v1=$(echo "${pv}" | cut -d '|' -f 2)
  49. v2=$(echo "${pv}" | cut -d '|' -f 3)
  50.  
  51. _sign_file_do() {
  52.     _sf_i=$(stat -c '%s' "$1")
  53.     [ -n "${_sf_i}" ] || return
  54.     _sf_t0=$(mktemp)
  55.     if [ "${_sf_i}" -gt 0 ]; then
  56.         cat < "$1" > "${_sf_t0}"
  57.  
  58.         _sf_i=$((_sf_i-1))
  59.         _sf_t1=$(od -N 1 -j ${_sf_i} -t u1 "$1" | head -n 1)
  60.         _sf_t1=$(echo "${_sf_t1}" | tr -s '[:space:]' ' ')
  61.         _sf_t1=$(echo "${_sf_t1}" | cut -d ' ' -f 2)
  62.         if [ "${_sf_t1}" -ne 10 ]; then
  63.             echo >> "${_sf_t0}"
  64.         fi
  65.         echo >> "${_sf_t0}"
  66.     fi
  67.  
  68.     _sf_t1=$(mktemp -u); rm -f "${_sf_t1}"
  69.     gpg \
  70.       --utf8-strings --textmode --armor --clearsign \
  71.       --output "${_sf_t1}" "${_sf_t0}"
  72.     _sf_i=$?
  73.     if [ "${_sf_i}" -eq 0 ] ; then
  74.         cat < "${_sf_t1}" > "$1"
  75.     fi
  76.     rm -f "${_sf_t0}" "${_sf_t1}"; unset _sf_t0 _sf_t1
  77.     return ${_sf_i}
  78. }
  79.  
  80. T=$(mktemp -d)
  81.  
  82. _cleanup() {
  83.     cd "${TMP}/"
  84.     rm -rf "$T/"
  85. }
  86.  
  87. W=$T/tmp/; mkdir -p "$W/"
  88. if [ -z "${v1}" ]; then
  89.     ## native Debian package
  90.     ## grab all the things
  91.     tar -cf - ./ | tar -C "$W" -xf -
  92. else
  93.     ## grab only debian related files
  94.     tar -cf - debian/ | tar -C "$W" -xf -
  95. fi
  96. cd "$W/"
  97.  
  98. chmod 755 debian/rules
  99.  
  100. ## prepare gpg
  101. t=$T/z; date > "$t"; _sign_file "$t"; rm "$t"; unset t
  102.  
  103. ## native Debian package
  104. if [ -z "${v1}" ]; then
  105.     if ! dpkg-buildpackage --build=source -z9 -d -nc ${NO_SIGN_OPT}; then
  106.         _cleanup
  107.         exit 1
  108.     fi
  109.  
  110.     cd "$T/"; rm -rf "$W/"
  111.     echo "index of $T/" 1>&2
  112.     ls -lhgG
  113.     exit
  114. fi
  115.  
  116. pnu=${n}_${v1}
  117. pnd=${pnu}-${v2}
  118. (
  119.     find -L "$G/../" -mindepth 1 -maxdepth 1 -name "${pnu}.orig.*" 2>$D | \
  120.     while read i; do
  121.         k=$(readlink -e "$i" 2>$D)
  122.         [ -n "$k" ] || continue
  123.         i=$(basename "$i")
  124.         cp "$k" "$T/$i"
  125.     done; unset i k
  126.  
  127.     if ! [ -f debian/control ]; then
  128.         if ! debian/rules debian/control 2>&1; then
  129.             return 1
  130.         fi
  131.     fi
  132.  
  133.     tar -cf - debian/ | xz -9 > "$T/${pnd}.debian.tar.xz"
  134.  
  135.     cat > "$W/dpkg-source-raw" <<-"EOF"
  136.         #!/usr/bin/perl
  137.         #
  138.         # dpkg-source-raw:
  139.         #   hackish script based on original dpkg-source
  140.         #
  141.         # Copyright © 1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
  142.         # Copyright © 1997 Klee Dienes <klee@debian.org>
  143.         # Copyright © 1999-2003 Wichert Akkerman <wakkerma@debian.org>
  144.         # Copyright © 1999 Ben Collins <bcollins@debian.org>
  145.         # Copyright © 2000-2003 Adam Heath <doogie@debian.org>
  146.         # Copyright © 2005 Brendan O'Dea <bod@debian.org>
  147.         # Copyright © 2006-2008 Frank Lichtenheld <djpig@debian.org>
  148.         # Copyright © 2006-2009,2012 Guillem Jover <guillem@debian.org>
  149.         # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
  150.         #
  151.         # This program is free software; you can redistribute it and/or modify
  152.         # it under the terms of the GNU General Public License as published by
  153.         # the Free Software Foundation; either version 2 of the License, or
  154.         # (at your option) any later version.
  155.         #
  156.         # This program is distributed in the hope that it will be useful,
  157.         # but WITHOUT ANY WARRANTY; without even the implied warranty of
  158.         # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  159.         # GNU General Public License for more details.
  160.         #
  161.         # You should have received a copy of the GNU General Public License
  162.         # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  163.  
  164.         use strict;
  165.         use warnings;
  166.  
  167.         use List::Util qw(any none);
  168.         use Cwd;
  169.         use File::Basename;
  170.         use File::Spec;
  171.  
  172.         use Dpkg ();
  173.         use Dpkg::Gettext;
  174.         use Dpkg::ErrorHandling;
  175.         use Dpkg::Arch qw(:operators);
  176.         use Dpkg::Deps;
  177.         use Dpkg::Compression;
  178.         use Dpkg::Conf;
  179.         use Dpkg::Control::Info;
  180.         use Dpkg::Control::Tests;
  181.         use Dpkg::Control::Fields;
  182.         use Dpkg::Substvars;
  183.         use Dpkg::Version;
  184.         use Dpkg::Vars;
  185.         use Dpkg::Changelog::Parse;
  186.         use Dpkg::Source::Package;
  187.         use Dpkg::Vendor;
  188.  
  189.         # textdomain('dpkg-dev');
  190.  
  191.         my $build_format;
  192.         my %options = ();
  193.  
  194.         my $substvars = Dpkg::Substvars->new();
  195.  
  196.         my @options;
  197.  
  198.         # --format options are not allowed, they would take precedence
  199.         # over real command line options, debian/source/format should be used
  200.         # instead
  201.         # --unapply-patches is only allowed in local-options as it's a matter
  202.         # of personal taste and the default should be to keep patches applied
  203.         my $forbidden_opts_re = {
  204.             'options' => qr/^--(?:format=|unapply-patches$|abort-on-upstream-changes$)/,
  205.             'local-options' => qr/^--format=/,
  206.         };
  207.  
  208.         foreach my $filename ('local-options', 'options') {
  209.             my $conf = Dpkg::Conf->new();
  210.             my $optfile = "debian/source/$filename";
  211.             next unless -f $optfile;
  212.             $conf->load($optfile);
  213.             $conf->filter(remove => sub { $_[0] =~ $forbidden_opts_re->{$filename} });
  214.             if (@$conf) {
  215.                 unshift @options, @$conf;
  216.             }
  217.         }
  218.  
  219.         $options{origtardir} = $ARGV[0];
  220.         # report_options(quiet_warnings => 1);
  221.         # $options{quiet} = 1;
  222.  
  223.         my %ch_options = (file => "debian/changelog");
  224.         my $changelog = changelog_parse(%ch_options);
  225.         my $control = Dpkg::Control::Info->new("debian/control");
  226.  
  227.         # <https://reproducible-builds.org/specs/source-date-epoch/>
  228.         $ENV{SOURCE_DATE_EPOCH} ||= $changelog->{timestamp} || time;
  229.  
  230.         my $srcpkg = Dpkg::Source::Package->new(options => \%options);
  231.         my $fields = $srcpkg->{fields};
  232.  
  233.         my @sourcearch;
  234.         my %archadded;
  235.         my @binarypackages;
  236.  
  237.         # Scan control info of source package
  238.         my $src_fields = $control->get_source();
  239.         error(g_("debian/control doesn't contain any information about the source package")) unless defined $src_fields;
  240.         my $src_sect = $src_fields->{'Section'} || 'unknown';
  241.         my $src_prio = $src_fields->{'Priority'} || 'unknown';
  242.  
  243.         foreach (keys %{$src_fields}) {
  244.             my $v = $src_fields->{$_};
  245.             if (m/^Source$/i) {
  246.                 set_source_package($v);
  247.                 $fields->{$_} = $v;
  248.             } elsif (m/^Uploaders$/i) {
  249.                 ($fields->{$_} = $v) =~ s/\s*[\r\n]\s*/ /g; # Merge in a single-line
  250.             } elsif (m/^Build-(?:Depends|Conflicts)(?:-Arch|-Indep)?$/i) {
  251.                 my $dep;
  252.                 my $type = field_get_dep_type($_);
  253.                 $dep = deps_parse($v, build_dep => 1, union => $type eq 'union');
  254.                 error(g_('error occurred while parsing %s'), $_) unless defined $dep;
  255.                 my $facts = Dpkg::Deps::KnownFacts->new();
  256.                 $dep->simplify_deps($facts);
  257.                 $dep->sort() if $type eq 'union';
  258.                 $fields->{$_} = $dep->output();
  259.             } else {
  260.                 field_transfer_single($src_fields, $fields);
  261.             }
  262.         }
  263.  
  264.         # Scan control info of binary packages
  265.         my @pkglist;
  266.         foreach my $pkg ($control->get_packages()) {
  267.             my $p = $pkg->{'Package'};
  268.             my $sect = $pkg->{'Section'} || $src_sect;
  269.             my $prio = $pkg->{'Priority'} || $src_prio;
  270.             my $type = $pkg->{'Package-Type'} ||
  271.                     $pkg->get_custom_field('Package-Type') || 'deb';
  272.             my $arch = $pkg->{'Architecture'};
  273.             my $profile = $pkg->{'Build-Profiles'};
  274.  
  275.             my $pkg_summary = sprintf('%s %s %s %s', $p, $type, $sect, $prio);
  276.  
  277.             $pkg_summary .= ' arch=' . join ',', split ' ', $arch;
  278.  
  279.             if (defined $profile) {
  280.                 # If the string does not contain brackets then it is using the
  281.                 # old syntax. Emit a fatal error.
  282.                 if ($profile !~ m/^\s*<.*>\s*$/) {
  283.                     error(g_('binary package stanza %s is using an obsolete ' .
  284.                              'Build-Profiles field syntax'), $p);
  285.                 }
  286.  
  287.                 # Instead of splitting twice and then joining twice, we just do
  288.                 # simple string replacements:
  289.  
  290.                 # Remove the enclosing <>
  291.                 $profile =~ s/^\s*<(.*)>\s*$/$1/;
  292.                 # Join lists with a plus (OR)
  293.                 $profile =~ s/>\s+</+/g;
  294.                 # Join their elements with a comma (AND)
  295.                 $profile =~ s/\s+/,/g;
  296.                 $pkg_summary .= " profile=$profile";
  297.             }
  298.  
  299.             if (defined $pkg->{'Essential'} and $pkg->{'Essential'} eq 'yes') {
  300.                 $pkg_summary .= ' essential=yes';
  301.             }
  302.  
  303.             push @pkglist, $pkg_summary;
  304.             push @binarypackages, $p;
  305.             foreach (keys %{$pkg}) {
  306.                 my $v = $pkg->{$_};
  307.                 if (m/^Architecture$/) {
  308.                     # Gather all binary architectures in one set. 'any' and 'all'
  309.                     # are special-cased as they need to be the only ones in the
  310.                     # current stanza if present.
  311.                     if (debarch_eq($v, 'any') || debarch_eq($v, 'all')) {
  312.                         push(@sourcearch, $v) unless $archadded{$v}++;
  313.                     } else {
  314.                         for my $a (split(/\s+/, $v)) {
  315.                             error(g_("'%s' is not a legal architecture string"), $a)
  316.                                 if debarch_is_illegal($a);
  317.                             error(g_('architecture %s only allowed on its ' .
  318.                                      "own (list for package %s is '%s')"),
  319.                                   $a, $p, $a)
  320.                                 if $a eq 'any' or $a eq 'all';
  321.                             push(@sourcearch, $a) unless $archadded{$a}++;
  322.                         }
  323.                     }
  324.                 } elsif (m/^(?:Homepage|Description)$/) {
  325.                     # Do not overwrite the same field from the source entry
  326.                 } else {
  327.                     field_transfer_single($pkg, $fields);
  328.                 }
  329.             }
  330.         }
  331.  
  332.         unless (scalar(@pkglist)) {
  333.             error(g_("debian/control doesn't list any binary package"));
  334.         }
  335.  
  336.         if (any { $_ eq 'any' } @sourcearch) {
  337.             # If we encounter one 'any' then the other arches become insignificant
  338.             # except for 'all' that must also be kept
  339.             if (any { $_ eq 'all' } @sourcearch) {
  340.                 @sourcearch = qw(any all);
  341.             } else {
  342.                 @sourcearch = qw(any);
  343.             }
  344.         } else {
  345.             # Minimize arch list, by removing arches already covered by wildcards
  346.             my @arch_wildcards = grep { debarch_is_wildcard($_) } @sourcearch;
  347.             my @mini_sourcearch = @arch_wildcards;
  348.             foreach my $arch (@sourcearch) {
  349.                 if (none { debarch_is($arch, $_) } @arch_wildcards) {
  350.                     push @mini_sourcearch, $arch;
  351.                 }
  352.             }
  353.             @sourcearch = @mini_sourcearch;
  354.         }
  355.         $fields->{'Architecture'} = join(' ', @sourcearch);
  356.         $fields->{'Package-List'} = "\n" . join("\n", sort @pkglist);
  357.  
  358.         # Scan fields of dpkg-parsechangelog
  359.         foreach (keys %{$changelog}) {
  360.             my $v = $changelog->{$_};
  361.  
  362.             if (m/^Source$/) {
  363.                 set_source_package($v);
  364.                 $fields->{$_} = $v;
  365.             } elsif (m/^Version$/) {
  366.                 my ($ok, $error) = version_check($v);
  367.                 error($error) unless $ok;
  368.                 $fields->{$_} = $v;
  369.             } elsif (m/^Binary-Only$/) {
  370.                 error(g_('building source for a binary-only release'))
  371.                     if $v eq 'yes';
  372.             } elsif (m/^Maintainer$/i) {
  373.                 # Do not replace the field coming from the source entry
  374.             } else {
  375.                 field_transfer_single($changelog, $fields);
  376.             }
  377.         }
  378.  
  379.         $fields->{'Binary'} = join(', ', @binarypackages);
  380.         # Avoid overly long line by splitting over multiple lines
  381.         if (length($fields->{'Binary'}) > 980) {
  382.             $fields->{'Binary'} =~ s/(.{0,980}), ?/$1,\n/g;
  383.         }
  384.  
  385.         # Select the format to use
  386.         if (not defined $build_format) {
  387.             if (-e "debian/source/format") {
  388.                 open(my $format_fh, '<', "debian/source/format")
  389.                     or syserr(g_('cannot read debian/source/format'));
  390.                 $build_format = <$format_fh>;
  391.                 chomp($build_format) if defined $build_format;
  392.                 error(g_('debian/source/format is empty'))
  393.                     unless defined $build_format and length $build_format;
  394.                 close($format_fh);
  395.             } else {
  396.                 $build_format = '1.0';
  397.             }
  398.         }
  399.  
  400.         $fields->{'Format'} = $build_format;
  401.         $srcpkg->upgrade_object_type(); # Fails if format is unsupported
  402.         # Parse command line options
  403.         $srcpkg->init_options();
  404.  
  405.         my $basenamerev = $srcpkg->get_basename(1);
  406.  
  407.             ## portions from /usr/share/perl5/Dpkg/Source/Package/V2.pm
  408.             ## Dpkg::Source::Package::V2::_generate_patch
  409.  
  410.             my ($tarfile);
  411.             my $comp_ext_regex = compression_get_file_extension_regex();
  412.             foreach my $file (sort $srcpkg->find_original_tarballs()) {
  413.                 if ($file =~ /\.orig\.tar\.$comp_ext_regex$/) {
  414.                     if (defined($tarfile)) {
  415.                         error(g_('several orig.tar files found (%s and %s) but only ' .
  416.                                  'one is allowed'), $tarfile, $file);
  417.                     }
  418.                     $srcpkg->add_file($file);
  419.                 } elsif ($file =~ /\.orig-([[:alnum:]-]+)\.tar\.$comp_ext_regex$/) {
  420.                     $srcpkg->add_file($file);
  421.                 }
  422.             }
  423.  
  424.             ## portions from /usr/share/perl5/Dpkg/Source/Package/V2.pm
  425.             ## Dpkg::Source::Package::V2::do_build
  426.  
  427.             $srcpkg->add_file("$ARGV[0]/$basenamerev.debian.tar.xz");
  428.  
  429.         # Write the .dsc
  430.         my $dscname = "$ARGV[0]/$basenamerev.dsc";
  431.         $srcpkg->write_dsc(filename => $dscname, substvars => $substvars);
  432.         exit(0);
  433.     EOF
  434.     chmod +x "$W/dpkg-source-raw"
  435.     perl "$W/dpkg-source-raw" "$T" || return
  436.     rm "$W/dpkg-source-raw"
  437.  
  438.     _sign_file "$T/${pnd}.dsc"
  439.  
  440.     dpkg-genbuildinfo --build=source || return
  441.     _sign_file "$T/${pnd}_${a}.buildinfo"
  442.  
  443.     dpkg-genchanges --build=source -q >"$T/${pnd}_${a}.changes" || return
  444.     _sign_file "$T/${pnd}_${a}.changes"
  445.  
  446.     cd "$T/"; rm -rf "$W/"
  447.     echo "index of $T/" 1>&2
  448.     ls -lhgG
  449.     exit
  450. )
  451. if [ $? -ne 0 ]; then
  452.     _cleanup
  453.     exit 1
  454. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement