PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/context/ruby/base/tool.rb

https://gitlab.com/brcha/context-serbian
Ruby | 291 lines | 202 code | 48 blank | 41 comment | 15 complexity | 29e1c8a9368389f7ef8b354138cba79e MD5 | raw file
  1. # module : base/tool
  2. # copyright : PRAGMA Advanced Document Engineering
  3. # version : 2002-2005
  4. # author : Hans Hagen
  5. #
  6. # project : ConTeXt / eXaMpLe
  7. # concept : Hans Hagen
  8. # info : j.hagen@xs4all.nl
  9. # www : www.pragma-ade.com
  10. require 'timeout'
  11. require 'socket'
  12. require 'rbconfig'
  13. module Tool
  14. $constructedtempdir = ''
  15. def Tool.constructtempdir(create,mainpath='',fallback='')
  16. begin
  17. mainpath += '/' unless mainpath.empty?
  18. timeout(5) do
  19. begin
  20. t = Time.now
  21. u = t.usec.to_s % [1..2] [0..3]
  22. pth = t.strftime("#{mainpath}%Y%m%d-%H%M%S-#{u}-#{Process.pid}")
  23. #
  24. # problems with 1.9
  25. #
  26. # if pth == $constructedtempdir
  27. # # sleep(0.01)
  28. # retry
  29. # end
  30. pth == $constructedtempdir
  31. #
  32. Dir.mkdir(pth) if create
  33. $constructedtempdir = pth
  34. return pth
  35. rescue
  36. # sleep(0.01)
  37. retry
  38. end
  39. end
  40. rescue TimeoutError
  41. # ok
  42. rescue
  43. # ok
  44. end
  45. unless fallback.empty?
  46. begin
  47. pth = "#{mainpath}#{fallback}"
  48. mkdir(pth) if create
  49. $constructedtempdir = path
  50. return pth
  51. rescue
  52. return '.'
  53. end
  54. else
  55. return '.'
  56. end
  57. end
  58. def Tool.findtempdir(*vars)
  59. constructtempdir(false,*vars)
  60. end
  61. def Tool.maketempdir(*vars)
  62. constructtempdir(true,*vars)
  63. end
  64. # print maketempdir + "\n"
  65. # print maketempdir + "\n"
  66. # print maketempdir + "\n"
  67. # print maketempdir + "\n"
  68. # print maketempdir + "\n"
  69. def Tool.ruby_platform
  70. case RUBY_PLATFORM
  71. when /(mswin|bccwin|mingw|cygwin)/i then 'mswin'
  72. when /(linux)/i then 'linux'
  73. when /(netbsd|unix)/i then 'unix'
  74. when /(darwin|rhapsody|nextstep)/i then 'macosx'
  75. else 'unix'
  76. end
  77. end
  78. $defaultlineseparator = $/ # $RS in require 'English'
  79. def Tool.file_platform(filename)
  80. begin
  81. if f = open(filename,'rb') then
  82. str = f.read(4000)
  83. str.gsub!(/(.*?)\%\!PS/mo, "%!PS") # don't look into preamble crap
  84. f.close
  85. nn = str.count("\n")
  86. nr = str.count("\r")
  87. if nn>nr then
  88. return 2
  89. elsif nn<nr then
  90. return 3
  91. else
  92. return 1
  93. end
  94. else
  95. return 0
  96. end
  97. rescue
  98. return 0
  99. end
  100. end
  101. def Tool.path_separator
  102. return File::PATH_SEPARATOR
  103. end
  104. def Tool.line_separator(filename)
  105. case file_platform(filename)
  106. when 1 then return $defaultlineseparator
  107. when 2 then return "\n"
  108. when 3 then return "\r"
  109. else return $defaultlineseparator
  110. end
  111. end
  112. def Tool.default_line_separator
  113. $defaultlineseparator
  114. end
  115. def Tool.simplefilename(old)
  116. return old # too fragile
  117. return old if not FileTest.file?(old)
  118. new = old.downcase
  119. new.gsub!(/[^A-Za-z0-9\_\-\.\\\/]/o) do # funny chars
  120. '-'
  121. end
  122. if old =~ /[a-zA-Z]\:/o
  123. # seems like we have a dos/windows drive prefix, so roll back
  124. new.sub!(/^(.)\-/) do
  125. $1 + ':'
  126. end
  127. end
  128. # fragile for a.b.c.d.bla-bla.e.eps
  129. # new.gsub!(/(.+?)\.(.+?)(\..+)$/o) do # duplicate .
  130. # $1 + '-' + $2 + $3
  131. # end
  132. new.gsub!(/\-+/o) do # duplicate -
  133. '-'
  134. end
  135. new
  136. end
  137. if RbConfig::CONFIG['host_os'] =~ /mswin/ then
  138. require 'Win32API'
  139. GetShortPathName = Win32API.new('kernel32', 'GetShortPathName', ['P','P','N'], 'N')
  140. GetLongPathName = Win32API.new('kernel32', 'GetLongPathName', ['P','P','N'], 'N')
  141. def Tool.dowith_pathname (filename,filemethod)
  142. filename.gsub!(/\\/o,'/')
  143. case filename
  144. when /\;/o then
  145. # could be a path spec
  146. return filename
  147. when /\s+/o then
  148. # danger lurking
  149. buffer = ' ' * 260
  150. length = filemethod.call(filename,buffer,buffer.size)
  151. if length>0 then
  152. return buffer.slice(0..length-1)
  153. else
  154. # when the path or file does not exist, nothing is returned
  155. # so we try to handle the path separately from the basename
  156. basename = File.basename(filename)
  157. pathname = File.dirname(filename)
  158. length = filemethod.call(pathname,buffer,260)
  159. if length>0 then
  160. return buffer.slice(0..length-1) + '/' + basename
  161. else
  162. return filename
  163. end
  164. end
  165. else
  166. # no danger
  167. return filename
  168. end
  169. end
  170. def Tool.shortpathname(filename)
  171. dowith_pathname(filename,GetShortPathName)
  172. end
  173. def Tool.longpathname(filename)
  174. dowith_pathname(filename,GetLongPathName)
  175. end
  176. else
  177. def Tool.shortpathname(filename)
  178. filename
  179. end
  180. def Tool.longpathname(filename)
  181. filename
  182. end
  183. end
  184. # print shortpathname("C:/Program Files/ABBYY FineReader 6.0/matrix.str")+ "!\n"
  185. # print shortpathname("C:/Program Files/ABBYY FineReader 6.0/matrix.strx")+ "!\n"
  186. def Tool.checksuffix(old)
  187. return old unless FileTest.file?(old)
  188. new = old
  189. unless new =~ /\./io # no suffix
  190. f = open(filename,'rb')
  191. if str = f.gets
  192. case str
  193. when /^\%\!PS/io
  194. # logging.report(filename, 'analyzed as EPS')
  195. new = new + '.eps'
  196. when /^\%PDF/io
  197. # logging.report(filename, 'analyzed as PDF')
  198. new = new + '.pdf'
  199. else
  200. # logging.report(filename, 'fallback as TIF')
  201. new = new + '.tif'
  202. end
  203. end
  204. f.close
  205. end
  206. new.sub!(/\.jpeg$/io) do
  207. '.jpg'
  208. end
  209. new.sub!(/\.tiff$/io) do
  210. '.tif'
  211. end
  212. new.sub!(/\.ai$/io) do
  213. '.eps'
  214. end
  215. new.sub!(/\.ai([a-z0-9]*)$/io) do
  216. '-' + $1 + '.eps'
  217. end
  218. new
  219. end
  220. def Tool.cleanfilename(old,logging=nil)
  221. return old if not FileTest.file?(old)
  222. new = checksuffix(simplefilename(old))
  223. unless new == old
  224. begin # bugged, should only be name, not path
  225. File.rename(old,new)
  226. logging.report("renaming fuzzy name #{old} to #{new}") unless logging
  227. return old
  228. rescue
  229. logging.report("unable to rename fuzzy name #{old} to #{new}") unless logging
  230. end
  231. end
  232. return new
  233. end
  234. def Tool.servername
  235. host = Socket::gethostname
  236. begin
  237. Socket::gethostbyname(host)[0]
  238. rescue
  239. host
  240. end
  241. end
  242. # print file_platform(ARGV[0])
  243. end