Skip to content

Commit fea1f8e

Browse files
author
Jonas Pfenniger
committed
ruby is not a dependency anymore
1 parent 50df4dd commit fea1f8e

File tree

2 files changed

+86
-122
lines changed

2 files changed

+86
-122
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Dependencies
1717
------------
1818

1919
# Debian & Ubuntu
20-
$ apt-get install build-essential git-core ruby1.8 curl tar <FIXME>
20+
$ apt-get install build-essential git-core curl tar <FIXME???>
2121

2222

2323
Build
@@ -31,4 +31,4 @@ TODO
3131
----
3232

3333
* remove remaining libs
34-
* re-write fetchurl in shell instead of ruby to avoid dependency
34+

fetchurl

Lines changed: 84 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,102 @@
1-
#!/usr/bin/env ruby
1+
#!/bin/sh
22
#
33
# Small utility to fetch and unpack urls (with cache)
44
#
55
# Depends on : wget, tar
66
#
7-
require 'optparse'
8-
require 'uri'
9-
require 'tmpdir'
10-
require 'fileutils'
11-
require 'digest/sha1'
12-
13-
include FileUtils
14-
15-
options = {
16-
:unpack => true,
17-
:target_dir => Dir.pwd,
18-
:cache_dir => ENV["HOME"] && ENV["HOME"].any? && "~/.cache/fetchurl",
19-
:cache => true
7+
8+
set -e
9+
set +u
10+
11+
# ENV vars, inherited from external
12+
CACHE=${CACHE:-1}
13+
UNPACK=${UNPACK:-1}
14+
VERBOSE=${VERBOSE:-0}
15+
16+
TARGET_DIR=${TARGET_DIR:-`pwd`}
17+
if [ -n "$HOME" ]; then
18+
CACHE_DIR=${CACHE_DIR:-$HOME/.cache/fetchurl}
19+
else
20+
CACHE_DIR=${CACHE_DIR:-}
21+
fi
22+
TMP_DIR=${TMP_DIR:-/tmp}
23+
24+
URL=$1
25+
26+
set -u
27+
28+
stderr () {
29+
echo $@ 1>&2
2030
}
2131

22-
opts = OptionParser.new do |opts|
23-
opts.banner = "Usage: #{File.basename $0} [options]"
24-
25-
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
26-
options[:verbose] = v
27-
end
28-
opts.on("-u", "--[no-]unpack", "Decide to unpack the fetched source") do |u|
29-
options[:unpack] = u
30-
end
31-
opts.on("-n", "--[no-]cache", "Decide to use the cache or not") do |c|
32-
options[:cache] = c
33-
end
34-
opts.on("-t", "--target-dir PATH", "Where to unpack the file") do |path|
35-
options[:target_dir] = path
36-
end
37-
opts.on("-c", "--cache-dir PATH", "Where to cache the downloaded files") do |path|
38-
options[:cache_dir] = path
39-
end
40-
opts.on("--sha1 SHA1", "Verify download with checksum") do |sha1|
41-
options[:sha1sum] = sha1
42-
end
43-
end
44-
45-
if !options[:cache_dir] && options[:cache]
46-
$stderr.puts "ERROR: missing cache dir"
47-
$stderr.puts opts
48-
exit 1
49-
end
32+
sh () {
33+
echo $ $@
34+
if [ "$VERBOSE" -ne 0 ]; then
35+
$@
36+
else
37+
$@ >/dev/null 2>&1
38+
fi
39+
}
5040

51-
begin
52-
opts.parse!
53-
rescue OptionParser::ParseError => ex
54-
$stderr.puts ex
55-
$stderr.puts opts
56-
exit 1
57-
end
41+
usage() {
42+
echo "Usage: fetchurl url"
43+
echo "CACHE=${CACHE}"
44+
echo "UNPACK=${UNPACK}"
45+
echo "VERBOSE=${VERBOSE}"
5846

59-
if ARGV.size < 1
60-
$stderr.puts "ERROR: missing url argument"
61-
$stderr.puts opts
47+
echo "TARGET_DIR=${TARGET_DIR}"
48+
echo "CACHE_DIR=${CACHE_DIR}"
49+
echo "TMP_DIR=${TMP_DIR}"
50+
51+
echo "URL=${URL}"
6252
exit 1
63-
end
53+
}
6454

65-
if options[:verbose]
66-
def sh(*args)
67-
$stdout.puts "$ " + args.join(' ')
68-
system *args
69-
end
70-
else
71-
require 'open3'
72-
def sh(*args)
73-
$stdout.puts "$ " + args.join(' ')
74-
begin
75-
# temporarily redirect stdout
76-
orig_stdout = $stdout.dup
77-
$stdout.reopen '/dev/null', 'w'
78-
ret = system *args
79-
ensure
80-
$stdout.reopen orig_stdout
81-
end
82-
return ret
83-
end
84-
end
85-
86-
options[:url] = ARGV[0]
87-
88-
filename = File.basename(URI.parse(options[:url]).path)
89-
90-
tmp_file = Dir.tmpdir + '/' + filename
91-
cache_file = File.expand_path(options[:cache_dir] + '/' + filename)
92-
93-
mkdir_p File.expand_path options[:cache_dir]
55+
if [ -z "$URL" ]; then
56+
stderr "ERROR: missing url"
57+
usage
58+
fi
59+
60+
if [ -z "$CACHE_DIR" ] && [ "$CACHE" -ne 0 ]; then
61+
stderr "ERROR: missing cache dir"
62+
usage
63+
fi
64+
65+
filename=`basename "$URL" | sed 's/\\\?.*//'`
66+
tmp_file="$TMP_DIR/$filename"
67+
cache_file="$CACHE_DIR/$filename"
68+
69+
mkdir -p "$CACHE_DIR"
9470

9571
# Fetch
96-
if options[:cache] && !File.exists?(cache_file)
97-
rm_rf tmp_file
98-
sh 'curl', '-L', '-o', tmp_file, options[:url]
99-
if $? != 0
100-
$stderr.puts "ERROR while fetching the file"
101-
exit 1
102-
end
103-
mv tmp_file, cache_file
104-
end
105-
$stdout.puts "*** File downloaded to #{cache_file}"
106-
107-
# Checksum
108-
sha1 = Digest::SHA1.hexdigest File.read(cache_file)
109-
if options[:sha1sum]
110-
if sha1 != options[:sha1sum].downcase
111-
$stderr.puts "ERROR: checksum #{sha1} does not match to given parameter"
112-
exit 1
113-
end
114-
else
115-
$stdout.puts "*** Checksum is #{sha1}"
116-
end
72+
if [ "$CACHE" -eq 0 ] || [ ! -f "$cache_file" ]; then
73+
rm -rf "$tmp_file"
74+
sh curl -L -o "$tmp_file" "$URL"
75+
sh mv "$tmp_file" "$cache_file"
76+
fi
77+
78+
# TODO: checksums
11779

11880
# Unpack
119-
if options[:unpack]
120-
extname = /(?:\.tar)?\.[^.]+$|$/.match(filename)[0]
121-
basename = File.basename(filename, extname)
81+
if [ "$UNPACK" -ne 0 ]; then
82+
extname=`echo "$filename" | sed 's/.*\(\.tar\.gz\|\.tgz\|\.tar\.bz2\)$/\1/'`
83+
bname=`basename "$filename" "$extname"`
84+
target_dir=`readlink -f "$TARGET_DIR"`
85+
86+
mkdir -p "$target_dir"
87+
sh cd "$target_dir"
12288

123-
target_dir = File.expand_path(options[:target_dir])
124-
mkdir_p target_dir
125-
Dir.chdir(target_dir) do
126-
case extname
127-
when ".tar.gz", ".tgz":
128-
sh "tar xzvf #{cache_file}"
129-
when ".tar.bz2":
130-
sh "tar xjvf #{cache_file}"
131-
else
132-
$stderr.puts "ERROR: #{extname} not supported"
89+
case "$extname" in
90+
.tar.gz|.tgz)
91+
sh tar xzvf "$cache_file"
92+
;;
93+
.tar.bz2)
94+
sh tar xjvf "$cache_file"
95+
;;
96+
*)
97+
stderr $extname extension not supported
13398
exit 1
134-
end
135-
end
136-
$stdout.puts "*** File extracted to #{target_dir}"
137-
end
99+
;;
100+
esac
101+
fi
138102

0 commit comments

Comments
 (0)