Module: Pa::ClassMethods::Cmd

Included in:
Pa
Defined in:
lib/tagen/core/pa/cmd.rb

Instance Method Summary collapse

Instance Method Details

#chroot(path) ⇒ nil

chroot

Parameters:

Returns:

  • (nil)

See Also:

  • Pa::ClassMethods::Cmd.{Dir{Dir.chroot}


19
# File 'lib/tagen/core/pa/cmd.rb', line 19

def chroot(path) Dir.chroot(get(path)) end

#cp(src_s, dest, o) ⇒ nil #cp(src_s, dest, o) {|src, dest, o| ... } ⇒ nil

copy

cp file dir

cp 'a', 'dir' #=> dir/a 
cp 'a', 'dir/a' #=> dir/a

cp file1 file2 .. dir

cp ['a','b'], 'dir' #=> dir/a dir/b

Examples:

cp '*', 'dir' do |src, dest, o|
  skip if src.name=~'.o$'
  dest.replace 'dirc' if src.name=="foo"
  yield  # use yield to do the actuactal cp work
end

Overloads:

  • #cp(src_s, dest, o) ⇒ nil

    Parameters:

    Options Hash (o):

    • :mkdir (Boolean)

      mkdir(dest) if dest not exists.

    • :verbose (Boolean)

      puts cmd when execute

    • :folsymlink (Boolean)

      follow symlink

    • :overwrite (Boolean)

      overwrite dest file if dest is a file

    • :special (Boolean)

      special copy, when cp a directory, only mkdir, not cp the directory’s content, usefull in Pa.each_r

    Returns:

    • (nil)
  • #cp(src_s, dest, o) {|src, dest, o| ... } ⇒ nil

    Yields:

    • (src, dest, o)

    Returns:

    • (nil)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/tagen/core/pa/cmd.rb', line 239

def cp(src_s, dest, o={}, &blk)
	srcs = glob(*Array.wrap(src_s)).map{|v| v.path}
	dest = Pa.get(dest)

	if o[:mkdir] and (not File.exists?(dest))
		Pa.mkdir dest
	end

	# cp file1 file2 .. dir
	if srcs.size>1 and (not File.directory?(dest))
		raise Errno::ENOTDIR, "dest not a directory when cp more than one src -- #{dest}"  
	end

	srcs.each do |src|
		dest1 = File.directory?(dest) ? File.join(dest, File.basename(src)) : dest

		if blk
			blk.call src, dest1, o, proc{_copy(src, dest1, o)}
		else
			_copy src, dest1, o
		end

	end
end

#home(user = nil) ⇒ Object

def mktmpdir



113
# File 'lib/tagen/core/pa/cmd.rb', line 113

def home(user=nil) Dir.home end

#mkdir(*paths, o = {}) ⇒ nil

make a directory

Parameters:

  • *paths (String)
  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :mode (Fixnum)
  • :force (Boolean)

Returns:

  • (nil)


67
# File 'lib/tagen/core/pa/cmd.rb', line 67

def mkdir(*args) paths, o = args.extract_options; _mkdir(paths, o) end

#mkdir_f(*paths, o = {}) ⇒ nil

mkdir force

Returns:

  • (nil)

See Also:



74
# File 'lib/tagen/core/pa/cmd.rb', line 74

def mkdir_f(*args) paths, o = args.extract_options; o[:force]=true; _mkdir(*paths, o) end

#mktmpdir(o = {}, &blk) ⇒ String

make temp directory

Parameters:

  • o (Hash) (defaults to: {})

    options

Options Hash (o):

  • :prefix (Symbol) — default: ""
  • :suffix (Symbol) — default: ""
  • :tmpdir (Symbol) — default: ENV["TEMP"]

Returns:



106
107
108
109
110
111
# File 'lib/tagen/core/pa/cmd.rb', line 106

def mktmpdir(o={}, &blk) 
	p = _mktmpname(o)
	File.mkdir(p)
	begin blk.call(p) ensure Dir.delete(p) end if blk
	p
end

#mktmpfile(o = {}, &blk) ⇒ String

make temp file

Parameters:

  • o (Hash) (defaults to: {})

    options

Returns:

See Also:



120
121
122
123
124
# File 'lib/tagen/core/pa/cmd.rb', line 120

def mktmpfile(o={}, &blk) 
	p = _mktmpname(o) 
	begin blk.call(p) ensure File.delete(p) end if blk
	p
end

#mv(src_s, dest, o = {}, &blk) ⇒ nil

move, use rename for same device. and cp for cross device.

Parameters:

  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :verbose (Boolean)
  • :mkdir (Boolean)
  • :overwrite (Boolean)

Returns:

  • (nil)

See Also:



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/tagen/core/pa/cmd.rb', line 323

def mv(src_s, dest, o={}, &blk)
	srcs = glob(*Array.wrap(src_s)).map{|v| get(v)}
	dest = get(dest)

	if o[:mkdir] and (not File.exists?(dest))
		mkdir dest
	end

	# mv file1 file2 .. dir
	if srcs.size>1 and (not File.directory?(dest))
		raise Errno::ENOTDIR, "dest not a directory when mv more than one src -- #{dest}"  
	end

	srcs.each do |src|
		dest1 = File.directory?(dest) ? File.join(dest, File.basename(src)) : dest

		if blk
			blk.call src, dest1, o, proc{_move(src, dest1, o)}
		else
			_move src, dest1, o
		end

	end
end

#rm(*paths) ⇒ nil

rm file only

Parameters:

  • *paths (String)

    support globbing

Returns:

  • (nil)


152
153
154
155
156
157
# File 'lib/tagen/core/pa/cmd.rb', line 152

def rm(*paths) 
	glob(*paths) { |pa|
		next if not File.exists?(pa.p)
		File.delete(pa.p)
	}
end

#rm_if(*paths) {|path| ... } ⇒ nil

rm_r(path) if condition is true

Examples:

Pa.rm_if '/tmp/**/*.rb' do |pa|
  pa.name == 'old'
end

Parameters:

  • *paths (String)

    support globbing

Yields:

  • (path)

Yield Parameters:

  • path (Pa)

Yield Returns:

  • (Boolean)

    rm_r path if true

Returns:

  • (nil)


193
194
195
196
197
# File 'lib/tagen/core/pa/cmd.rb', line 193

def rm_if(*paths, &blk)
	glob(*paths) do |pa|
		rm_r pa if blk.call(pa)
	end
end

#rm_r(*paths) ⇒ nil

rm recusive, rm both file and directory

Returns:

  • (nil)

See Also:



174
175
176
177
178
179
# File 'lib/tagen/core/pa/cmd.rb', line 174

def rm_r(*paths)
	glob(*paths){ |pa|
		next if not File.exists?(pa.p)
		File.directory?(pa.p)  ? _rmdir(pa) : File.delete(pa.p)
	}
end

#rmdir(*paths) ⇒ nil

rm directory only. still remove if directory is not empty.

Parameters:

  • *paths (String)

    support globbing

Returns:

  • (nil)


163
164
165
166
167
168
# File 'lib/tagen/core/pa/cmd.rb', line 163

def rmdir *paths
	glob(*paths) { |pa|
		raise Errno::ENOTDIR, "-- #{pa}" if not File.directory?(pa.p)
		_rmdir(pa)
	}
end

#touch(*paths, o = {}) ⇒ nil

touch a blank file

Parameters:

  • *paths (String)
  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :mode (Fixnum, String)
  • :mkdir (Boolean)

    auto mkdir if path contained directory not exists.

  • :force (Boolean)

Returns:

  • (nil)


30
# File 'lib/tagen/core/pa/cmd.rb', line 30

def touch(*args) paths, o = args.extract_options; _touch(*paths, o) end

#touch_f(*paths, o = {}) ⇒ nil

touch force

Returns:

  • (nil)

See Also:



37
# File 'lib/tagen/core/pa/cmd.rb', line 37

def touch_f(*args) paths, o = args.extract_options; o[:force]=true; 	_touch(*paths, o) end