From 0f78dd071235cbcbc9d2d27a74a76c673e06b4a8 Mon Sep 17 00:00:00 2001
From: Guillem Jover <guillem@debian.org>
Date: Fri, 20 Nov 2009 19:51:44 +0100
Subject: [PATCH 1/3] Refactor deb extractors into two new functions

---
 functions                       |   43 ++++++++++++++++++++++++++------------
 scripts/debian/potato           |    6 +----
 scripts/debian/sarge            |    6 +----
 scripts/debian/sarge.buildd     |    6 +----
 scripts/debian/sarge.fakechroot |    6 +----
 scripts/debian/sid              |    6 +----
 scripts/debian/woody            |    6 +----
 scripts/debian/woody.buildd     |    6 +----
 scripts/ubuntu/breezy           |    6 +----
 scripts/ubuntu/dapper           |    6 +----
 scripts/ubuntu/edgy             |    6 +----
 scripts/ubuntu/feisty           |    6 +----
 scripts/ubuntu/gutsy            |    6 +----
 scripts/ubuntu/hoary            |    6 +----
 scripts/ubuntu/hoary.buildd     |    6 +----
 scripts/ubuntu/warty            |    6 +----
 scripts/ubuntu/warty.buildd     |    6 +----
 17 files changed, 45 insertions(+), 94 deletions(-)

diff --git a/functions b/functions
index e832d70..66021e8 100644
--- a/functions
+++ b/functions
@@ -717,27 +717,42 @@ get_debs () {
 
 ################################################################ extraction
 
+extract_deb_field () {
+	local pkg="$1"
+	local field="$2"
+
+	ar -p "$pkg" control.tar.gz | zcat |
+	    tar -O -xf - control ./control 2>/dev/null |
+	    grep -i "^$field:" | sed -e 's/[^:]*: *//' | head -n 1
+}
+
+extract_deb_data () {
+	local pkg="$1"
+	local tarball=$(ar -t "$pkg" | grep "^data.tar.[bgx]z")
+
+	case "$tarball" in
+		data.tar.gz) cat_cmd=zcat ;;
+		data.tar.bz2) cat_cmd=bzcat ;;
+		data.tar.xz) cat_cmd=xzcat ;;
+		*) error 1 UNKNOWNDATACOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
+	esac
+
+	if type $cat_cmd >/dev/null 2>&1; then
+		ar -p "$pkg" data.tar.gz | $cat_cmd | tar -xf -
+	else
+		error 1 UNPACKCMDUNVL "The $cat_cmd is not available on the system"
+	fi
+}
+
 extract () { (
 	cd "$TARGET"
-	local p=0 tarball cat_cmd
+	local p=0 cat_cmd
 	for pkg in $(debfor "$@"); do
 		p="$(($p + 1))"
 		progress "$p" "$#" EXTRACTPKGS "Extracting packages"
 		packagename="$(echo "$pkg" | sed 's,^.*/,,;s,_.*$,,')"
 		info EXTRACTING "Extracting %s..." "$packagename"
-		tarball=$(ar -t "./$pkg" | grep "^data.tar.[bgx]z")
-		case "$tarball" in
-			data.tar.gz) cat_cmd=zcat ;;
-			data.tar.bz2) cat_cmd=bzcat ;;
-			data.tar.xz) cat_cmd=xzcat ;;
-			*) error 1 UNKNOWNDATACOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
-		esac
-
-		if type $cat_cmd >/dev/null 2>&1; then
-			ar -p "./$pkg" data.tar.gz | $cat_cmd | tar -xf -
-		else
-			error 1 UNPACKCMDUNVL "The $cat_cmd is not available on the system"
-		fi
+		extract_deb_data "./$pkg"
 	done
 ); }
 
diff --git a/scripts/debian/potato b/scripts/debian/potato
index 3204c7d..304cbe0 100644
--- a/scripts/debian/potato
+++ b/scripts/debian/potato
@@ -43,11 +43,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg=$1
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/sarge b/scripts/debian/sarge
index e49a490..252e180 100644
--- a/scripts/debian/sarge
+++ b/scripts/debian/sarge
@@ -111,11 +111,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/sarge.buildd b/scripts/debian/sarge.buildd
index 0b9ad9a..249a035 100644
--- a/scripts/debian/sarge.buildd
+++ b/scripts/debian/sarge.buildd
@@ -72,11 +72,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/sarge.fakechroot b/scripts/debian/sarge.fakechroot
index 36234fa..2fe4a38 100644
--- a/scripts/debian/sarge.fakechroot
+++ b/scripts/debian/sarge.fakechroot
@@ -77,11 +77,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/sid b/scripts/debian/sid
index 59e70fd..50a9f50 100644
--- a/scripts/debian/sid
+++ b/scripts/debian/sid
@@ -59,11 +59,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/woody b/scripts/debian/woody
index 66a1261..d174be8 100644
--- a/scripts/debian/woody
+++ b/scripts/debian/woody
@@ -97,11 +97,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/debian/woody.buildd b/scripts/debian/woody.buildd
index 8485135..06cb8e3 100644
--- a/scripts/debian/woody.buildd
+++ b/scripts/debian/woody.buildd
@@ -72,11 +72,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/breezy b/scripts/ubuntu/breezy
index f445598..2646ca2 100644
--- a/scripts/ubuntu/breezy
+++ b/scripts/ubuntu/breezy
@@ -49,11 +49,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/dapper b/scripts/ubuntu/dapper
index 7f88d55..e57680a 100644
--- a/scripts/ubuntu/dapper
+++ b/scripts/ubuntu/dapper
@@ -56,11 +56,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/edgy b/scripts/ubuntu/edgy
index 0dbc5dc..483e992 100644
--- a/scripts/ubuntu/edgy
+++ b/scripts/ubuntu/edgy
@@ -66,11 +66,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/feisty b/scripts/ubuntu/feisty
index 4036828..0b98170 100644
--- a/scripts/ubuntu/feisty
+++ b/scripts/ubuntu/feisty
@@ -66,11 +66,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/gutsy b/scripts/ubuntu/gutsy
index 37d4b53..ba2a3f8 100644
--- a/scripts/ubuntu/gutsy
+++ b/scripts/ubuntu/gutsy
@@ -66,11 +66,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/hoary b/scripts/ubuntu/hoary
index 7944999..e5fe9fc 100644
--- a/scripts/ubuntu/hoary
+++ b/scripts/ubuntu/hoary
@@ -70,11 +70,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/hoary.buildd b/scripts/ubuntu/hoary.buildd
index 2596086..afb0904 100644
--- a/scripts/ubuntu/hoary.buildd
+++ b/scripts/ubuntu/hoary.buildd
@@ -66,11 +66,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/warty b/scripts/ubuntu/warty
index 0e16a62..e21a6d8 100644
--- a/scripts/ubuntu/warty
+++ b/scripts/ubuntu/warty
@@ -58,11 +58,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
diff --git a/scripts/ubuntu/warty.buildd b/scripts/ubuntu/warty.buildd
index b980e45..165377e 100644
--- a/scripts/ubuntu/warty.buildd
+++ b/scripts/ubuntu/warty.buildd
@@ -66,11 +66,7 @@ first_stage_install () {
     x_feign_install () {
         local pkg="$1"
         local deb="$(debfor $pkg)"
-        local ver="$(
-            ar -p "$TARGET/$deb" control.tar.gz | zcat |
-                tar -O -xf - control ./control 2>/dev/null |
-                grep -i ^Version: | sed -e 's/[^:]*: *//' | head -n 1
-        )"
+        local ver="$(extract_deb_field "$TARGET/$deb" Version)"
 
         mkdir -p "$TARGET/var/lib/dpkg/info"
 
-- 
1.6.5.3

