On 14.05.2007 14:56, Rebhan, Gilbert wrote:
-----Original Message-----
i don’t want to bother with bad code, but here is the whole thing,
for you to understand what i try to achieve =
I was referring to the fact that you present one bit of code and in your
answer to my posting you present another bit of code as if that was
the original code.
However, there are quite a few things to say about this bit of code.
q.default = “#{ENV[“USERNAME”]}”
q.echo = true}
cvspass = ask("Enter password: ") { |q| q.echo = ‘*’ }
puts “\n\n”
config[‘cvsrepos’].each {|x|
puts “Login CVS Repository >> #{x} …”
IO.popen("#{config[‘CVSEXE’]} -d
:pserver:#{cvsuser}:#{cvspass}@cvsprod:d:/cvsrepos/#{x} login")
}
Why do you use popen if you do not read the pipe? If you use popen
you should make sure the pipe is read from - even if you ignore what you
find because otherwise the other process might get blocked.
}
Rather use the non block form of gsub unless you want to make sure that
there is no metacharacter interpretation going on. (Even in that case
I’d probably prefer the non block form with a modified replaceString.)
end
tmpFile.close
srcFile .close
Rather use the block form of File.open which is much safer.
s=config[‘replaceto1’]<<"#{@cvsreg}"<<config[‘replaceto2’]
Why do you use “#{@cvsreg}” instead of plain @cvsreg or @cvsreg.to_s?
config[‘targetdirs’].each do |dir|
puts “\n\n”
Find.find(dir) do |f|
if f =~ /#{config[‘targetfilepattern’]}/
You should pull out this regexp compilation from both loops for more
efficiency.
puts "... processing "<<f
filesed!(config['replacefrom'],s,f)
Indentation.
end
end
end
puts “\n\nDone !!”
sleep 1
Why the sleep?
else
puts “\n\nDone !!”
sleep 1
Why the sleep?
targetfilepattern: Scm\w+.xml$
replacefrom: “.*”
This pattern will kill you if there are two sections with
in the file. You should at least use the
reluctanct qualifier.
replaceto1: “<![CDATA[”
replaceto2: “]>”
This cries for using a regexp group. Also filesed is pretty inflexible.
I’d rather do something like this:
def file_replace(file, tmp = file + Time.now.usec.to_s)
File.open(tmp, “w”) do |out|
File.open(file) do |inf|
inf.each_line {|line| out.puts(yield(line)) }
end
end
File.mv(tmp, file, :force => true)
end
Now you can do arbitrary line based replacements like
file_replace “foo.txt” do |line|
"# " << line
end
----yaml----
the filesed method is not by me, found it somewhere, don’t remember
Regards, Gilbert
Regards
robert