Class: Pathname
- Inherits:
-
Object
- Object
- Pathname
- Defined in:
- lib/downlow/ext/pathname.rb
Overview
we enhance pathname to make our code more readable
Instance Method Summary collapse
- #abv ⇒ Object
- #chmod_R(perms) ⇒ Object
- #cp(dst) ⇒ Object
-
#extname ⇒ Object
extended to support the double extensions .tar.gz and .tar.bz2.
- #install(src) ⇒ Object
- #md5 ⇒ Object
-
#rmdir_if_possible ⇒ Object
I don’t trust the children.length == 0 check particularly, not to mention it is slow to enumerate the whole directory just to see if it is empty, instead rely on good ol’ libc and the filesystem.
-
#stem ⇒ Object
for filetypes we support, basename without extension.
-
#version ⇒ Object
attempts to retrieve the version component of this path, so generally you’ll call it on tarballs or extracted tarball directories, if you add to this please provide amend the unittest.
-
#write(content) ⇒ Object
we assume this pathname object is a file obviously.
Instance Method Details
#abv ⇒ Object
92 93 94 95 96 97 |
# File 'lib/downlow/ext/pathname.rb', line 92 def abv out='' n=`find #{to_s} -type f | wc -l`.to_i out<<"#{n} files, " if n > 1 out<<`/usr/bin/du -hd0 #{to_s} | cut -d"\t" -f1`.strip end |
#chmod_R(perms) ⇒ Object
87 88 89 90 |
# File 'lib/downlow/ext/pathname.rb', line 87 def chmod_R perms require 'fileutils' FileUtils.chmod_R perms, to_s end |
#cp(dst) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/downlow/ext/pathname.rb', line 55 def cp dst if file? FileUtils.cp to_s, dst else FileUtils.cp_r to_s, dst end return dst end |
#extname ⇒ Object
extended to support the double extensions .tar.gz and .tar.bz2
65 66 67 68 69 |
# File 'lib/downlow/ext/pathname.rb', line 65 def extname /(\.tar\.(gz|bz2))$/.match to_s return $1 if $1 return File.extname(to_s) end |
#install(src) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/downlow/ext/pathname.rb', line 28 def install src if src.is_a? Array src.collect {|src| install src } else raise "#{src} does not exist" unless File.exist? src mkpath if File.symlink? src # we use the BSD mv command because FileUtils copies the target and # not the link! I'm beginning to wish I'd used Python quite honestly! raise unless Kernel.system 'mv', src, to_s and $? == 0 else # we mv when possible as it is faster and you should only be using # this function when installing from the temporary build directory FileUtils.mv src, to_s end src=Pathname.new src return self+src.basename end end |
#md5 ⇒ Object
157 158 159 160 |
# File 'lib/downlow/ext/pathname.rb', line 157 def md5 require 'digest' Digest::MD5.hexdigest(File.read(self)) end |
#rmdir_if_possible ⇒ Object
I don’t trust the children.length == 0 check particularly, not to mention it is slow to enumerate the whole directory just to see if it is empty, instead rely on good ol’ libc and the filesystem
79 80 81 82 83 84 85 |
# File 'lib/downlow/ext/pathname.rb', line 79 def rmdir_if_possible rmdir true rescue SystemCallError => e raise unless e.errno == Errno::ENOTEMPTY::Errno or e.errno == Errno::EACCES::Errno false end |
#stem ⇒ Object
for filetypes we support, basename without extension
72 73 74 |
# File 'lib/downlow/ext/pathname.rb', line 72 def stem return File.basename(to_s, extname) end |
#version ⇒ Object
attempts to retrieve the version component of this path, so generally you’ll call it on tarballs or extracted tarball directories, if you add to this please provide amend the unittest
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/downlow/ext/pathname.rb', line 102 def version if directory? # directories don't have extnames stem=basename.to_s else stem=self.stem end # github tarballs are special # we only support numbered tagged downloads %r[github.com/.*/tarball/((\d\.)+\d)$].match to_s return $1 if $1 # eg. boost_1_39_0 /((\d+_)+\d+)$/.match stem return $1.gsub('_', '.') if $1 # eg. foobar-4.5.1-1 # eg. ruby-1.9.1-p243 /-((\d+\.)*\d\.\d+-(p|rc)?\d+)$/.match stem return $1 if $1 # eg. lame-398-1 /-((\d)+-\d)/.match stem return $1 if $1 # eg. foobar-4.5.1 /-((\d+\.)*\d+)$/.match stem return $1 if $1 # eg. foobar-4.5.1b /-((\d+\.)*\d+([abc]|rc\d))$/.match stem return $1 if $1 # eg foobar-4.5.0-beta1 /-((\d+\.)*\d+-beta\d+)$/.match stem return $1 if $1 # eg. foobar4.5.1 /((\d+\.)*\d+)$/.match stem return $1 if $1 # eg foobar-4.5.0-bin /-((\d+\.)+\d+[abc]?)[-.](bin|src|sources?)$/.match stem return $1 if $1 # eg. otp_src_R13B (this is erlang's style) # eg. astyle_1.23_macosx.tar.gz stem.scan /_([^_]+)/ do |match| return match.first if /\d/.match $1 end nil end |
#write(content) ⇒ Object
we assume this pathname object is a file obviously
49 50 51 52 53 |
# File 'lib/downlow/ext/pathname.rb', line 49 def write content raise "Will not overwrite #{to_s}" if exist? and not ARGV.force? dirname.mkpath File.open(self, 'w') {|f| f.write content } end |