This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpg-unpack.sh
109 lines (98 loc) · 3.53 KB
/
rpg-unpack.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
# The `rpg-unpack` program reads the gem file's internal tar-based structure
# and either untars into a new directory or writes the data segment's tar
# stream to stdout.
#
# Gem files are more or less normal tarballs that looks like this:
#
# $ tar tv < sinatra-0.9.6.gem
# -rw-r--r-- wheel/wheel 117190 1969-12-31 16:00:00 data.tar.gz
# -rw-r--r-- wheel/wheel 1225 1969-12-31 16:00:00 metadata.gz
#
# The `metadata.gz` file is a gzip compressed YAML gemspec. The
# `data.tar.gz` holds the unprefixed files.
#
# There's also an older gem format apparently, but I'm hoping to not have to
# deal with it.
set -e
. rpg-sh-setup
USAGE '${PROGNAME} [-p <path>|-P] <package> [<version>]
${PROGNAME} -c [-m] <package> [<version>]
Unpack a gem file to disk or as a tar stream on standard output.
Options
-c Write gem data tar stream to stdout. Do not create any files.
-f Unpack over an existing directory.
-m Change the behavior of the -c option. Write gem metadata
segment instead of the data segment.
-n Do nothing if unpack directory already exists.
-p <path> Unpack under <path> instead of the working directory.
-P Unpack under RPGPACKS instead of the working directory.
The <package> may be a package name or path to a gem file on disk. When a
package name is given, the <version> may also be specified.'
workdir=.
filter=untar
segment=data.tar.gz
noop=false
force=false
while getopts cfmnPp: opt
do
case $opt in
p) workdir="$OPTARG";;
P) workdir="$RPGPACKS";;
c) filter=cat;;
m) segment=metadata.gz;;
f) force=true;;
n) noop=true;;
?) helpthem
exit 2;;
esac
done
shift $(( $OPTIND - 1 ))
# Piping the gemspec through tar isn't going to help anyone. Fail fast.
if test $segment = "metadata.gz" -a $filter = "untar"
then warn "illegal argument: -m must be used with -c"
exit 2
fi
# Check whether a gem file or package name was given.
if expr "$1" : '.*\.gem' >/dev/null
then file="$1"
test -r "$file" || {
warn "gem file can not be read: $file"
exit 1
}
else file=$(rpg-fetch "$1" "${2:->0}")
fi
# Extract the package name and version from the gem file.
basename=$(basename "$file" .gem)
package=${basename%-*}
version=${basename##*-}
# This takes the gem's `data.tar` stream on stdin and untars it into a
# newly created directory after the gem name. When the `-c` option is not
# given, the gem tar stream is piped through here.
untar () {
if $force
then mkdir -p "$workdir/$package-$version"
else mkdir "$workdir/$package-$version"
fi
tar -xom -C "$workdir/$package-$version" -f - 2>/dev/null
}
# Bail out with just the unpack directory on stdout if the -n option was
# given.
$noop && test "$filter" = "untar" -a -d "$workdir/$package-$version" && {
notice "$workdir/$package-$version already exists. noop'ing."
echo "$workdir/$package-$version"
exit 0
}
# Pipe the gem directly into `tar` and extract only the file/segment we're
# interested in (the `-O` option causes the file to be written to stdout
# instead of to disk). Next, pipe that thing through gzip to decompress and
# finally into whatever filter was configured (`cat` with the `-c` option or
# our `untar` function above otherwise).
tar -xOmf - $segment < "$file" 2>/dev/null |
gzip -dc |
$filter
# Write the path to the unpacked package directory on standard output.
if test "$filter" = "untar"
then echo "$workdir/$package-$version"
fi
true