github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/debian/get-orig-source.sh (about)

     1  #!/bin/bash
     2  : <<=cut
     3  
     4  =head1 DESCRIPTION
     5  
     6  This script is called by uscan(1) as per "debian/watch" to download Multi
     7  Upstream Tarball (MUT) components.
     8  
     9  =head1 COPYRIGHT
    10  
    11  Copyright: 2018-2019 Dmitry Smirnov <onlyjob@member.fsf.org>
    12  
    13  =head1 LICENSE
    14  
    15  License: GPL-3+
    16   This program is free software: you can redistribute it and/or modify
    17   it under the terms of the GNU General Public License as published by
    18   the Free Software Foundation, either version 3 of the License, or
    19   (at your option) any later version.
    20   .
    21   This package is distributed in the hope that it will be useful,
    22   but WITHOUT ANY WARRANTY; without even the implied warranty of
    23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    24   GNU General Public License for more details.
    25   .
    26   You should have received a copy of the GNU General Public License
    27   along with this program. If not, see <http://www.gnu.org/licenses/>.
    28  
    29  =cut
    30  
    31  set -e
    32  set -u
    33  
    34  if [ "$1" = '--upstream-version' ]; then
    35      VERSION="$2"
    36  else
    37      printf "E: missing argument '--upstream-version'.\n" 1>&2
    38      exit 1
    39  fi
    40  
    41  # components, with optional '=VERSION'
    42  COMPONENTS=(
    43  "docker/cli=v$( debian/helpers/real-upstream-version.sh "$VERSION" )"
    44  "docker/libnetwork"
    45  "docker/swarmkit"
    46  )
    47  
    48  DEB_SOURCE="$( dpkg-parsechangelog -SSource )"
    49  #DEB_VERSION="$( dpkg-parsechangelog -SVersion )"
    50  filename="$( readlink -f ../${DEB_SOURCE}_${VERSION}.orig.tar.xz )"
    51  [ -s "${filename}" ] || exit 1
    52  
    53  ## _log() tries to mimic the style of uscan logs.
    54  ## However it does NOT separate between stdout (info)
    55  ## and stderr (warn, error), because uscan makes a mess
    56  ## of that (it seems like it displays the two streams
    57  ## one after another).
    58  _log() {
    59      local prefix=$1; shift
    60      local first_line=1
    61  
    62      while [ $# -gt 0 ]; do
    63          if [ $first_line -eq 1 ]; then
    64              printf "get-orig-source ${prefix}: "
    65              first_line=0
    66          else
    67              printf "    "
    68          fi
    69          
    70          printf "%s\n" "$1"
    71          shift
    72      done 1>&2
    73  }
    74  
    75  info()  { _log 'info'  "$@"; }
    76  warn()  { _log 'warn'  "$@"; }
    77  error() { _log 'error' "$@"; }
    78  
    79  drop_files_excluded() {
    80      local work_dir
    81      local files_excluded
    82      local file
    83  
    84      # remove files excluded:
    85      for work_dir in "$@"; do
    86          files_excluded=$( perl -0nE 'say $1 if m{^Files\-Excluded:\s*(.*?)(?:\n\n|^Files|^Comment)}sm;' debian/copyright )
    87          pushd "${work_dir}" >/dev/null
    88          for file in ${files_excluded}; do
    89              if [ -e "${file}" ]; then
    90                  rm -fr "${file}"
    91              else
    92                  warn "No files matched excluded pattern as the last matching glob: ${file}"
    93              fi
    94          done
    95          popd >/dev/null
    96      done
    97  
    98      # remove empty directories:
    99      if [ -d "${work_dir}"/vendor ]; then
   100          find "${work_dir}"/vendor -mindepth 1 -type d -empty -delete
   101      fi
   102  }
   103  
   104  ## tarpack() takes two arguments:
   105  ##  1. directory to compress
   106  ##  2. tarball path/name
   107  tarpack() {
   108      ( cd "$1" && \
   109        find -L . -xdev -type f -print | LC_ALL=C sort \
   110        | XZ_OPT="-6v" tar -caf "$2" -T- --owner=root --group=root --mode=a+rX \
   111      )
   112  }
   113  
   114  echo 1>&2 "========================================"
   115  echo 1>&2 "$(basename $0) $@"
   116  echo 1>&2 "========================================"
   117  
   118  ## prepare a workdir:
   119  work_dir="$( mktemp -d -t get-orig-source_${DEB_SOURCE}_XXXXXXXX )"
   120  trap "rm -rf '${work_dir}'" EXIT
   121  
   122  ## extract main tarball::
   123  info "Unpack main tarball"
   124  tar -xf "${filename}" -C "${work_dir}"
   125  
   126  ## make sure there's one and only one top directory:
   127  topdir=$( ls -1 "${work_dir}" )
   128  if [ $(echo "$topdir" | wc -l) -ne 1 ]; then
   129      error "Unexpected content in orig tarball"
   130      exit 1
   131  fi
   132  
   133  ## move sources in a subdirectory:
   134  mv "${work_dir}/${topdir}" "${work_dir}"/engine
   135  mkdir "${work_dir}/${topdir}"
   136  mv "${work_dir}"/engine "${work_dir}/${topdir}"
   137  
   138  ## drop excluded files:
   139  # not needed, already done by uscan/mk-origtargz
   140  # drop_files_excluded "${work_dir}"/${topdir}/engine
   141  
   142  ## repack:
   143  info "Repack main tarball"
   144  tarpack "${work_dir}" "${filename}"
   145  
   146  ## fetch Docker components:
   147  for I in "${COMPONENTS[@]}"; do
   148  
   149      C=$(   echo ${I} | awk -F= '{print $1}' )
   150      REV=$( echo ${I} | awk -F= '{print $2}' )
   151      URL="github.com/${C}"
   152  
   153      if [ -z "$REV" ]; then
   154  	# get revision from engine/vendor.conf
   155          REV=$( grep "${URL}" "${work_dir}"/*/engine/vendor.conf | head -1 | awk '{print $2}' )
   156      fi
   157      if [ -z "${REV}" ]; then
   158          error "Could not find commit for ${C}"
   159          exit 1
   160      fi
   161  
   162      COMP=${C##*/}
   163      FN="$( readlink -f ../${DEB_SOURCE}_${VERSION}.orig-${COMP}.tar.gz )"
   164  
   165      info "Process ${I}" "component = $COMP" "revision = $REV" "filename = $FN"
   166  
   167      if [ ! -s "${FN}" ]; then
   168          ## download tarball:
   169          archive_url="https://${URL}/archive/${REV}.tar.gz"
   170          info "Requesting URL:" "$archive_url"
   171          wget --quiet --tries=3 --timeout=40 --read-timeout=40 --continue \
   172              -O "${FN}" "$archive_url"
   173                      info "Successfully downloaded package: $(basename ${FN})"
   174  
   175          ## extract tarball:
   176          info "Unpack tarball:"
   177          component_dir="$( mktemp -d -t get-orig-source_XXXXXXXX )"
   178          mkdir "${component_dir}"/${COMP}
   179          tar -xf "${FN}" -C "${component_dir}"/${COMP} --strip-components=1
   180  
   181          ## drop excluded files:
   182          info "Drop excluded files:"
   183          drop_files_excluded "${component_dir}"/${COMP}
   184  
   185          ## repack:
   186          info "Repack tarball:"
   187          tarpack "${component_dir}" "${FN}"
   188          rm -rf "${component_dir}"
   189  
   190          ## make orig tarball:
   191          mkorigtargz_opts=(
   192              "--package" "${DEB_SOURCE}" "--version" "${VERSION}"
   193              "--rename" "--repack" "--compression" "xz" "--directory" ".."
   194              "--copyright-file" "debian/copyright" "--component" "${COMP}"
   195              "${FN}")
   196          info "Launch mk-origtargz with options:" "$(echo ${mkorigtargz_opts[@]})"
   197          mk-origtargz "${mkorigtargz_opts[@]}"
   198      fi
   199  done
   200  
   201  # vim: et sts=4 sw=4