Molecular Modeling Software
リビジョン | 49adab98c657ae4b09c86bd45c7f26bab4b172ac (tree) |
---|---|
日時 | 2014-04-25 14:18:32 |
作者 | toshinagata1964 <toshinagata1964@a2be...> |
コミッター | toshinagata1964 |
Setting the 'Scratch' directory is sorted out.
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@533 a2be9bc6-48de-4e38-9406-05402d4bc13c
@@ -107,6 +107,27 @@ class Molecule | ||
107 | 107 | |
108 | 108 | end |
109 | 109 | |
110 | +module Kernel | |
111 | + def ask_scratch_dir | |
112 | + sdir = get_global_settings("global.scratch_dir") | |
113 | + while 1 | |
114 | + p = Dialog.open_panel("Please select scratch directory", sdir, nil, true) | |
115 | + if p | |
116 | + if p =~ / / | |
117 | + error_message_box("Please avoid path containing a white space.\n" + p.sub(/ /, "<!> <!>")) | |
118 | + sdir = p | |
119 | + next | |
120 | + else | |
121 | + set_global_settings("global.scratch_dir", p) | |
122 | + return p | |
123 | + end | |
124 | + else | |
125 | + return nil | |
126 | + end | |
127 | + end | |
128 | + end | |
129 | +end | |
130 | + | |
110 | 131 | register_menu("Assign residue...", :cmd_assign_residue, :non_empty) |
111 | 132 | register_menu("Offset residue...", :cmd_offset_residue, :non_empty) |
112 | 133 | register_menu("Sort by residue", :cmd_sort_by_residue, :non_empty) |
@@ -1415,7 +1415,12 @@ end | ||
1415 | 1415 | |
1416 | 1416 | def cmd_show_ortep |
1417 | 1417 | mol = self |
1418 | - tmp = create_temp_dir("ortep", mol.name) | |
1418 | + begin | |
1419 | + tmp = create_temp_dir("ortep", mol.name) | |
1420 | + rescue | |
1421 | + error_message_box($!.to_s) | |
1422 | + return | |
1423 | + end | |
1419 | 1424 | tepexe = "#{ResourcePath}/ortep3/ortep3" |
1420 | 1425 | if $platform == "win" |
1421 | 1426 | tepexe += ".exe" |
@@ -1685,7 +1690,7 @@ def cmd_show_ortep | ||
1685 | 1690 | } |
1686 | 1691 | # Close handler (called when the close box is pressed or the document is closed) |
1687 | 1692 | @on_close = lambda { |*d| |
1688 | - cleanup_temp_dir(tmp) | |
1693 | + erase_old_logs(tmp) | |
1689 | 1694 | tmp = nil |
1690 | 1695 | true |
1691 | 1696 | } |
@@ -158,23 +158,28 @@ class Molecule | ||
158 | 158 | ncpus = 1 |
159 | 159 | end |
160 | 160 | |
161 | - # Prepare the scratch directory in the home directory | |
162 | - # (Not in the document home to avoid space-containing path in Windows) | |
163 | - scrdir = $home_directory + "/Test Dir/Molby/gamess" # "Test Dir" for debug | |
161 | + # Prepare the scratch directory | |
164 | 162 | begin |
165 | - mkdir_recursive(scrdir) | |
163 | + scrdir = create_temp_dir("gamess", inpbody) | |
166 | 164 | rescue |
167 | - error_message_box("Cannot create directory #{scrdir}: " + $!.to_s) | |
165 | + error_message_box($!.to_s) | |
168 | 166 | return |
169 | 167 | end |
168 | +# scrdir = $home_directory + "/Molby/gamess" | |
169 | +# begin | |
170 | +# mkdir_recursive(scrdir) | |
171 | +# rescue | |
172 | +# error_message_box("Cannot create directory #{scrdir}: " + $!.to_s) | |
173 | +# return | |
174 | +# end | |
170 | 175 | |
171 | - scrdir = scrdir + "/" + inpbody + "." + $$.to_s + ".0" | |
172 | - n = 0 | |
173 | - while File.exist?(scrdir) | |
174 | - scrdir = scrdir.sub(".#{n}", ".#{n + 1}") | |
175 | - n += 1 | |
176 | - end | |
177 | - Dir.mkdir(scrdir) | |
176 | +# scrdir = scrdir + "/" + inpbody + "." + $$.to_s + ".0" | |
177 | +# n = 0 | |
178 | +# while File.exist?(scrdir) | |
179 | +# scrdir = scrdir.sub(".#{n}", ".#{n + 1}") | |
180 | +# n += 1 | |
181 | +# end | |
182 | +# Dir.mkdir(scrdir) | |
178 | 183 | |
179 | 184 | if $platform == "win" |
180 | 185 | sep = "\\" |
@@ -196,7 +201,7 @@ class Molecule | ||
196 | 201 | end |
197 | 202 | uname = backquote("cmd.exe /c ver").to_s.gsub("\n", "") |
198 | 203 | else |
199 | - freebytes = `df -k #{scrdir}` | |
204 | + freebytes = `df -k "#{scrdir}"` | |
200 | 205 | uname = `uname`.chomp |
201 | 206 | end |
202 | 207 |
@@ -217,6 +222,7 @@ class Molecule | ||
217 | 222 | scrbody = scrprefix |
218 | 223 | end |
219 | 224 | filecopy(inpname, scrprefix + ".F05") |
225 | + File.open("#{scrdir}/.in_use", "w") { |fp| } | |
220 | 226 | |
221 | 227 | # Prepare environmental variables |
222 | 228 | auxdir = "#{gmsdir}#{sep}auxdata" |
@@ -413,6 +419,9 @@ class Molecule | ||
413 | 419 | icon = :error |
414 | 420 | end |
415 | 421 | msg += "\n(In directory #{inpdir})" |
422 | + | |
423 | + File.delete("#{scrdir}/.in_use") | |
424 | + | |
416 | 425 | ext_to_keep = [".dat", ".rst", ".trj", ".efp", ".gamma", ".log"] |
417 | 426 | ext_to_keep.each { |ex| |
418 | 427 | if File.exists?("#{scrprefix}#{ex}") |
@@ -424,7 +433,13 @@ class Molecule | ||
424 | 433 | File.delete("#{scrdir}#{sep}#{file}") |
425 | 434 | end |
426 | 435 | } |
427 | - erase_old_logs(scrdir, "latest", 5) | |
436 | + | |
437 | + begin | |
438 | + erase_old_logs(scrdir, "latest", 5) | |
439 | + rescue | |
440 | + error_message_box($!.to_s) | |
441 | + return | |
442 | + end | |
428 | 443 | if mol != nil |
429 | 444 | message_box(msg, hmsg, :ok, icon) |
430 | 445 | end |
@@ -493,13 +508,13 @@ class Molecule | ||
493 | 508 | if $platform == "win" |
494 | 509 | if gmsvers == "11" |
495 | 510 | hosts = "localhost " * ncpus |
496 | - cmdline = "cmd.exe /c \"#{gmsdir}/ddikick.exe #{gmsdir}/gamess.#{gmsvers}.exe #{inpbody} -ddi #{ncpus} #{ncpus} #{hosts} -scr \\\"#{scrdir}\\\" < NUL >>\\\"#{logname}\\\"" | |
511 | + cmdline = "cmd.exe /c \"#{gmsdir}/ddikick.exe #{gmsdir}/gamess.#{gmsvers}.exe #{inpbody} -ddi #{ncpus} #{ncpus} #{hosts} -scr #{scrdir} < NUL >>#{logname}" | |
497 | 512 | else |
498 | - cmdline = "cmd.exe /c \"mpiexec -configfile \\\"#{procfil}\\\" >>\\\"#{logname}\\\"" | |
513 | + cmdline = "cmd.exe /c \"mpiexec -configfile #{procfil} >>#{logname}" | |
499 | 514 | end |
500 | 515 | else |
501 | 516 | hosts = "localhost " * ncpus |
502 | - cmdline = "/bin/sh -c '\"#{gmsdir}/ddikick.x\" \"#{gmsdir}/gamess.#{gmsvers}.x\" #{inpbody} -ddi #{ncpus} #{ncpus} #{hosts} -scr \"#{scrdir}\" < /dev/null >>\"#{logname}\"'" | |
517 | + cmdline = "/bin/sh -c '#{gmsdir}/ddikick.x #{gmsdir}/gamess.#{gmsvers}.x #{inpbody} -ddi #{ncpus} #{ncpus} #{hosts} -scr #{scrdir} < /dev/null >>#{logname}'" | |
503 | 518 | end |
504 | 519 | |
505 | 520 | if mol |
@@ -235,10 +235,7 @@ class Molecule | ||
235 | 235 | end |
236 | 236 | |
237 | 237 | def ambertools_dialog(tool, msg = nil, block = nil) |
238 | - log_dir = get_global_settings("antechamber.log_dir") | |
239 | - if !log_dir | |
240 | - log_dir = $home_directory + "/Molby/antechamber" | |
241 | - end | |
238 | + log_dir = get_global_settings("global.scratch_dir") || "" | |
242 | 239 | if $platform == "win" |
243 | 240 | suffix = ".exe" |
244 | 241 | else |
@@ -292,10 +289,10 @@ class Molecule | ||
292 | 289 | -1, |
293 | 290 | item(:line), |
294 | 291 | -1, |
295 | - item(:text, :title=>"Log directory:"), | |
292 | + item(:text, :title=>"Scratch directory (default = home):"), | |
296 | 293 | [ item(:button, :title=>"Choose...", |
297 | 294 | :action=>lambda { |it| |
298 | - dir = Dialog.open_panel(nil, nil, nil, true) | |
295 | + dir = ask_scratch_dir | |
299 | 296 | if dir |
300 | 297 | set_value("log_dir", dir) |
301 | 298 | end |
@@ -326,6 +323,10 @@ class Molecule | ||
326 | 323 | } |
327 | 324 | hash.each_pair { |key, value| |
328 | 325 | next if key == :status |
326 | + if key == "log_dir" | |
327 | + set_global_settings("global.scratch_dir", value) | |
328 | + next | |
329 | + end | |
329 | 330 | v = [["log_none", "none"], ["log_error_only", "error_only"], ["log_keep_latest", "latest"], ["log_all", "all"]].assoc(key) |
330 | 331 | if v |
331 | 332 | next if value != 1 |
@@ -339,60 +340,72 @@ class Molecule | ||
339 | 340 | return 1 - hash[:status] # 1: OK, 0: Cancel, -1: Skip |
340 | 341 | end |
341 | 342 | |
342 | - def create_ante_log_dir(name, key) | |
343 | - log_dir = get_global_settings("antechamber.log_dir") | |
344 | - msg = "" | |
345 | - begin | |
346 | - if !FileTest.directory?(log_dir) | |
347 | - mkdir_recursive(log_dir) rescue ((msg = "Cannot create directory #{log_dir}") && raise) | |
348 | - end | |
349 | - n = 1 | |
350 | - while FileTest.exist?(dname = log_dir + "/#{name}_#{key}.#{n}") | |
351 | - n += 1 | |
352 | - end | |
353 | - Dir.mkdir(dname) rescue ((msg = "Cannot create directory #{dname}") && raise) | |
354 | - return dname | |
355 | - rescue | |
356 | - error_message_box(msg + ": " + $!.to_s) | |
357 | - return | |
358 | - end | |
359 | - end | |
343 | +# def create_ante_log_dir(name, key) | |
344 | +# log_dir = get_global_settings("global.scratch_dir") | |
345 | +# if log_dir == nil || log_dir == "" | |
346 | +# log_dir = ask_scratch_dir | |
347 | +# if log_dir == nil | |
348 | +# error_message_box("Scratch directory is not set.") | |
349 | +# return nil | |
350 | +# end | |
351 | +# end | |
352 | +# log_dir = log_dir + "/Molby/antechamber" | |
353 | +# msg = "" | |
354 | +# begin | |
355 | +# if !FileTest.directory?(log_dir) | |
356 | +# mkdir_recursive(log_dir) rescue ((msg = "Cannot create directory #{log_dir}") && raise) | |
357 | +# end | |
358 | +# n = 1 | |
359 | +# while FileTest.exist?(dname = log_dir + "/#{name}_#{key}.#{n}") | |
360 | +# n += 1 | |
361 | +# end | |
362 | +# Dir.mkdir(dname) rescue ((msg = "Cannot create directory #{dname}") && raise) | |
363 | +# return dname | |
364 | +# rescue | |
365 | +# error_message_box(msg + ": " + $!.to_s) | |
366 | +# return | |
367 | +# end | |
368 | +# end | |
360 | 369 | |
361 | - def clean_ante_log_dir(nkeep) | |
362 | - def rm_recursive(f) | |
363 | - if FileTest.directory?(f) | |
364 | - Dir.entries(f).each { |file| | |
365 | - next if file == "." || file == ".." | |
366 | - rm_recursive(f + "/" + file) | |
367 | - } | |
368 | - Dir.rmdir(f) | |
369 | - else | |
370 | - File.delete(f) | |
371 | - end | |
372 | - end | |
373 | - log_dir = get_global_settings("antechamber.log_dir") | |
374 | - cwd = Dir.pwd | |
375 | - count = 0 | |
376 | - begin | |
377 | - Dir.chdir(log_dir) | |
378 | - # Get subdirectories and sort by last modified time | |
379 | - dirs = Dir.entries(log_dir).reject! { |x| | |
380 | - !FileTest.directory?(x) || x == "." || x == ".." | |
381 | - }.sort_by { |x| | |
382 | - File.mtime(x) | |
383 | - } | |
384 | - dirs[0..-(nkeep + 1)].each { |d| | |
385 | - rm_recursive(d) | |
386 | - count += 1 | |
387 | - } | |
388 | - rescue | |
389 | - error_message_box $!.to_s | |
390 | - Dir.chdir(cwd) | |
391 | - return | |
392 | - end | |
393 | - Dir.chdir(cwd) | |
394 | - return count | |
395 | - end | |
370 | +# def clean_ante_log_dir(nkeep) | |
371 | +# def rm_recursive(f) | |
372 | +# if FileTest.directory?(f) | |
373 | +# Dir.entries(f).each { |file| | |
374 | +# next if file == "." || file == ".." | |
375 | +# rm_recursive(f + "/" + file) | |
376 | +# } | |
377 | +# Dir.rmdir(f) | |
378 | +# else | |
379 | +# File.delete(f) | |
380 | +# end | |
381 | +# end | |
382 | +# log_dir = get_global_settings("global.scratch_dir") | |
383 | +# if log_dir == nil || log_dir == "" | |
384 | +# return | |
385 | +# end | |
386 | +# log_dir = log_dir + "/Molby/antechamber" | |
387 | +# cwd = Dir.pwd | |
388 | +# count = 0 | |
389 | +# begin | |
390 | +# Dir.chdir(log_dir) | |
391 | +# # Get subdirectories and sort by last modified time | |
392 | +# dirs = Dir.entries(log_dir).reject! { |x| | |
393 | +# !FileTest.directory?(x) || x == "." || x == ".." | |
394 | +# }.sort_by { |x| | |
395 | +# File.mtime(x) | |
396 | +# } | |
397 | +# dirs[0..-(nkeep + 1)].each { |d| | |
398 | +# rm_recursive(d) | |
399 | +# count += 1 | |
400 | +# } | |
401 | +# rescue | |
402 | +# error_message_box $!.to_s | |
403 | +# Dir.chdir(cwd) | |
404 | +# return | |
405 | +# end | |
406 | +# Dir.chdir(cwd) | |
407 | +# return count | |
408 | +# end | |
396 | 409 | |
397 | 410 | def invoke_antechamber(ask_options = true, msg = nil) |
398 | 411 | # Find the ambertool directory |
@@ -407,25 +420,13 @@ class Molecule | ||
407 | 420 | guess_atom_types = get_global_settings("antechamber.guess_atom_types").to_i |
408 | 421 | optimize_structure = get_global_settings("antechamber.optimize_structure").to_i |
409 | 422 | use_residue = get_global_settings("antechamber.use_residue").to_i |
423 | + | |
410 | 424 | # Create log directory |
411 | 425 | name = (self.name || "unknown").sub(/\.\w*$/, "").sub(/\*/, "") # Remove the extension and "*" |
412 | - log_dir = get_global_settings("antechamber.log_dir") | |
413 | - if log_dir == nil | |
414 | - log_dir = document_home + "/Molby/antechamber" | |
415 | - end | |
416 | - if !File.directory?(log_dir) | |
417 | - mkdir_recursive(log_dir) | |
418 | - end | |
419 | - tdir = nil | |
420 | - 1000.times { |i| | |
421 | - tdir = sprintf("%s/%s.%04d", log_dir, name, i) | |
422 | - if !File.exists?(tdir) && (Dir.mkdir(tdir) == 0) | |
423 | - break | |
424 | - end | |
425 | - tdir = nil | |
426 | - } | |
427 | - if tdir == nil | |
428 | - error_message_box("Cannot create log directory in #{log_dir}.") | |
426 | + begin | |
427 | + tdir = create_temp_dir("antechamber", name) | |
428 | + rescue | |
429 | + error_message_box($!.to_s) | |
429 | 430 | return -1 |
430 | 431 | end |
431 | 432 | cwd = Dir.pwd |
@@ -1261,8 +1262,13 @@ class Molecule | ||
1261 | 1262 | ante_dir = Molby::ResourcePath + "/amber11/bin" |
1262 | 1263 | |
1263 | 1264 | # Create the temporary directory |
1264 | - dname = create_ante_log_dir((self.path ? File.basename(self.path, ".*") : self.name), "rs") | |
1265 | - return unless dname | |
1265 | + name = (self.name || "unknown").sub(/\.\w*$/, "").sub(/\*/, "") # Remove the extension and "*" | |
1266 | + begin | |
1267 | + dname = create_temp_dir("resp", name) | |
1268 | + rescue | |
1269 | + error_message_box($!.to_s) | |
1270 | + return -1 | |
1271 | + end | |
1266 | 1272 | cwd = Dir.pwd |
1267 | 1273 | Dir.chdir(dname) |
1268 | 1274 |
@@ -1381,6 +1387,11 @@ class Molecule | ||
1381 | 1387 | end |
1382 | 1388 | } |
1383 | 1389 | Dir.chdir(cwd) |
1390 | + | |
1391 | + log_level = get_global_settings("antechamber.log_level") | |
1392 | + log_keep_number = get_global_settings("antechamber.log_keep_number") | |
1393 | + erase_old_logs(dname, log_level, log_keep_number) | |
1394 | + | |
1384 | 1395 | return true |
1385 | 1396 | end |
1386 | 1397 |
@@ -1427,9 +1438,6 @@ class Molecule | ||
1427 | 1438 | [item(:button, :title=>"Run RESP...", :tag=>"resp", |
1428 | 1439 | :action=>lambda { |it| |
1429 | 1440 | if mol.cmd_run_resp |
1430 | - if get_global_settings("antechamber.log_level") == "latest" | |
1431 | - mol.clean_ante_log_dir(Integer(get_global_settings("antechamber.log_keep_number"))) | |
1432 | - end | |
1433 | 1441 | end_modal(0) |
1434 | 1442 | end |
1435 | 1443 | }), |
@@ -35,6 +35,24 @@ case RUBY_PLATFORM | ||
35 | 35 | $home_directory = ENV['HOME'] |
36 | 36 | end |
37 | 37 | |
38 | +sdir = get_global_settings("global.scratch_dir") | |
39 | +if sdir == nil || sdir == "" | |
40 | + sdir = $home_directory | |
41 | + if $platform == "win" && sdir =~ / / | |
42 | + # Try 8.3 name | |
43 | + tdir = ENV['TEMP'] | |
44 | + if tdir != nil | |
45 | + sdir = tdir.gsub(/\\/, "/") | |
46 | + end | |
47 | + if sdir =~ /\/AppData\/Local\/Temp$/ | |
48 | + sdir = Regexp.last_match.pre_match | |
49 | + end | |
50 | + end | |
51 | + if sdir !~ / / | |
52 | + set_global_settings("global.scratch_dir", sdir) | |
53 | + end | |
54 | +end | |
55 | + | |
38 | 56 | $backtrace = nil |
39 | 57 | |
40 | 58 | def backtrace |
@@ -103,18 +121,29 @@ module Kernel | ||
103 | 121 | end |
104 | 122 | end |
105 | 123 | def create_temp_dir(tag, name = nil) |
106 | - # Create a temporary directory like HomeDirectory/Molby/tag/name.xxxxxx | |
107 | - name ||= "temp" | |
108 | - base = $home_directory + "/Molby/" + tag | |
109 | - mkdir_recursive(base) | |
110 | - 10000.times { |n| | |
111 | - p = sprintf("%s/%s.%05d", base, name, n) | |
112 | - if !FileTest.exist?(p) | |
113 | - Dir.mkdir(p) | |
114 | - return p | |
124 | + # Create a temporary directory like %scratch%/Molby/tag/name.xxxxxx | |
125 | + base = get_global_settings("global.scratch_dir") | |
126 | + if base == nil || base == "" | |
127 | + base = ask_scratch_dir | |
128 | + if base == nil | |
129 | + raise "Scratch directory is not set." | |
115 | 130 | end |
116 | - } | |
117 | - raise "Cannot create temporary directory in #{base}" | |
131 | + end | |
132 | + msg = nil | |
133 | + name ||= "temp" | |
134 | + base = base + "/Molby/" + tag | |
135 | + begin | |
136 | + mkdir_recursive(base) rescue ((msg = "Cannot create directory #{base}") && raise) | |
137 | + 10000.times { |n| | |
138 | + p = sprintf("%s/%s.%05d", base, name, n) | |
139 | + if !FileTest.exist?(p) | |
140 | + Dir.mkdir(p) rescue ((msg = "Cannot create directory #{p}") && raise) | |
141 | + return p | |
142 | + end | |
143 | + } | |
144 | + rescue | |
145 | + raise "Cannot create temporary directory in #{base}" | |
146 | + end | |
118 | 147 | end |
119 | 148 | def rm_recursive(path) |
120 | 149 | if FileTest.directory?(path) |
@@ -127,39 +156,44 @@ module Kernel | ||
127 | 156 | File.unlink(path) |
128 | 157 | end |
129 | 158 | end |
130 | - def cleanup_temp_dir(path, option = nil) | |
159 | +# def cleanup_temp_dir(path, option = nil) | |
131 | 160 | # Clean-up temporary directories |
132 | 161 | # If option is nil, then the directory at path is removed |
133 | 162 | # If option is a number, then the directories that are in the same parent directory as path |
134 | 163 | # are removed except for the newest ones |
135 | - if path.index($home_directory + "/Molby/") != 0 | |
136 | - raise "Bad cleanup_temp_dir call: the path does not begin with $HOME/Molby/ (#{path})" | |
137 | - end | |
138 | - if !FileTest.directory?(path) | |
139 | - raise "Bad cleanup_temp_dir call: the path is not a directory (#{path})" | |
140 | - end | |
141 | - option = option.to_i | |
142 | - if option <= 0 | |
143 | - rm_recursive(path) | |
144 | - else | |
145 | - base = File.dirname(path) | |
146 | - ent = Dir.entries.sort_by { |en| File.mtime("#{base}/#{en}").to_i * (-1) } - [".", ".."] | |
147 | - if $platform == "mac" | |
148 | - ent -= [".DS_Store"] | |
149 | - end | |
150 | - ent[0, option] = [] # Remove newest #{option} entries | |
151 | - # Mark this directory to be ready to remove (see below) | |
152 | - open("#{path}/.done", "w") { |fp| } | |
153 | - # Remove the older directories | |
154 | - ent.each { |en| | |
155 | - # Check the existence of ".done" file (otherwise, we may accidentarily remove a directory | |
156 | - # that is still in use in other threads) | |
157 | - if File.exist?("#{base}/#{en}/.done") | |
158 | - rm_recursive("#{base}/#{en}") | |
159 | - end | |
160 | - } | |
161 | - end | |
162 | - end | |
164 | +# base = get_global_settings("global.scratch_dir") | |
165 | +# if base == nil || base == "" | |
166 | +# raise "Scratch directory is not set." | |
167 | +# end | |
168 | +# base = base + "/Molby/" | |
169 | +# if path.index(base) != 0 | |
170 | +# raise "Bad cleanup_temp_dir call: the path is not a part of the scratch directory (#{path})" | |
171 | +# end | |
172 | +# if !FileTest.directory?(path) | |
173 | +# raise "Bad cleanup_temp_dir call: the path is not a directory (#{path})" | |
174 | +# end | |
175 | +# option = option.to_i | |
176 | +# if option <= 0 | |
177 | +# rm_recursive(path) | |
178 | +# else | |
179 | +# base = File.dirname(path) | |
180 | +# ent = Dir.entries.sort_by { |en| File.mtime("#{base}/#{en}").to_i * (-1) } - [".", ".."] | |
181 | +# if $platform == "mac" | |
182 | +# ent -= [".DS_Store"] | |
183 | +# end | |
184 | +# ent[0, option] = [] # Remove newest #{option} entries | |
185 | +# # Mark this directory to be ready to remove (see below) | |
186 | +# open("#{path}/.done", "w") { |fp| } | |
187 | +# # Remove the older directories | |
188 | +# ent.each { |en| | |
189 | +# # Check the existence of ".done" file (otherwise, we may accidentarily remove a directory | |
190 | +# # that is still in use in other threads) | |
191 | +# if File.exist?("#{base}/#{en}/.done") | |
192 | +# rm_recursive("#{base}/#{en}") | |
193 | +# end | |
194 | +# } | |
195 | +# end | |
196 | +# end | |
163 | 197 | |
164 | 198 | def remove_dir(dir) |
165 | 199 | entries = Dir.entries(dir) |
@@ -175,8 +209,17 @@ module Kernel | ||
175 | 209 | Dir.unlink(dir) |
176 | 210 | end |
177 | 211 | |
178 | - def erase_old_logs(tdir, level, keep_number) | |
212 | + def erase_old_logs(tdir, level = nil, keep_number = 0) | |
179 | 213 | log_dir = File.dirname(tdir) |
214 | + base = get_global_settings("global.scratch_dir") | |
215 | + if base == nil || base == "" | |
216 | + raise "Scratch directory is not set." | |
217 | + end | |
218 | + base = base.gsub(/\\/, "/") + "/Molby/" | |
219 | + tdir = tdir.gsub(/\\/, "/") | |
220 | + if tdir.index(base) != 0 | |
221 | + raise "Bad erase_old_logs call: the path is not a part of the scratch directory (#{tdir}, #{base})" | |
222 | + end | |
180 | 223 | if level == nil || level == "none" |
181 | 224 | remove_dir(tdir) |
182 | 225 | elsif level == "latest" |
@@ -185,11 +228,18 @@ module Kernel | ||
185 | 228 | else |
186 | 229 | keep_number = keep_number.to_i |
187 | 230 | end |
188 | - entries = Dir.entries(log_dir).select { |en| en != "." && en != ".." && File.directory?("#{log_dir}/#{en}") } | |
189 | - # Sort by modification date | |
190 | - entries = entries.sort_by { |en| File.mtime("#{log_dir}/#{en}").to_i } | |
191 | - (0...entries.count - keep_number).each { |i| | |
192 | - remove_dir("#{log_dir}/#{entries[i]}") | |
231 | + entries = Dir.entries(log_dir).sort_by { |en| File.mtime("#{log_dir}/#{en}").to_i * (-1) } - [".", ".."] | |
232 | + if $platform == "mac" | |
233 | + entries -= [".DS_Store"] | |
234 | + end | |
235 | + entries[0, keep_number] = [] # Remove newest #{keep_number} entries | |
236 | + # Remove the older directories | |
237 | + entries.each { |en| | |
238 | + # Check the existence of ".in_use" file (otherwise, we may accidentarily remove a directory | |
239 | + # that is still in use in other threads) | |
240 | + if !File.exist?("#{log_dir}/#{en}/.in_use") | |
241 | + rm_recursive("#{log_dir}/#{en}") | |
242 | + end | |
193 | 243 | } |
194 | 244 | end |
195 | 245 | end |