randfiles 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2
+
3
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ ## Overview
2
+
3
+ This little utility can be used to choose a random selection of files from a
4
+ given directory and output the list to stdout. The invocation looks like this:
5
+
6
+ randfiles [options] [dir1, [dir2, [...]]]
7
+
8
+ If no directories are given, the current directory is used. The possible options are:
9
+
10
+ - `-s, --size-limit [SIZE]`: Set a size limit for the selected files. For
11
+ example, `randfiles --size-limit 400MB` will select up to 400MB of files.
12
+ - `-c --count [N]`: Set the maximum number of files. `randfiles --count 3`
13
+ would only choose three random files.
14
+
15
+ ## Examples
16
+
17
+ To copy a random selection of music to a flash drive, limited to 3GB, you can do
18
+ this:
19
+
20
+ randfiles --size-limit 3GB ~/music | xargs -i -d'\n' cp -v {} /mnt/usb
21
+
22
+ If you're unfamiliar with xargs:
23
+
24
+ - `-i` replaces all occurrences of `{}` with what's read as input
25
+ - `-d'\n'` ensures that the items are delimited by newlines (there may be
26
+ spaces in the file names)
27
+
28
+ ## Todo
29
+
30
+ - A `-0` option for easier piping
31
+ - Filtering files by regex or glob
data/bin/randfiles ADDED
@@ -0,0 +1,7 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $: << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'randfiles'
6
+
7
+ puts Randfiles.run(ARGV)
data/lib/randfiles.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'randfiles/file_list'
2
+ require 'randfiles/input'
3
+
4
+ module Randfiles
5
+ def self.run(argv)
6
+ input = Randfiles::Input.new(argv)
7
+ file_list = Randfiles::FileList.new(input.files, input.options)
8
+
9
+ file_list.select_files
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module Randfiles
2
+ class FileList
3
+ def initialize(files, options)
4
+ @files = files
5
+ @count = options.max_count || files.count
6
+ @size_limit = options.size_limit
7
+ end
8
+
9
+ def select_files
10
+ selected = []
11
+ total_size = 0
12
+
13
+ @count.times do
14
+ if @files.empty?
15
+ break
16
+ else
17
+ file = pop_random_file
18
+ size = File.size(file)
19
+
20
+ next if @size_limit and total_size + size > @size_limit
21
+
22
+ total_size += size
23
+ selected << file
24
+ end
25
+ end
26
+
27
+ selected
28
+ end
29
+
30
+ private
31
+
32
+ def pop_random_file
33
+ @files.delete_at(rand(@files.size))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,60 @@
1
+ require 'ostruct'
2
+ require 'optparse'
3
+ require 'find'
4
+ require 'randfiles/version'
5
+
6
+ module Randfiles
7
+ class Input
8
+ attr_reader :options
9
+
10
+ def initialize(args)
11
+ @options = OpenStruct.new
12
+ @dirs = []
13
+
14
+ parse_args(args)
15
+ end
16
+
17
+ def files
18
+ @dirs.map do |dir|
19
+ Find.find(dir).
20
+ reject { |f| File.directory? f }.
21
+ map { |f| f.sub './', '' }
22
+ end.flatten
23
+ end
24
+
25
+ private
26
+
27
+ def parse_args(args)
28
+ parser = OptionParser.new do |opts|
29
+ opts.banner = "Usage: randfiles [options] [dir1, [dir2, [...]]]"
30
+
31
+ opts.on '-s', '--size-limit [SIZE]', 'Size limit for random file list. E.g. "400MB", "3GB"' do |size|
32
+ @options.size_limit = parse_size(size)
33
+ end
34
+ opts.on '-c', '--count [N]', Integer, 'Maximum number of files to list' do |count|
35
+ @options.max_count = count
36
+ end
37
+
38
+ opts.on_tail('-h', '--help', 'Show this message') { puts opts ; exit }
39
+ opts.on_tail('--version', 'Show version') { puts Randfiles::VERSION ; exit }
40
+ end
41
+
42
+ @dirs = parser.permute(args)
43
+ @dirs = ['.'] if @dirs.empty?
44
+ end
45
+
46
+ def parse_size(size)
47
+ return size.to_i if size =~ /^\d+$/
48
+
49
+ size, modifier = /^(\d+)(.*)$/.match(size) { [$1.to_i, $2] }
50
+
51
+ case modifier.upcase
52
+ when 'B' then size
53
+ when 'KB' then size * 1024
54
+ when 'MB' then size * 1024 * 1024
55
+ when 'GB' then size * 1024 * 1024 * 1024
56
+ else raise "Can't parse size: #{size}"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Randfiles
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: randfiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Radev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &12105040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *12105040
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &12104620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *12104620
36
+ description: ! " Given a set of random directories, this scripts outputs the files
37
+ in them\n in a random order. Combined with limitations by size or by number,
38
+ this can\n be used to pick a random collection of files from the given directories.\n"
39
+ email:
40
+ - andrey.radev@gmail.com
41
+ executables:
42
+ - randfiles
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/randfiles/file_list.rb
47
+ - lib/randfiles/input.rb
48
+ - lib/randfiles/version.rb
49
+ - lib/randfiles.rb
50
+ - bin/randfiles
51
+ - LICENSE
52
+ - README.md
53
+ homepage: http://github.com/AndrewRadev/randfiles
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 1.3.6
71
+ requirements: []
72
+ rubyforge_project: randfiles
73
+ rubygems_version: 1.8.15
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Select random files
77
+ test_files: []