ba3a7b26722b909f728fc9f69a9bd744a65c8989
[software/sisu] / lib / sisu / develop / 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/develop/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 po4a #po4a
138 program='po4a'
139 program_ref="\n\t\tpo4a"
140 if program_found?(program); true
141 else
142 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
143 warn("#{program} is not installed #{program_ref}")
144 false
145 end
146 end
147 def zip #zip
148 program='zip'
149 program_ref="\n\t\tused to in the making of number of file formats, odf, epub"
150 if program_found?(program); true
151 else
152 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
153 mark("*WARN* #{program} is not installed #{program_ref}")
154 false
155 end
156 end
157 def openssl #openssl for digests
158 program='openssl'
159 program_ref="\n\t\tused to generate requested source document identification digest"
160 if program_found?(program); true
161 else
162 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
163 warn("#{program} is not installed #{program_ref}")
164 false
165 end
166 end
167 def md5(filename) #md5 dgst
168 program='openssl'
169 program_ref="\n\t\tmd5 digest requested"
170 if program_found?(program)
171 pwd=Dir.pwd
172 Dir.chdir(File.dirname(filename))
173 dgst=%x{openssl dgst -md5 #{File.basename(filename)}}.strip #use file name without file path
174 Dir.chdir(pwd)
175 dgst.scan(/\S+/)
176 else
177 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
178 warn("#{program} is not installed #{program_ref}")
179 false
180 end
181 end
182 def sha256(filename) #sha dgst
183 program='openssl'
184 program_ref="\n\t\tsha digest requested"
185 if program_found?(program)
186 pwd=Dir.pwd
187 Dir.chdir(File.dirname(filename))
188 dgst=%x{openssl dgst -sha256 #{File.basename(filename)}}.strip #use file name without file path
189 Dir.chdir(pwd)
190 dgst.scan(/\S+/)
191 else
192 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
193 warn("#{program} is not installed #{program_ref}")
194 false
195 end
196 end
197 def sha512(filename) #sha dgst
198 program='openssl'
199 program_ref="\n\t\tsha digest requested"
200 if program_found?(program)
201 pwd=Dir.pwd
202 Dir.chdir(File.dirname(filename))
203 dgst=%x{openssl dgst -sha512 #{File.basename(filename)}}.strip #use file name without file path
204 Dir.chdir(pwd)
205 dgst.scan(/\S+/)
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 psql #psql
213 program='psql'
214 program_ref="\n\t\tpsql requested"
215 if program_found?(program); true
216 else
217 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
218 warn("#{program} is not installed #{program_ref}")
219 false
220 end
221 end
222 def create_pg_db(dbname_stub=nil) #createdb
223 unless dbname_stub
224 @pwd ||=Dir.pwd
225 m=/.+\/(?:src\/)?(\S+)/im # m=/.+?\/(?:src\/)?([^\/]+)$/im # m=/.+\/(\S+)/m
226 dbname_stub=@pwd[m,1]
227 end
228 program='createdb'
229 db_name="#{Db[:name_prefix]}#{dbname_stub}"
230 program_ref="\n\t\tcreatedb dbname #{db_name} #for postgresql database creation"
231 (program_found?(program)) \
232 ? system("#{program} #{dbname_name}")
233 : (STDERR.puts "\t*WARN* #{program} is not available #{program_ref}")
234 end
235 def relaxng(cmd='') #trang - convert between different schema languages for XML
236 program='trang'
237 program_ref="\n\t\tsee <http://www.thaiopensource.com/relaxng/trang.html>"
238 (program_found?(program)) \
239 ? system("#{program} #{@input} #{@output}")
240 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" if cmd =~/V/)
241 end
242 def qrencode #qrcode - for generating QR code
243 program='qrencode'
244 program_ref="\n\t\tsee <http://megaui.net/fukuchi/works/qrencode/index.en.html>"
245 found=(program_found?(program)) ? true : false
246 found \
247 ? (system(%{
248 echo "#{@input}" | #{program} -s 3 -o #{@output}
249 }))
250 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" if @cmd =~/V/)
251 #found
252 end
253 def imagemagick #imagemagick is a image manipulation program
254 program='identify'
255 #program_ref="\n\t\tsee <http://www.imagemagick.org/>"
256 found=(program_found?(program)) ? true : false
257 #STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" unless found
258 found
259 end
260 def graphicsmagick #graphicsmagick is a image manipulation program
261 program='gm'
262 #program_ref="\n\t\tsee <http://www.graphicsmagick.org/>"
263 found=(program_found?(program)) ? true : false
264 #STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}" unless found
265 found
266 end
267 def well_formed? #tidy - check for well formed xml xhtml etc.
268 program=@prog.tidy
269 program_ref="\n\t\tsee <http://tidy.sourceforge.net/>"
270 (program_found?(program)) \
271 ? system("#{@prog.tidy} -xml #{@input} > #{@output}")
272 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}")
273 end
274 def tex2pdf_engine
275 progs=['xetex','xelatex','pdflatex','pdfetex','pdftex']
276 @pdfetex_flag=false
277 @cmd ||=''
278 @texpdf=nil
279 progs.each do |program|
280 if program_found?(program)
281 @texpdf=program if program =~/xetex|xelatex|pdftex|pdflatex/
282 @pdfetex_flag=true
283 break
284 end
285 end
286 if @pdfetex_flag==false
287 @texpdf=progs.join(', ')
288 end
289 @texpdf
290 end
291 def latex2pdf(md,papersize='a4') #convert from latex to pdf
292 tell=if @cmd =~/[MV]/
293 ''
294 elsif @cmd =~/[v]/
295 %q{2>&1 | grep -v ' WARNING '}
296 else %q{2>&1 | grep -v '$'}
297 end
298 mode='batchmode' #mode='nonstopmode'
299 texpdf=tex2pdf_engine
300 if @pdfetex_flag
301 texpdf_cmd=case texpdf
302 when /xetex/
303 %{#{texpdf} -interaction=#{mode} -fmt=xelatex -papersize="#{papersize}" #{@input} #{tell}\n}
304 when /xelatex/
305 %{#{texpdf} -interaction=#{mode} -papersize="#{papersize}" #{@input} #{tell}\n}
306 when /pdftex/
307 "#{texpdf} -interaction=#{mode} -fmt=pdflatex #{@input} #{tell}\n"
308 when /pdflatex/
309 "#{texpdf} -interaction=#{mode} #{@input} #{tell}\n"
310 end
311 system(texpdf_cmd)
312 else STDERR.puts "\t*WARN* none of the following programs are installed: #{@texpdf}"
313 end
314 end
315 def makeinfo #texinfo
316 program='makeinfo'
317 options='' #'--force' #''
318 program_ref="\n\t\tsee http://www.gnu.org/software/texinfo/"
319 (program_found?(program)) \
320 ? system("#{program} #{options} #{@input}\n")
321 : (STDERR.puts "\t*WARN* #{program} is not installed #{program_ref}")
322 end
323 def scp
324 puts "scp -Cr #{@input} #{@output}" if @cmd =~/[vVM]/
325 puts "scp disabled"
326 end
327 def rsync(action='',chdir=nil)
328 program='rsync'
329 if program_found?(program)
330 vb=if @cmd =~/q/; 'q'
331 elsif @cmd =~/v/; 'v'
332 else ''
333 end
334 cX=SiSU_Screen::Ansi.new(@cmd).cX
335 msg=(@cmd =~/q/) ? '' : %{ && echo " #{cX.grey}OK: #{@input} -> #{@output}#{cX.off}"}
336 amp=(@opt \
337 && @opt.files.length > 1) \
338 ? ''
339 : ((@cmd =~/[vVM]/) ? '' : '&')
340 rsync_cmd="rsync -az#{vb} #{action} #{@input} #{@output}"
341 puts rsync_cmd if @cmd =~/[vVM]/
342 dir_change=dir_return=nil
343 if not chdir.nil? \
344 && chdir != Dir.pwd
345 dir_change=Dir.chdir(chdir)
346 dir_return=Dir.pwd
347 end
348 dir_change if dir_change
349 system("
350 #{rsync_cmd} #{msg} #{amp}
351 ")
352 dir_return if dir_return
353 else STDERR.puts "\t*WARN* #{program} not found"
354 end
355 end
356 def rm
357 if @cmd =~/^-Z[mMvVq]*$/; FileUtils::rm_rf(@input)
358 elsif @cmd =~/V/; FileUtils::rm(@input)
359 elsif @cmd !~/q/; FileUtils::rm(@input)
360 elsif @cmd =~/q/; FileUtils::rm(@input)
361 else STDERR.puts "\t*WARN* operation ignored"
362 end
363 end
364 end
365 end
366 module SiSU_Info_Program
367 require_relative 'constants' # constants.rb
368 require_relative 'utils' # utils.rb
369 require_relative 'se_info_env' # se_info_env.rb
370 class InfoProgram < SiSU_Info_Env::InfoEnv # se_info_env.rb
371 attr_accessor :editor,:wc,:tidy,:rexml,:pdflatex,:postgresql,:sqlite
372 def initialize
373 prog=SiSU_Env::InfoEnv.new.program
374 @editor, @wc, @tidy, @rexml, @pdflatex, @postgresql, @sqlite=
375 prog.text_editor,prog.wc,prog.tidy,prog.rexml,prog.pdflatex,prog.postgresql,prog.sqlite
376 end
377 end
378 end
379 module SiSU_Info_Set
380 require_relative 'se_info_env' # se_info_env.rb
381 class InfoSettings < SiSU_Info_Env::InfoEnv # se_info_env.rb
382 def permission?(prog) # program defaults
383 (defined? @rc['permission_set'][prog]) \
384 ? @rc['permission_set'][prog]
385 : false
386 end
387 def program?(prog) # program defaults
388 (defined? @rc['program_set'][prog]) \
389 ? @rc['program_set'][prog]
390 : false
391 end
392 end
393 end
394 __END__