Class: Smithy::FileOperations
- Inherits:
-
Object
- Object
- Smithy::FileOperations
- Defined in:
- lib/smithy/file_operations.rb
Class Method Summary collapse
- .install_file(source, dest, options = {}) ⇒ Object
- .install_from_string(content, dest, options = {}) ⇒ Object
- .make_directory(d, options = {}) ⇒ Object
- .make_executable(f, options = {}) ⇒ Object
- .make_group_writable(f, options = {}) ⇒ Object
- .make_symlink(old, new, options = {}) ⇒ Object
- .render_erb(args = {}) ⇒ Object
- .set_group(f, new_group, options = {}) ⇒ Object
Class Method Details
.install_file(source, dest, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/smithy/file_operations.rb', line 134 def install_file(source, dest, = {}) current_time = Time.now.to_i duplicate_dest = dest+"_copy_"+current_time.to_s installed = false force = .try(:[],:force) force = Smithy::Config.global.try(:[], :"force") unless force .reject!{|k,v| k==:force} if File.exists?(dest) && !force if FileUtils.identical?(source, dest) notice_identical dest installed = true else notice_conflict dest overwrite = "unknown" while overwrite == "unknown" do prompt = Readline.readline(" "*FILE_NOTICE_COLUMNS+"Overwrite? (enter \"h\" for help) [ynsdh] ") case prompt.downcase when "y" overwrite = true when "n" overwrite = false when "s" overwrite = false duplicate = true when "d" puts `diff -uw #{dest} #{source}` when "h" indent = " "*FILE_NOTICE_COLUMNS puts indent+"y - yes, overwrite" puts indent+"n - no, do not overwrite" puts indent+"s - save to a separate file" puts indent+"d - diff, show the differences between the old and the new" puts indent+"h - help, show this help}" #when "q" #raise "Abort new package" #else #overwrite = true end end if overwrite notice_force dest FileUtils.install source, dest, installed = true else if duplicate FileUtils.install source, duplicate_dest, notice_create duplicate_dest else notice_skip dest end end end else FileUtils.install source, dest, notice_create dest installed = true end return installed end |
.install_from_string(content, dest, options = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/smithy/file_operations.rb', line 198 def install_from_string(content, dest, = {}) if [:noop] updated_file = File.join(Smithy::Config.homedir+"/.smithy_#{File.basename(dest)}_#{Time.now.to_i}") else updated_file = File.join(File.dirname(dest), ".#{File.basename(dest)}_#{Time.now.to_i}") end File.open(updated_file , "w+") do |f| f.write(content) end FileOperations.install_file updated_file, dest, FileUtils.rm_f(updated_file) # Always remove end |
.make_directory(d, options = {}) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/smithy/file_operations.rb', line 115 def make_directory(d, = {}) if File.directory?(d) notice_exist d else FileUtils.mkdir_p d, notice_create d end end |
.make_executable(f, options = {}) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/smithy/file_operations.rb', line 69 def make_executable(f, = {}) unless [:noop] p = File.stat(f).mode | 0111 FileUtils.chmod p, f, end end |
.make_group_writable(f, options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/smithy/file_operations.rb', line 93 def make_group_writable(f, = {}) f = f.path if f.class == File # FileUtils.chmod_R doesn't work well for combinations of files # with different bitmasks, it sets everything the same if .has_key? :recursive command = "chmod -R g+w #{f}" else command = "chmod g+w #{f}" unless [:noop] # Check to see if it's already group writable # convert the integer to a string in base 8 mode = File.stat(f).mode.to_s(8) # check the group bit, convert back to integer group_bit = mode[mode.size-2].to_i return if group_bit == 6 || group_bit == 7 end end puts command if [:verbose] `#{command}` unless [:noop] end |
.make_symlink(old, new, options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/smithy/file_operations.rb', line 124 def make_symlink(old, new, = {}) if File.symlink?(new) notice_exist new else FileUtils.rm_f(new, ) FileUtils.ln_sf(old, new, ) notice_link old, new end end |
.render_erb(args = {}) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/smithy/file_operations.rb', line 213 def render_erb(args = {}) args[:options] = {} unless args[:options] = {:noop => false, :verbose => false} .merge!(args[:options]) erb_filename = args[:erb] dest = args[:destination] if [:noop] rendered_file = Smithy::Config.homedir+"/.#{File.basename(dest)}_#{Time.now.to_i}" else rendered_file = "#{File.dirname(dest)}/.#{File.basename(dest)}_#{Time.now.to_i}" end erb_string = args[:erb_string] erb_string = File.read(erb_filename) if erb_string.blank? erb = ERB.new(erb_string, nil, "<>") File.open(rendered_file, "w+") do |f| f.write erb.result(args[:binding]) end FileOperations.install_file(rendered_file, dest, ) FileUtils.rm_f(rendered_file) # Always remove end |
.set_group(f, new_group, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/smithy/file_operations.rb', line 76 def set_group(f, new_group, = {}) method = :chown if .has_key? :recursive .reject!{|k,v| k.eql?(:recursive)} method = :chown_R end current_group = Etc.getgrgid(File.stat(f).gid).name rescue nil return if current_group.eql?(new_group) && method == :chown begin FileUtils.send method, nil, new_group, f, rescue # raise "Could not set group \"#{new_group}\" on \"#{f}\"" end end |