From b6ed2b69954f21a4f1f69f536d74a48cde8cad5c Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Fri, 15 Apr 2016 08:45:26 +0900 Subject: [PATCH 1/3] use tmpdir and ensure removing for pdfmaker. --- lib/review/pdfmaker.rb | 137 +++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index e68930490..2368355d8 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -52,7 +52,7 @@ def remove_old_file end def build_path - "./#{@config["bookname"]}-pdf" + Dir.mktmpdir("#{@config["bookname"]}-pdf-", Dir.pwd) end def check_compile_status(ignore_errors) @@ -111,88 +111,89 @@ def execute(*args) def generate_pdf(yamlfile) remove_old_file @path = build_path() - Dir.mkdir(@path) - - @chaps_fnames = Hash.new{|h, key| h[key] = ""} - @compile_errors = nil - - book = ReVIEW::Book.load(@basedir) - book.config = @config - @converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new) - book.parts.each do |part| - if part.name.present? - if part.file? - output_chaps(part.name, yamlfile) - @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| - else - @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| + begin + @chaps_fnames = Hash.new{|h, key| h[key] = ""} + @compile_errors = nil + + book = ReVIEW::Book.load(@basedir) + book.config = @config + @converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new) + book.parts.each do |part| + if part.name.present? + if part.file? + output_chaps(part.name, yamlfile) + @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| + else + @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| + end end - end - part.chapters.each do |chap| - filename = File.basename(chap.path, ".*") - output_chaps(filename, yamlfile) - @chaps_fnames["PREDEF"] << "\\input{#{filename}.tex}\n" if chap.on_PREDEF? - @chaps_fnames["CHAPS"] << "\\input{#{filename}.tex}\n" if chap.on_CHAPS? - @chaps_fnames["APPENDIX"] << "\\input{#{filename}.tex}\n" if chap.on_APPENDIX? - @chaps_fnames["POSTDEF"] << "\\input{#{filename}.tex}\n" if chap.on_POSTDEF? + part.chapters.each do |chap| + filename = File.basename(chap.path, ".*") + output_chaps(filename, yamlfile) + @chaps_fnames["PREDEF"] << "\\input{#{filename}.tex}\n" if chap.on_PREDEF? + @chaps_fnames["CHAPS"] << "\\input{#{filename}.tex}\n" if chap.on_CHAPS? + @chaps_fnames["APPENDIX"] << "\\input{#{filename}.tex}\n" if chap.on_APPENDIX? + @chaps_fnames["POSTDEF"] << "\\input{#{filename}.tex}\n" if chap.on_POSTDEF? + end end - end - check_compile_status(@config["ignore-errors"]) + check_compile_status(@config["ignore-errors"]) - @config["pre_str"] = @chaps_fnames["PREDEF"] - @config["chap_str"] = @chaps_fnames["CHAPS"] - @config["appendix_str"] = @chaps_fnames["APPENDIX"] - @config["post_str"] = @chaps_fnames["POSTDEF"] + @config["pre_str"] = @chaps_fnames["PREDEF"] + @config["chap_str"] = @chaps_fnames["CHAPS"] + @config["appendix_str"] = @chaps_fnames["APPENDIX"] + @config["post_str"] = @chaps_fnames["POSTDEF"] - @config["usepackage"] = "" - if @config["texstyle"] - @config["usepackage"] = "\\usepackage{#{@config['texstyle']}}" - end + @config["usepackage"] = "" + if @config["texstyle"] + @config["usepackage"] = "\\usepackage{#{@config['texstyle']}}" + end - copy_images("./images", File.join(@path, "images")) - copyStyToDir(File.join(Dir.pwd, "sty"), @path) - copyStyToDir(File.join(Dir.pwd, "sty"), @path, "fd") - copyStyToDir(File.join(Dir.pwd, "sty"), @path, "cls") - copyStyToDir(Dir.pwd, @path, "tex") + copy_images("./images", File.join(@path, "images")) + copyStyToDir(File.join(Dir.pwd, "sty"), @path) + copyStyToDir(File.join(Dir.pwd, "sty"), @path, "fd") + copyStyToDir(File.join(Dir.pwd, "sty"), @path, "cls") + copyStyToDir(Dir.pwd, @path, "tex") - template = get_template - Dir.chdir(@path) do - File.open("./book.tex", "wb"){|f| f.write(template)} + template = get_template + Dir.chdir(@path) do + File.open("./book.tex", "wb"){|f| f.write(template)} - call_hook("hook_beforetexcompile") + call_hook("hook_beforetexcompile") - ## do compile - kanji = 'utf8' - texcommand = "platex" - texoptions = "-kanji=#{kanji}" - dvicommand = "dvipdfmx" - dvioptions = "-d 5" + ## do compile + kanji = 'utf8' + texcommand = "platex" + texoptions = "-kanji=#{kanji}" + dvicommand = "dvipdfmx" + dvioptions = "-d 5" - if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 - warn "command configuration is prohibited in safe mode. ignored." - else - texcommand = @config["texcommand"] if @config["texcommand"] - dvicommand = @config["dvicommand"] if @config["dvicommand"] - dvioptions = @config["dvioptions"] if @config["dvioptions"] - texoptions = @config["texoptions"] if @config["texoptions"] - end - 3.times do - system_or_raise("#{texcommand} #{texoptions} book.tex") - end - call_hook("hook_aftertexcompile") + if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 + warn "command configuration is prohibited in safe mode. ignored." + else + texcommand = @config["texcommand"] if @config["texcommand"] + dvicommand = @config["dvicommand"] if @config["dvicommand"] + dvioptions = @config["dvioptions"] if @config["dvioptions"] + texoptions = @config["texoptions"] if @config["texoptions"] + end + 3.times do + system_or_raise("#{texcommand} #{texoptions} book.tex") + end + call_hook("hook_aftertexcompile") - if File.exist?("book.dvi") - system_or_raise("#{dvicommand} #{dvioptions} book.dvi") + if File.exist?("book.dvi") + system_or_raise("#{dvicommand} #{dvioptions} book.dvi") + end end - end - call_hook("hook_afterdvipdf") + call_hook("hook_afterdvipdf") - FileUtils.cp(File.join(@path, "book.pdf"), pdf_filepath) + FileUtils.cp(File.join(@path, "book.pdf"), pdf_filepath) - unless @config["debug"] - remove_entry_secure @path + ensure + unless @config["debug"] + remove_entry_secure @path + end end end From 324e5b13f0a18d9e8e4b834e45f110971fb81393 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 15 Apr 2016 20:03:10 +0900 Subject: [PATCH 2/3] fix build_path in debug mode --- lib/review/pdfmaker.rb | 11 ++++++++++- test/test_pdfmaker.rb | 10 ++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index 2368355d8..f16ef2be1 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -52,7 +52,16 @@ def remove_old_file end def build_path - Dir.mktmpdir("#{@config["bookname"]}-pdf-", Dir.pwd) + if @config["debug"] + path = "#{@config["bookname"]}-pdf" + if File.exist?(path) + FileUtils.rm_rf(path, secure: true) + end + Dir.mkdir(path) + return path + else + return Dir.mktmpdir("#{@config["bookname"]}-pdf-") + end end def check_compile_status(ignore_errors) diff --git a/test/test_pdfmaker.rb b/test/test_pdfmaker.rb index 164ce2f4e..80eabcc25 100644 --- a/test/test_pdfmaker.rb +++ b/test/test_pdfmaker.rb @@ -43,8 +43,14 @@ def test_check_book_none end end - def test_buildpath - assert_equal(@maker.build_path, "./sample-pdf") + def test_buildpath_debug + @maker.config["debug"] = true + path = @maker.build_path + begin + assert_equal(path, "sample-pdf") + ensure + FileUtils.remove_entry_secure path + end end def test_parse_opts_help From b0b429c68337e0cac428026d650ca686e4f4ca71 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 15 Apr 2016 20:09:15 +0900 Subject: [PATCH 3/3] fix for rubocop --- lib/review/pdfmaker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index f16ef2be1..f235ace71 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -55,7 +55,7 @@ def build_path if @config["debug"] path = "#{@config["bookname"]}-pdf" if File.exist?(path) - FileUtils.rm_rf(path, secure: true) + FileUtils.rm_rf(path, :secure => true) end Dir.mkdir(path) return path