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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
# program : rsfiltool
# copyright : PRAGMA Publishing On Demand
# version : 1.01 - 2002
# author : Hans Hagen
#
# project : eXaMpLe
# concept : Hans Hagen
# info : j.hagen@xs4all.nl
# www : www.pragma-pod.com / www.pragma-ade.com
unless defined? ownpath
ownpath = $0.sub(/[\\\/]\w*?\.rb/i,'')
$: << ownpath
end
# --name=a,b,c.xml wordt names [a.xml, b.xml, c.xml]
# --path=x/y/z/a,b,c.xml wordt [x/y/z/a.xml, x/y/z/b.xml, x/y/z/c.xml]
# todo : split session stuff from xmpl/base into an xmpl/session module and "include xmpl/session" into base and here and ...
require 'fileutils'
# require 'ftools'
require 'xmpl/base'
require 'xmpl/switch'
require 'xmpl/request'
session = Example.new('rsfiltool', '1.01', 'PRAGMA POD')
filterprefix = 'rsfil-'
commandline = CommandLine.new
commandline.registerflag('submit')
commandline.registerflag('fetch')
commandline.registerflag('report')
#commandline.registerflag('split')
commandline.registerflag('stamp')
commandline.registerflag('silent')
commandline.registerflag('request')
commandline.registerflag('nobackup')
commandline.registervalue('filter')
commandline.registervalue('root')
commandline.registervalue('path')
commandline.registervalue('name')
commandline.expand
session.set('log.silent',true) if commandline.option('silent')
session.inherit(commandline)
session.identify
# session.exit unless session.loadenvironment
def prepare (session)
# Normally the system provides the file, but a user can provide the rest; in
# order to prevent problems with keying in names, we force lowercase names.
session.set('option.file',session.get('argument.first')) if session.get('option.file').empty?
root = session.get('option.root').downcase
path = session.get('option.path').downcase
name = session.get('option.name').downcase
file = session.get('option.file').downcase
session.error('provide file') if file.empty?
session.error('provide root') if root.empty?
filter = session.get('option.filter').downcase
trash = session.get('option.trash').downcase
trash = '' unless FileTest.directory?(trash)
if not filter.empty? then
begin
require filter
rescue Exception
begin
require filterprefix + filter
rescue Exception
session.error('invalid filter')
end
end
begin
if RSFIL::valid?(file) then
split = RSFIL::split(file,name)
path = if split[0].downcase then split[0] else '' end
file = if split[1].downcase then split[1] else '' end
name = if split[2].downcase then split[2] else '' end
session.report('split result',split.inspect)
session.error('unable to split off path') if path.empty?
session.error('unable to split off file') if file.empty?
session.error('unable to split off name') if name.empty?
session.set('option.path',path) if path
session.set('option.file',file) if file
session.set('option.name',name) if name
else
session.error('invalid filename', file)
unless trash.empty? then
File.copy(file,trash + '/' + file)
end
end
rescue
session.error('unable to split',file,'with filter',filter)
end
end
session.error('provide path') if path.empty?
session.error('invalid root') unless test(?d,root)
exit if session.error?
session.set('fb.filename',file)
path.gsub!(/\\/o, '/')
path.gsub!(/\s/o, '')
path = root + '/' + path
# multiple paths
if path =~ /^(.*)\/(.*?)$/o then
prepath = $1
postpath = $2
paths = postpath.split(/\,/)
paths.collect! do |p|
prepath + '/' + p
end
else
paths = Array.new
paths.push(path)
end
paths.collect! do |p|
p.gsub(/[^a-zA-Z0-9\s\-\_\/\.\:]/o, '-')
end
file.gsub!(/\\/o, '/')
file.gsub!(/[^a-zA-Z0-9\s\-\_\/\.\:]/o, '-')
# if session.get('option.split')
# if file =~ /(.*)\.(.*?)$/o
# path = path + '/' + $1
# else
# session.error('nothing to split in filename')
# end
# end
paths.each do |p|
begin
session.report('creating path', p)
File.makedirs(p)
rescue
session.error('unable to create path', p)
end
end
name.gsub!(/\s+/,'')
# can be a,b,c.exa.saved => a.exa.saved,b.exa.saved,c.exa.saved
if name =~ /(.*?)\.(.*)$/
name = $1
suffix = $2
names = name.split(/\,/)
names.collect! do |n|
n + '.' + suffix
end
name = names.join(',')
else
names = name.split(/\,/)
end
session.set('fb.path',path)
session.set('fb.paths',paths)
session.set('fb.name',name)
session.set('fb.names',names)
end
def thefullname(path,file,name='')
filename = file.gsub(/.*?\//, '')
if name.empty?
path + '/' + filename
else
unless name =~ /\..+$/o # unless name.match(/\..+$/o)
if filename =~ /(\..+)$/o # if file.match(/(\..+)$/o)
name = name + $1
end
end
path + '/' + name
end
end
def submitfile (session)
filename = session.get('fb.filename')
paths = session.get('fb.paths')
names = session.get('fb.names')
paths.each do |path|
session.report('submitting path',path)
names.each do |name|
session.report('submitting file',filename,'to',name)
submit(session,path,filename,name)
end
end
end
def submitlist (session)
requestname = session.get('fb.filename')
paths = session.get('fb.paths')
if test(?e,requestname)
session.report('loading request file', requestname)
if request = ExaRequest.new(requestname)
filelist = request.files
if filelist && (filelist.size > 0)
filelist.each do |filename|
paths.each do |path|
session.report('submitting file from list', filename)
submit(session,path,filename,request.naturalname(filename))
end
end
else
session.warning('no filelist in', requestname)
end
else
session.warning('unable to load', requestname)
end
else
session.warning('no file', requestname)
end
end
def submit (session, path, filename, newname)
fullname = thefullname(path,newname)
unless test(?e,filename)
session.warning('no file to submit', filename)
return
end
begin
File.copy(fullname,fullname+'.old') if ! session.get('nobackup') && test(?e,fullname)
if test(?e,filename)
File.copy(filename,fullname)
session.report('submit', filename, 'in', fullname)
if session.get('option.stamp')
f = open(fullname+'.tim','w')
f.puts(Time.now.gmtime.strftime("%a %b %d %H:%M:%S %Y"))
f.close
end
else
session.error('unable to locate', filename)
end
rescue
session.error('unable to move', filename, 'to', fullname)
end
end
def fetch (session)
filename = session.get('fb.filename')
paths = session.get('fb.paths')
name = session.get('fb.name')
begin
File.copy(filename,filename+'.old') if ! session.get('nobackup') && test(?e,filename)
paths.each do |path|
# fullname = thefullname(path,request.naturalname(filename))
# fullname = thefullname(path,filename)
fullname = thefullname(path,name)
if test(?e,fullname)
File.copy(fullname,filename)
session.report('fetch', filename, 'from', fullname)
return
else
session.report('file',fullname, 'is not present')
end
end
rescue
session.error('unable to fetch file from path')
end
session.error('no file',filename, 'fetched') unless test(?e,filename)
end
def report (session)
filename = session.get('fb.filename')
paths = session.get('fb.paths')
paths.each do |path|
fullname = thefullname(path,request.naturalname(filename))
if test(?e,fullname)
begin
session.report('file', fullname)
session.report('size', test(?s,fullname))
if test(?e,fullname+'.tim')
str = IO.readlines(fullname+'.tim')
# str = IO.read(fullname+'.tim')
session.report('time', str)
end
rescue
session.error('unable to report about', fullname)
end
end
end
end
if session.get('option.submit')
prepare(session)
if session.get('option.request')
submitlist(session)
else
submitfile(session)
end
elsif session.get('option.fetch')
prepare(session)
fetch(session)
elsif session.get('option.report')
prepare(session)
report(session)
else
session.report('provide action')
end
|