Advertisement
orgads

qbs sort

Sep 30th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.92 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. File.open(ARGV[0]) { |input|
  4.   File.open(ARGV[0] + '.sorted', 'w') { |out|
  5.     sorting = false
  6.     lists = {}
  7.     while line = input.gets
  8.       prefix = ''
  9.       if sorting
  10.         prefix = line.sub(/[^\/]*$/, '')
  11.         lists[prefix] = [] if lists[prefix].nil?
  12.       end
  13.       list = lists[prefix]
  14.       if sorting and line !~ /\]/
  15.         list << line.chomp
  16.       elsif line =~ /\]/
  17.         all = []
  18.         lists.keys.sort.each { |key|
  19.           list = lists[key]
  20.           list.sort!
  21.           list.map! { |file| file !~ /,$/ ? file + ',' : file }
  22.           all += list
  23.         }
  24. #        all[-1].sub!(/,$/, '') unless all.empty?
  25.         all.each { |file| out.puts file }
  26.         lists.clear
  27.         sorting = false
  28.       elsif line =~ /files.*\[/
  29.     out.puts line
  30.         sorting = true
  31.       end
  32.       out.puts line unless sorting
  33.     end
  34.   }
  35. }
  36. File.rename(ARGV[0] + '.sorted', ARGV[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement