Module: Fs

Defined in:
lib/posix-fileutils/fileutils.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.defoptsObject

Returns the value of attribute defopts.



32
33
34
# File 'lib/posix-fileutils/fileutils.rb', line 32

def defopts
  @defopts
end

Class Method Details

.cd(list, *opts, &block) ⇒ Object

Raises:

  • (ArgumentError)


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/posix-fileutils/fileutils.rb', line 240

def self.cd list, *opts, &block
  opts = opts.to_set

  list, opts = parse_list_args list, *opts
  opts.flags = [:v]

  raise ArgumentError if list.a?

  return false unless list.directory?

  cwd = self.pwd
  old_defopts = @defopts.clone

  @defopts += opts

  FileUtils.cd list.to_s, opts.include?(:v) ? {:verbose => true} : {:verbose => false}

  if block
    err = nil

    begin
      yield
    rescue => e
      err = e
    end

    self.cd Pathname.new(list).absolute? ? cwd : cwd.relative_path_from(self.pwd), *opts
    @defopts = old_defopts

    raise err if err
  end

  true
end

.cp(src, dst, *opts) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/posix-fileutils/fileutils.rb', line 80

def self.cp src, dst, *opts
  opts = opts.to_set

  src, dst, opts = parse_cp_args src, dst, *opts

  opts << :r if src.a? ? (src.inject(false) do |a,p| p.directory? | a end) : src.directory?

  Kernel.system "cp #{opts.to_s}#{src.to_s} #{dst.to_s}"
end

.diff(file1, file2, *opts) ⇒ Object

Raises:

  • (ArgumentError)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/posix-fileutils/fileutils.rb', line 279

def self.diff file1, file2, *opts
  file1 = Pathname.new file1 unless file1.kind_of? Pathname
  file2 = Pathname.new file2 unless file2.kind_of? Pathname

  raise ArgumentError unless file1.directory? == file2.directory?

  if file1.directory?
    list1 = Dir["#{file1.to_s}/**/{*,.*}"]
    list2 = Dir["#{file2.to_s}/**/{*,.*}"]

    rellist1 = list1.map do |e| Pathname.new(e).relative_path_from file1 end
    rellist2 = list2.map do |e| Pathname.new(e).relative_path_from file2 end

    return true unless rellist1.to_set == rellist2.to_set

    list1.each do |file|
      file = Pathname.new file
      ofile = file2 + file.relative_path_from(file1)

      if file.directory?
        return true if Dir["#{file.to_s}/{*,.*}"] == Dir["#{ofile.to_s}/{*,.*}"]

        next
      end

      return true unless ofile.exist?
      return true if ofile.directory?
      return true unless Digest::SHA256.file(file) == Digest::SHA256.file(ofile)
    end

    return false
  end

  return true unless Digest::SHA256.file(file1) == Digest::SHA256.file(file2)

  false
end

.dsync(src, dst, *opts) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/posix-fileutils/fileutils.rb', line 90

def self.dsync src, dst, *opts
  src, dst, opts = parse_cp_args src, dst, *opts

  opts = opts.to_set
  
  raise ArgumentError unless src.directory? && dst.directory?

  if opts.include? :d
    Dir["#{dst.to_s}/**/{*,.*}"].each do |file|
      file = Pathname.new file
      next unless file.exist?

      srcfile = src + file.relative_path_from(dst)

      (rm file, *(opts&@popts[:rm]) or return false) unless srcfile.exist?
    end
  end
  
  Dir["#{src.to_s}/**/{*,.*}"].each do |file|
    file = Pathname.new file
    next if file.directory?

    dstfile = dst + file.relative_path_from(src)

    (mkdir dstfile.dirname, *((opts+[:p])&@popts[:mkdir]) or return false) unless dstfile.dirname.directory?

    next if dstfile.file? && Digest::SHA256.file(file) == Digest::SHA256.file(dstfile)

    cp file, dstfile, *(opts&@popts[:cp]) or return false
  end

  true
end

.mkdir(list, *opts, &block) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/posix-fileutils/fileutils.rb', line 182

def self.mkdir list, *opts, &block
  opts = opts.to_set

  list, opts = parse_list_args list, *opts
  opts.flags = [:v, :p]

  if opts.include? :f
    unless list.a?
      return true if list.directory?
    else
      list.select! do |elem| !elem.directory? end
      return true if list.empty?
    end
  end

  res = Kernel.system "mkdir #{opts.to_s}#{list.to_s}"
  
  return res if res != true

  raise ArgumentError, 'Only in case single directory creating passing block is permitted' if list.a? && block

  if block
    err = nil

    cwd = Fs.pwd
    old_defopts = @defopts.clone
    Fs.cd list, *(opts&@popts[:mkdir])

    @defopts += opts

    begin
      yield
    rescue => e
      err = e
    end
    
    Fs.cd Pathname.new(list).absolute? ? cwd : cwd.relative_path_from(self.pwd), *(opts&@popts[:mkdir])
    @defopts = old_defopts

    raise err if e
  end

  true
end

.mv(src, dst, *opts) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/posix-fileutils/fileutils.rb', line 124

def self.mv src, dst, *opts
  opts = opts.to_set

  src, dst, opts = parse_cp_args src, dst, *opts

  Kernel.system "mv #{opts.to_s}#{src.to_s} #{dst.to_s}"
end

.parse_cp_args(src, dst, *opts) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/posix-fileutils/fileutils.rb', line 39

def self.parse_cp_args src, dst, *opts
  raise ArgumentError unless !dst.a?

  dst = Pathname.new dst.to_s unless dst.kind_of? Pathname

  if src.a?
    return true if src.count == 0

    raise ArgumentError unless dst.directory?

    src.map! do |elem| Pathname.new elem.to_s unless elem.kind_of? Pathname end

    class << src
      def to_s
        inject('') do |accu, elem| "#{accu}#{elem.to_s} " end.chomp ' '
      end
    end
  else
    src = Pathname.new src.to_s unless src.kind_of? Pathname
  end

  class << opts
    attr_accessor :src
    attr_accessor :dst

    def to_s
      opts_s = '-f '

      [:T, :a, :v, :r].each do |opt|
        opts_s << "-#{opt.to_s} " if include?(opt) || Fs.defopts.include?(opt)
      end

      opts_s
    end
  end

  opts.src, opts.dst = src, dst

  [src, dst, opts]
end

.parse_list_args(list, *opts) ⇒ Object



132
133
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
# File 'lib/posix-fileutils/fileutils.rb', line 132

def self.parse_list_args list, *opts
  opts = opts.to_set

  if list.a?
    return [[],[].to_set] if list.count == 0

    list.map! do |elem| Pathname.new elem.to_s unless elem.kind_of? Pathname end

    class << list
      def to_s
        inject('') do |accu,elem| "#{accu}#{elem.to_s} " end.chomp ' '
      end
    end
  else
    list = Pathname.new list.to_s unless list.kind_of? Pathname
  end

  class << opts
    attr_accessor :list
    attr_accessor :flags

    def to_s
      opts_s = ''

      @flags.each do |opt|
        opts_s << "-#{opt.to_s} " if include?(opt) || Fs.defopts.include?(opt)
      end

      opts_s
    end
  end

  opts.list = list

  [list, opts]
end

.poptsObject



35
36
37
# File 'lib/posix-fileutils/fileutils.rb', line 35

def self.popts
  @popts.clone
end

.pwdObject



275
276
277
# File 'lib/posix-fileutils/fileutils.rb', line 275

def self.pwd
  Pathname.new(`pwd`.chomp)
end

.rm(list, *opts) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/posix-fileutils/fileutils.rb', line 169

def self.rm list, *opts
  return true if list.a? && list.empty?

  opts = opts.to_set

  list, opts = parse_list_args list, *opts
  opts.flags = [:v, :f, :r]

  opts << :r if list.a? ? (list.inject(false) do |a,p| p.directory? | a end) : list.directory?

  Kernel.system "rm #{opts.to_s}#{list.to_s}"
end

.touch(list, *opts) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/posix-fileutils/fileutils.rb', line 227

def self.touch list, *opts
  list, opts = parse_list_args list, *opts
  opts.flags = [:v]

  opts = opts.to_set

  res = Kernel.system "touch #{list.to_s}"

  puts "touched files #{list.to_s}" if (opts|@defopts.to_set).include? :v

  res
end