merge_files

in tools/merge_subscriptions.rb [31:133]


def merge_files(old_host, new_host, out)
  
  counts = {}
  File.open(File.join(old_host, 'list-counts')).each do |line|
    if line =~ %r{^( *\d+) (\S+)}
      counts[$2] = $1
    else
      puts "Unexpected count line #{line}"
    end
  end

  File.open(File.join(new_host, 'list-counts')).each do |line|
    if line =~ %r{^( *\d+) (\S+)}
      counts[$2] = $1
    else
      puts "Unexpected count line #{line}"
    end
  end

  File.open(File.join(out, 'list-counts'), 'w') do |f|
    counts.delete('total') 
    counts.sort.each do |k, v|
      f.puts "#{v} #{k}"
    end
  end

  
  old_flags = File.join(old_host, 'list-flags')
  new_flags = File.join(new_host, 'list-flags')
  out_flags = File.join(out, 'list-flags')
  out_time = File.mtime(out_flags) rescue 0
  
  if File.mtime(old_flags) > out_time || File.mtime(new_flags) > out_time

    flags = {}
    File.open(old_flags).each do |line|
      parts = line.chomp.split(' ', 2)
      flags[parts[1]] = parts[0]
    end

    File.open(new_flags).each do |line|
      parts = line.chomp.split(' ', 2)
      flags[parts[1]] = parts[0]
    end

    File.open(out_flags, 'w') do |f|
      flags.sort.each do |k, v|
        f.puts "#{v} #{k}"
      end
    end

  end

    
  Find.find(File.join(new_host, 'cache')) do |path|
    if File.file? path
      targ = path.sub(new_host, out)
      dir = File.dirname(targ)
      unless File.directory? dir
        
        FileUtils.mkdir_p dir
      end
      
      
      FileUtils.symlink path, targ, force: true
    end
  end

  
  Find.find(File.join(old_host, 'cache')) do |path|
    if File.file? path
      targ = path.sub(old_host, out)
      
      
      unless File.exist?(targ) && File.ftype(targ) == 'link'
        dir = File.dirname(targ)
        unless File.directory? dir
          
          FileUtils.mkdir_p dir
        end
        
        FileUtils.symlink path, targ, force: true
      end
    end
  end

  
  Find.find(File.join(out, 'cache')) do |path|
    next if File.directory? path
    unless File.exist?(path) && File.ftype(path) == 'link'
      $stderr.puts "WARN: Removing real file or missing link #{path}"
      begin
        File.unlink(path)
      rescue StandardError => e
        $stderr.puts "WARN: Failed to remove path #{e.inspect}"
      end
    end
  end

  
  FileUtils.copy_file(File.join(new_host, 'list-start'), File.join(out, 'list-start'), true)
end