Class: Six::Cleanup

Inherits:
Object
  • Object
show all
Defined in:
lib/six/cleanup.rb

Class Method Summary collapse

Class Method Details

.check_dir(dir) ⇒ Object



49
50
51
52
53
54
# File 'lib/six/cleanup.rb', line 49

def self.check_dir(dir)
  return nil unless File.directory?(dir)
  entry = File.join(dir, ".repository.yml")
  return nil unless File.exists?(entry)
  true
end

.cleanup(entry, repack = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/six/cleanup.rb', line 35

def self.cleanup(entry, repack = false)
  dirname = File.dirname(entry)
  base = File.basename(entry)

  # Rename if the base contains capitals
  if base =~ /[A-Z]/
    newentry = File.join(dirname, base.downcase)
    rename(entry, newentry)
    entry = newentry
    base = File.basename(entry)
  end
  check_dir(entry) ? entry : nil
end

.handle_repo(dir, repack = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/six/cleanup.rb', line 56

def self.handle_repo(dir, repack = false)
  return unless File.directory?(dir)
  entry = File.join(dir, ".repository.yml")
  return unless File.exists?(entry)
  #puts "Processing: #{entry}"
  config = YAML::load_file(entry)
  [:pack, :wd].each do |typ|
    ar = []
    config[typ].each do |e|
      sum = if repack && e[0] =~ /.*[A-Z].*\.gz$/
        entry = File.join(dir, e[0])
        entry = repack(entry)
        md5sum(entry)
      else
        e[1]
      end
      ar << [e[0].downcase, sum]
    end
    config[typ] = ar
  end
  File.open(entry, 'w') {|f| f.puts config.to_yaml }#.to_s
end

.md5sum(target) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/six/cleanup.rb', line 10

def self.md5sum(target)
  folder, file = File.dirname(target), File.basename(target)
  r = Dir.chdir(folder) do
    %x[md5sum "#{file}"]
  end
  r[/\A\w*/]
end

.process(dir, repack = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/six/cleanup.rb', line 18

def self.process(dir, repack = false)
  return unless File.directory?(dir)

  repos = []
  # Handle folder and file renames, in reverse, and . and .. excluded
  Dir.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).reject{ |e|
    ["..", "."].include? File.basename(e) }.reverse.each do |entry|
    r = cleanup(entry)
    repos << r if r
  end

  # Handle all .repository.yml as last
  repos.each do |e|
    handle_repo(e, repack)
  end
end

.rename(entry, newentry) ⇒ Object



79
80
81
82
# File 'lib/six/cleanup.rb', line 79

def self.rename(entry, newentry)
  FileUtils.mv(entry, "#{entry}_tmp")
  FileUtils.mv("#{entry}_tmp", newentry)
end

.repack(entry) ⇒ Object

TODO: take a serious look at repack :) especially with handle_repo



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/six/cleanup.rb', line 85

def self.repack(entry)
  dirname = File.dirname(entry)
  base = File.basename(entry)
  if base =~ /\.gz$/
    Dir.chdir(dirname) do
      f = base.clone
      f.sub!(/\.gz$/, "")
      puts "Repacking #{f} to #{f.downcase}"
      system "gzip -d \"#{base}\""
      rename(f, f.downcase)
      base = f.downcase
      system "gzip --rsyncable --best \"#{base}\""
      File.join(dirname, base)
    end
  end
end