zu 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/zu +118 -0
  2. metadata +63 -0
data/bin/zu ADDED
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env ruby
2
+ ## Like "uz", but more featureful.
3
+
4
+ # TODO:
5
+ # - Refactor
6
+ # - Make this n00bish code less ugly
7
+ # - Extract lib/zu.rb so it's usable inside Ruby
8
+ # - Add -h/--help
9
+ # - Add -D/--deps
10
+ # - Make -d delete all archives on the args lit (use an options gem)
11
+ # - Build if given -b ?
12
+ # - Add these formats:
13
+ # .jar (zip)
14
+ # .lzma (unlzma)
15
+ # .tlz (unlzma)
16
+ # .7z (7z)
17
+ # .apk (zip?)
18
+ # ...?
19
+
20
+ class Array
21
+ def polite?
22
+ dir_match = %r[[^/]+/]
23
+ first = self[0][dir_match]
24
+ self.size == 1 or (
25
+ first and size == select do |e| e[dir_match] == first end.size
26
+ )
27
+ end
28
+ end
29
+
30
+ def redwarn msg
31
+ warn "\e[31m#{msg}\e[0m"
32
+ end
33
+
34
+ def maybe_download str
35
+ return str unless str[%r(^(?:ht|f)tp.+/(.+))]
36
+ $delete_afterward = true # just treat the download as temporary.
37
+ if File.exists? $1
38
+ redwarn "#$1 already exists. Not re-downloading."
39
+ exit 3
40
+ end
41
+ unless system 'wget', str
42
+ redwarn "#$1 appears to be a downloadable file, but download failed."
43
+ warn 'Please verify that `wget` is installed and available in your PATH.'
44
+ exit 4
45
+ end
46
+ $1
47
+ end
48
+
49
+ def rpm2tgz rpm_name
50
+ tgzname = File.basename(full_path, '.rpm') + '.tgz'
51
+ system 'rpm2tgz', full_path
52
+ tgzname
53
+ end
54
+
55
+ $delete_afterward = false
56
+ def extract e
57
+ if '-d' == e
58
+ $delete_afterward = true
59
+ return
60
+ end
61
+ f = maybe_download e
62
+ return redwarn "'#{f}' doesn't seem to exist." unless File.exists? f
63
+ full_path = File.expand_path f
64
+ basename = File.basename f
65
+ ext = File.extname f
66
+ simplename = basename.chomp ext
67
+ if '.tar' == File.extname(simplename)
68
+ simpename = simplename.chomp '.tar'
69
+ ext = '.tar' + ext
70
+ end
71
+
72
+ run_and_split = proc do |cmd_and_args|
73
+ IO.popen(cmd_and_args + ' ' + full_path).read.split /\n/
74
+ end
75
+
76
+ files_list, extract_cmd = *(
77
+ case ext.downcase
78
+ when '.zip'; [ run_and_split['zipinfo -1'], %w(unzip) ]
79
+ when '.tar', '.gem'; [ run_and_split['tar tf'], %w(tar xvf) ]
80
+ when '.tar.gz', '.tgz'; [ run_and_split['tar tzf'], %w(tar xvzf) ]
81
+ when '.tar.bz2', '.tbz2'; [ run_and_split['tar tjf'], %w(tar xvjf) ]
82
+ when '.tar.xz', '.txz'; [ run_and_split['tar tJf'], %w(tar xvJf) ]
83
+ when '.deb', '.ar'; [ run_and_split['ar t'], %w(ar xv) ]
84
+ when '.rpm'; return extract rpm2tgz[full_path]
85
+ when '.gz', '.xz', '.bz2'
86
+ [
87
+ [ simplename ],
88
+ { '.gz' => %w(gunzip), '.xz' => %w(unxz), '.bz2' => %w(bunzip2) }[ext]
89
+ ]
90
+ else
91
+ return redwarn "'#{f}' - Unknown extension. Doing nothing with it."
92
+ end)
93
+
94
+ extract = proc do
95
+ extract_cmd.push full_path
96
+ if system *extract_cmd
97
+ if $delete_afterward
98
+ File.exist? full_path and File.unlink full_path
99
+ end
100
+ else
101
+ redwarn "`#{extract_cmd.join ' '}` failed (#{$?})."
102
+ end
103
+ end
104
+
105
+ if files_list.polite?
106
+ extract.call
107
+ else
108
+ puts "# Caught impoliteness. mkdir \e[34;1m#{simplename}\e[0m/, then."
109
+ Dir.mkdir simplename
110
+ Dir.chdir simplename do
111
+ extract.call
112
+ end
113
+ end
114
+ end
115
+
116
+ ARGV.each do |e|
117
+ extract e
118
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zu
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bill
9
+ - Ted
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-11 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: ! "zu\n==\n\nUnzipper (in the tradition of `uz`, but better). Works for
16
+ .tgz, .xz, .zip,\n.deb, .rpm — you name it. (Literally. If you find an archive that
17
+ it doesn't\nopen, let me know about it and I'll add that.)\n\nIf you have an archive
18
+ sitting there of format `xyz`, then `zu foo.xyz` should\ntake care of it.\n\nIt
19
+ will:\n\n- Know how to extract the archive (based on extension ┈ though a version
20
+ that\n detects based on `file` is something we're considering)\n- Guard against
21
+ impoliteness. That is, if the archive only has one file, it\n will be permitted
22
+ to extract into the current directory, otherwise it will\n first `mkdir foo; cd
23
+ foo` then extract there. (The directory name will be\n the archive file minus the
24
+ extension.)\n- Download the file first, using `wget`, if the arg starts with `http:`,\n
25
+ \ `https:`, or `ftp:`\n- Remove the archive file if you pass `-d`\n\nDependencies\n------------\n\n`zu`
26
+ doesn't strive to be dependency-free by any means.\n\nFor starters, it expects Ruby.\n\nThen
27
+ it simply delegates to `unzip`, `gunzip`, `tar`, etc.\n\nNot sure if I ever plan
28
+ on changing this. The main purpose is to optimize the\ncommand-line extraction of
29
+ archives on a configured box.\n\nInstallation\n------------\n\n1. Have Ruby 1.8
30
+ (with gems) or 1.9\n2. `gem install zu`\n\nFeedback\n--------\n\nTell us. (exad-zu@sharpsaw.worg)[mailto:exad-zu@sharpsaw.org]\n"
31
+ email: exad-zu@sharpsaw.org
32
+ executables:
33
+ - zu
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - bin/zu
38
+ homepage: https://github.com/exad/zu/
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.24
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: An omni-unzipper in the tradition of `uz`. Works with .zip, .tgz, .deb, etc.
62
+ test_files: []
63
+ has_rdoc: