ee85725f97e9278fcee2f52453edb25452dfe8c8
[software/sisu] / lib / sisu / current / se_programs.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** system environment, resource control and configuration details
8
9 ** Author: Ralph Amissah
10 <ralph@amissah.com>
11 <ralph.amissah@gmail.com>
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see <http://www.gnu.org/licenses/>.
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 <http://www.fsf.org/licensing/licenses/gpl.html>
39 <http://www.gnu.org/licenses/gpl.html>
40
41 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/current/se_programs.rb;hb=HEAD>
55
56 =end
57 module SiSU_Sys_Call
58 begin
59 require 'singleton'
60 rescue LoadError
61 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
62 error('singleton NOT FOUND (LoadError)')
63 end
64 class SystemCall
65 @@locale_flag=false
66 def initialize(input='',output='',opt_or_cmd='')
67 @input,@output=input,output
68 (opt_or_cmd.is_a?(SiSU_Commandline::Options)) \
69 ? (@cmd,@opt=opt_or_cmd.cmd,opt_or_cmd)
70 : (@cmd,@opt=opt_or_cmd,nil) #cmd.is_a?(String)
71 @prog=SiSU_Env::InfoProgram.new
72 @sys=SiSU_Info_Sys::InfoSystem.instance
73 end
74 def program_found?(program)
75 found=`which #{program}` #`whereis #{program}`
76 (found =~/bin\/#{program}\b/) ? true : false
77 end
78 def locale #locales utf8 or other
79 unless @@locale_flag
80 @@locale_flag=true
81 end
82 @sys.locale
83 end
84 def file_encoding(filename,act='') #file encoding
85 program='file'
86 fnsp=SiSU_Env::InfoEnv.new(filename).source_file_with_path
87 if program_found?(program)
88 encoding=%x{file -L #{fnsp}}.strip
89 encoding=encoding.gsub(/#{fnsp}:(\s+|$)/,'')
90 encoding=if encoding \
91 and not encoding.empty?
92 encoding
93 else 'UTF-8 assumed, encoding undetermined'
94 end
95 if act[:verbose_plus][:set] ==:on \
96 or act[:maintenance][:set] ==:on
97 puts encoding
98 end
99 encoding
100 else
101 'UTF-8 assumed, file encoding check program unavailable'
102 end
103 end
104 def wc #word count
105 program='wc'
106 if program_found?(program) \
107 and locale !~/utf-?8/i
108 true
109 else
110 program_ref="(not available)" \
111 unless program_found?(program)
112 program_ref="(UTF-8)" \
113 if locale =~/utf-?8/i
114 false
115 end
116 end
117 def rcs #rcs for document markup data
118 program='rcs'
119 program_ref="\n\t\tdocument version information requested"
120 if program_found?(program); true
121 else
122 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
123 warn("#{program} is not installed #{program_ref}")
124 false
125 end
126 end
127 def cvs #cvs for document markup data
128 program='cvs'
129 program_ref="\n\t\tdocument version information requested"
130 if program_found?(program); true
131 else
132 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
133 warn("#{program} is not installed #{program_ref}")
134 false
135 end
136 end
137 def zip #zip
138 program='zip'
139 program_ref="\n\t\tused to in the making of number of file formats, odf, epub"
140 if program_found?(program); true
141 else
142 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
143 mark("*WARN* #{program} is not installed #{program_ref}")
144 false
145 end
146 end
147 def openssl #openssl for digests
148 program='openssl'
149 program_ref="\n\t\tused to generate requested source document identification digest"
150 if program_found?(program); true
151 else
152 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
153 warn("#{program} is not installed #{program_ref}")
154 false
155 end
156 end
157 def md5(filename) #md5 dgst
158 program='openssl'
159 program_ref="\n\t\tmd5 digest requested"
160 if program_found?(program)
161 pwd=Dir.pwd
162 Dir.chdir(File.dirname(filename))
163 dgst=%x{openssl dgst -md5 #{File.basename(filename)}}.strip #use file name without file path
164 Dir.chdir(pwd)
165 dgst.scan(/\S+/)
166 else
167 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
168 warn("#{program} is not installed #{program_ref}")
169 false
170 end
171 end
172 def sha256(filename) #sha dgst
173 program='openssl'
174 program_ref="\n\t\tsha digest requested"
175 if program_found?(program)
176 pwd=Dir.pwd
177 Dir.chdir(File.dirname(filename))
178 dgst=%x{openssl dgst -sha256 #{File.basename(filename)}}.strip #use file name without file path
179 Dir.chdir(pwd)
180 dgst.scan(/\S+/)
181 else
182 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
183 warn("#{program} is not installed #{program_ref}")
184 false
185 end
186 end
187 def sha512(filename) #sha dgst
188 program='openssl'
189 program_ref="\n\t\tsha digest requested"
190 if program_found?(program)
191 pwd=Dir.pwd
192 Dir.chdir(File.dirname(filename))
193 dgst=%x{openssl dgst -sha512 #{File.basename(filename)}}.strip #use file name without file path
194 Dir.chdir(pwd)
195 dgst.scan(/\S+/)
196 else
197 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
198 warn("#{program} is not installed #{program_ref}")
199 false
200 end
201 end
202 def psql #psql
203 program='psql'
204 program_ref="\n\t\tpsql requested"
205 if program_found?(program); true
206 else
207 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
208 warn("#{program} is not installed #{program_ref}")
209 false
210 end
211 end
212 def create_pg_db(dbname_stub=nil) #createdb
213 unless dbname_stub
214 @pwd ||=Dir.pwd
215 m=/.+\/(?:src\/)?(\S+)/im # m=/.+?\/(?:src\/)?([^\/]+)$/im # m=/.+\/(\S+)/m
216 dbname_stub=@pwd[m,1]
217 end
218 program='createdb'
219 db_name="#{Db[:name_prefix]}#{dbname_stub}"
220 program_ref="\n\t\tcreatedb dbname #{db_name} #for postgresql database creation"
221 (program_found?(program)) \
222 ? system("#{program} #{dbname_name}")
223 : (STDERR.puts "\t*WARN* #{program} is not available #{program_ref}")
224 end
225 def relaxng(cmd='') #trang - convert between different schema languages for XML
226 program='trang'
227 program_ref="\n\t\tsee <http://www.thaiopensource.com/relaxng/trang.html>"
228 (program_found?(program)) \
229 ? system("#{program} #{@input} #{@output}")
230 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" if cmd =~/V/)
231 end
232 def qrencode #qrcode - for generating QR code
233 program='qrencode'
234 program_ref="\n\t\tsee <http://megaui.net/fukuchi/works/qrencode/index.en.html>"
235 found=(program_found?(program)) ? true : false
236 found \
237 ? (system(%{
238 echo "#{@input}" | #{program} -s 3 -o #{@output}
239 }))
240 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" if @cmd =~/V/)
241 #found
242 end
243 def imagemagick #imagemagick is a image manipulation program
244 program='identify'
245 #program_ref="\n\t\tsee <http://www.imagemagick.org/>"
246 found=(program_found?(program)) ? true : false
247 #STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" unless found
248 found
249 end
250 def graphicsmagick #graphicsmagick is a image manipulation program
251 program='gm'
252 #program_ref="\n\t\tsee <http://www.graphicsmagick.org/>"
253 found=(program_found?(program)) ? true : false
254 #STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" unless found
255 found
256 end
257 def well_formed? #tidy - check for well formed xml xhtml etc.
258 program=@prog.tidy
259 program_ref="\n\t\tsee <http://tidy.sourceforge.net/>"
260 (program_found?(program)) \
261 ? system("#{@prog.tidy} -xml #{@input} > #{@output}")
262 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}")
263 end
264 def tex2pdf_engine
265 progs=['xetex','xelatex','pdflatex','pdfetex','pdftex']
266 @pdfetex_flag=false
267 @cmd ||=''
268 @texpdf=nil
269 progs.each do |program|
270 if program_found?(program)
271 @texpdf=program if program =~/xetex|xelatex|pdftex|pdflatex/
272 @pdfetex_flag=true
273 break
274 end
275 end
276 if @pdfetex_flag==false
277 @texpdf=progs.join(', ')
278 end
279 @texpdf
280 end
281 def latex2pdf(md,papersize='a4') #convert from latex to pdf
282 tell=if @cmd =~/[MV]/
283 ''
284 elsif @cmd =~/[v]/
285 %q{2>&1 | grep -v ' WARNING '}
286 else %q{2>&1 | grep -v '$'}
287 end
288 mode='batchmode' #mode='nonstopmode'
289 texpdf=tex2pdf_engine
290 if @pdfetex_flag
291 texpdf_cmd=case texpdf
292 when /xetex/
293 %{#{texpdf} -interaction=#{mode} -fmt=xelatex -papersize="#{papersize}" #{@input} #{tell}\n}
294 when /xelatex/
295 %{#{texpdf} -interaction=#{mode} -papersize="#{papersize}" #{@input} #{tell}\n}
296 when /pdftex/
297 "#{texpdf} -interaction=#{mode} -fmt=pdflatex #{@input} #{tell}\n"
298 when /pdflatex/
299 "#{texpdf} -interaction=#{mode} #{@input} #{tell}\n"
300 end
301 system(texpdf_cmd)
302 else STDERR.puts "\t*WARN* none of the following programs are installed: #{@texpdf}"
303 end
304 end
305 def makeinfo #texinfo
306 program='makeinfo'
307 options='' #'--force' #''
308 program_ref="\n\t\tsee http://www.gnu.org/software/texinfo/"
309 (program_found?(program)) \
310 ? system("#{program} #{options} #{@input}\n")
311 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}")
312 end
313 def scp
314 puts "scp -Cr #{@input} #{@output}" if @cmd =~/[vVM]/
315 puts "scp disabled"
316 end
317 def rsync(action='',chdir=nil)
318 program='rsync'
319 if program_found?(program)
320 vb=if @cmd =~/q/; 'q'
321 elsif @cmd =~/v/; 'v'
322 else ''
323 end
324 cX=SiSU_Screen::Ansi.new(@cmd).cX
325 msg=(@cmd =~/q/) ? '' : %{ && echo " #{cX.grey}OK: #{@input} -> #{@output}#{cX.off}"}
326 amp=(@opt \
327 && @opt.files.length > 1) \
328 ? ''
329 : ((@cmd =~/[vVM]/) ? '' : '&')
330 rsync_cmd="rsync -az#{vb} #{action} #{@input} #{@output}"
331 puts rsync_cmd if @cmd =~/[vVM]/
332 dir_change=dir_return=nil
333 if not chdir.nil? \
334 && chdir != Dir.pwd
335 dir_change=Dir.chdir(chdir)
336 dir_return=Dir.pwd
337 end
338 dir_change if dir_change
339 system("
340 #{rsync_cmd} #{msg} #{amp}
341 ")
342 dir_return if dir_return
343 else STDERR.puts "\t*WARN* #{program} not found"
344 end
345 end
346 def rm
347 if @cmd =~/^-Z[mMvVq]*$/; FileUtils::rm_rf(@input)
348 elsif @cmd =~/V/; FileUtils::rm(@input)
349 elsif @cmd !~/q/; FileUtils::rm(@input)
350 elsif @cmd =~/q/; FileUtils::rm(@input)
351 else STDERR.puts "\t*WARN* operation ignored"
352 end
353 end
354 end
355 end
356 module SiSU_Info_Program
357 require_relative 'constants' # constants.rb
358 require_relative 'utils' # utils.rb
359 require_relative 'se_info_env' # se_info_env.rb
360 class InfoProgram < SiSU_Info_Env::InfoEnv # se_info_env.rb
361 attr_accessor :editor,:wc,:tidy,:rexml,:pdflatex,:postgresql,:sqlite
362 def initialize
363 prog=SiSU_Env::InfoEnv.new.program
364 @editor, @wc, @tidy, @rexml, @pdflatex, @postgresql, @sqlite=
365 prog.text_editor,prog.wc,prog.tidy,prog.rexml,prog.pdflatex,prog.postgresql,prog.sqlite
366 end
367 end
368 end
369 module SiSU_Info_Set
370 require_relative 'se_info_env' # se_info_env.rb
371 class InfoSettings < SiSU_Info_Env::InfoEnv # se_info_env.rb
372 def permission?(prog) # program defaults
373 (defined? @rc['permission_set'][prog]) \
374 ? @rc['permission_set'][prog]
375 : false
376 end
377 def program?(prog) # program defaults
378 (defined? @rc['program_set'][prog]) \
379 ? @rc['program_set'][prog]
380 : false
381 end
382 end
383 end
384 __END__