Class: Normalizer

Inherits:
Object
  • Object
show all
Includes:
Compressible
Defined in:
lib/escoffier/normalizer.rb

Constant Summary collapse

COMPRESSION_REGEXES =
/\.bz2$/
PFILE_REGEXES =
[/^P\d{5}\.7$/]
DICOM_REGEXES =
[/\d{3,5}.dcm$/, /I?.\d{4}$/ ]
IMAGE_REGEXES =
[PFILE_REGEXES, DICOM_REGEXES].flatten

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compressible

#unzip, #zip

Constructor Details

#initialize(entry_path, options = Hash.new) ⇒ Normalizer

Returns a new instance of Normalizer.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/escoffier/normalizer.rb', line 15

def initialize(entry_path, options = Hash.new)
  if options[:user]
    user = options[:user]
  else
    user = 'raw'
  end
  
  if options[:group]
    group = options[:group]
  else
    group = 'raw'
  end
  
  if options[:dry_run]
    @dry_run = true
  else
    @dry_run = false
  end
  
  unless Process.uid == 0 || @dry_run
    puts "The normalizer must be run as root for correct permissions. Please use 'sudo'." 
    exit
  end
      
  @entry_path = entry_path
  @uid, @gid, @username, @groupname = Etc.getpwnam(user).uid, Etc.getgrnam(group).gid, user, group
end

Instance Attribute Details

#entry_pathObject (readonly)

Returns the value of attribute entry_path.



13
14
15
# File 'lib/escoffier/normalizer.rb', line 13

def entry_path
  @entry_path
end

Instance Method Details

#normalize_compression(file) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/escoffier/normalizer.rb', line 57

def normalize_compression(file)
   IMAGE_REGEXES.each do |regex|
     if file =~ regex
       file = zip(file) unless @dry_run
     end
   end

  # if file =~ COMPRESSION_REGEXES
  #  unzip(file)
  # end
  
  return file
end

#normalize_directory!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/escoffier/normalizer.rb', line 43

def normalize_directory!
  puts "Scanning #{@entry_path}"
  
  # Toggle Zippedness
  Find.find(@entry_path) do |file|
    next unless File.exists?(file)
    next if File.directory?(file)

    file = normalize_compression(file)
    normalize_ownership(file)

  end
end

#normalize_ownership(file, owner = 'raw', maximum_perms = 0755) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/escoffier/normalizer.rb', line 71

def normalize_ownership(file, owner = 'raw', maximum_perms = 0755)
  stat = File.stat(file)
  
  file_uid, file_gid, mode = stat.uid, stat.gid, stat.mode
  
  begin
    if file_uid != @uid
      begin
        current_owner = Etc.getpwuid(file_uid).name
      rescue ArgumentError # No such user; just use UID
        current_owner = "uid #{file_uid}"
      end
      puts " CHOWN #{file}"
      puts "  Current owner is #{current_owner}, should be #{@username}"
      File.chown(@uid, nil, file) unless @dry_run
    end
  
    if file_gid != @gid
      begin
        current_group = Etc.getgrgid(file_gid).name
      rescue ArgumentError # No such group; just use GID
        current_group = "gid #{file_gid}"
      end
      puts " CHOWN #{file}"
      puts "  Current group is #{current_group}, should be #{@groupname}"
      File.chown(nil, @gid, file) unless @dry_run
    end
  
    perms = mode & 0777
    should_be = perms & maximum_perms
    if perms != should_be
      puts " CHMOD #{file}"
      puts "  Current perms are #{perms.to_s(8)}, should be #{should_be.to_s(8)}"
      File.chmod(perms & maximum_perms, file) unless @dry_run
    end
  rescue Exception => e
    if e.errno == 'eperm'
      puts "Cannot change ownership - insufficient permissions."
    else
      raise e
    end
  end
end