-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch
executable file
·131 lines (113 loc) · 3.6 KB
/
batch
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env ruby
require "thor"
require 'fileutils'
require 'csv'
class SubCommandBase < Thor
def self.banner(command, namespace = nil, subcommand = false)
"#{basename} #{subcommand_prefix} #{command.usage}"
end
def self.subcommand_prefix
self.name.gsub(%r{.*::}, '').gsub(%r{^[A-Z]}) { |match| match[0].downcase }.gsub(%r{[A-Z]}) { |match| "-#{match[0].downcase}" }
end
end
module App
class Files < SubCommandBase
desc "list <path>", "list files in path. what formats can we accept?"
#options :path => :array
def list(path)
# list
puts "listing files in path: #{path}"
puts Dir[path].each { |file| puts file }
end
desc "sanitize", "sanitize filenames"
def sanitize(path)
# allowed_characters: a-zA-Z0-9_-.
# sanitize
puts "sanitizing filenames in path: #{path}"
puts Dir[path].each { |old_file|
puts "old_file #{old_file}"
if /^[a-zA-Z0-9_\-.\/]+$/.match(old_file)
puts "VALID FILENAMNE"
else
puts "INVALID FILESNAMEAS!"
new_file = old_file.gsub(/[^a-zA-Z0-9_\-.\/]+/, '_')
puts "new_file #{new_file}"
if File.file? new_file
puts "WARNING: file with new filename #{new_file} exists! overwriting it, possibly"
end
FileUtils.mv(old_file, new_file)
end
}
end
end
class Manifest < SubCommandBase
desc "list <path>", "list files in path"
def list(path)
# list
# list all CSV files?
puts "listing CSV files in path: #{path}"
end
desc "validate", "validate manifest format and filenames/existence"
def validate(csv_path)
# allowed_characters: a-zA-Z0-9_-.
# validate
end
desc "create", "create new manifest"
# TODO: figure out what fields we need for this
# metadata row
#
# sfx_batch01_100,[email protected],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#
#
options :sections => :numeric
def create()
section_size = options[:sections]
STDERR.puts "Section size #{section_size}"
title = "SFX with #{section_size} sections"
files_path = "wayzata_wav_all/*.wav"
batch_name = "sfx_batch_#{section_size}_sections"
email = "[email protected]"
#metadata_row = "batch_name,email"
# create
file_rows = []
file_columns = []
date_issued = "2021-03-07"
file_columns.push("Title")
file_rows.push( title )
file_columns.push("Date Issued")
file_rows.push( date_issued )
STDERR.puts Dir[files_path].each_with_index { |file,index|
label = File.basename( file, ".*" ).capitalize
#file_rows.push( [ title, date_issued, file ] )
file_rows.push( file )
file_rows.push( label )
STDERR.puts "file_rows #{file_rows}"
file_columns.push("File")
file_columns.push("Label")
STDERR.puts "file #{file}, index #{index}"
break if index >= section_size
}
csv_string = CSV.generate do |csv|
csv << [ batch_name, email ].fill(nil, 3...file_columns.count())
csv << file_columns
#csv << [ "Date Issued", "Label", "File" ]
csv << file_rows
#file_rows.each { |row|
# csv << row
#}
end
puts csv_string
csv_string.each_line do |line|
STDERR.puts "Number of commas on this line: #{line.count(',')}"
end
#puts csv;
end
end
class CLI < Thor
desc "files", "list and sanitize files"
subcommand "files", Files
desc "manifest", "list, create, and validate manifests"
subcommand "manifest", Manifest
end
end
App::CLI.start