unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip McGrath <philip@philipmcgrath.com>
To: 56759@debbugs.gnu.org
Cc: Philip McGrath <philip@philipmcgrath.com>
Subject: [bug#56759] [PATCH 18/20] gnu: ruby-anystyle: Initialize dictionary files.
Date: Mon, 25 Jul 2022 08:16:33 -0400	[thread overview]
Message-ID: <04d6abe5dd2afb63a531c749104b7f6a84f21e38.1658750358.git.philip@philipmcgrath.com> (raw)
In-Reply-To: <cover.1658750358.git.philip@philipmcgrath.com>

For the GDBM and Marshal dictionary adapters to be useful with their
default settings, we must initialize their data files during the package
build: upstream would initialize them lazily, but that doesn't work with
an immutable installation directory (at least, not without more complex
patches). Otherwise, we would always end up rebuilding the dictionary at
startup, which is “slow” and “not recommended”.

* gnu/packages/patches/ruby-anystyle-fix-dictionary-populate.patch: New
patch.
* gnu/local.mk (dist_patch_DATA): Add it.
* gnu/packages/ruby.scm (ruby-anystyle)[patches]: Use it.
[arguments]<#:phases>: Add 'populate-dictionaries' phase.
---
 gnu/local.mk                                  |  1 +
 ...uby-anystyle-fix-dictionary-populate.patch | 94 +++++++++++++++++++
 gnu/packages/ruby.scm                         | 31 +++++-
 3 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/ruby-anystyle-fix-dictionary-populate.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 54ac9f2f9f..cb7bd1dc23 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1775,6 +1775,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/rocm-opencl-runtime-4.3-noclinfo.patch \
   %D%/packages/patches/rocm-opencl-runtime-4.3-noopencl.patch \
   %D%/packages/patches/ruby-anystyle-data-immutable-install.patch	\
+  %D%/packages/patches/ruby-anystyle-fix-dictionary-populate.patch	\
   %D%/packages/patches/ruby-latex-decode-fix-test.patch		\
   %D%/packages/patches/ruby-mustache-1.1.1-fix-race-condition-tests.patch \
   %D%/packages/patches/ruby-sanitize-system-libxml.patch	\
diff --git a/gnu/packages/patches/ruby-anystyle-fix-dictionary-populate.patch b/gnu/packages/patches/ruby-anystyle-fix-dictionary-populate.patch
new file mode 100644
index 0000000000..b2e0498e8d
--- /dev/null
+++ b/gnu/packages/patches/ruby-anystyle-fix-dictionary-populate.patch
@@ -0,0 +1,94 @@
+From fae622c8b77feebac66a538d76e4211de8bd8eb3 Mon Sep 17 00:00:00 2001
+From: Philip McGrath <philip@philipmcgrath.com>
+Date: Sun, 24 Jul 2022 21:50:44 -0400
+Subject: [PATCH] fix saving `AnyStyle::Dictionary` after `populate!`
+
+Some of these fixes are more generally applicable.
+
+A more robust solution might find data files using
+e.g. `Gem.find_files()`.
+---
+ lib/anystyle/dictionary/gdbm.rb    |  6 ++++++
+ lib/anystyle/dictionary/marshal.rb | 31 ++++++++++++++++++++++++------
+ 2 files changed, 31 insertions(+), 6 deletions(-)
+
+diff --git a/lib/anystyle/dictionary/gdbm.rb b/lib/anystyle/dictionary/gdbm.rb
+index 754903c..c814df2 100644
+--- a/lib/anystyle/dictionary/gdbm.rb
++++ b/lib/anystyle/dictionary/gdbm.rb
+@@ -1,5 +1,6 @@
+ module AnyStyle
+   require 'gdbm'
++  require 'fileutils'
+ 
+   class Dictionary
+     class GDBM < Dictionary
+@@ -17,8 +18,13 @@ module AnyStyle
+ 
+       def open
+         close
++        FileUtils.mkdir_p(File.dirname(options[:path]))
+         @db = ::GDBM.new(*options.values_at(:path, :mode, :flags))
+         self
++      rescue Errno::EACCES
++        # GDBM.new tries this if :flags is nil, but not necessarily otherwise
++        @db = ::GDBM.new(options[:path],options[:mode],::GDBM::READER)
++        self
+       ensure
+         populate! if empty?
+       end
+diff --git a/lib/anystyle/dictionary/marshal.rb b/lib/anystyle/dictionary/marshal.rb
+index 761ca36..b9529d0 100644
+--- a/lib/anystyle/dictionary/marshal.rb
++++ b/lib/anystyle/dictionary/marshal.rb
+@@ -1,4 +1,6 @@
+ module AnyStyle
++  require 'fileutils'
++  require 'tempfile'
+   class Dictionary
+     class Marshal < Dictionary
+       @defaults = {
+@@ -10,17 +12,34 @@ module AnyStyle
+       end
+ 
+       def open
+-        if File.exists?(options[:path])
+-          @db = ::Marshal.load(File.open(options[:path]))
+-        else
+-          @db = {}
++        File.open(options[:path]) do |file|
++          @db = ::Marshal.load(file)
+         end
+         self
++      rescue Errno::ENOENT
++        @db = {}
++        self
+       ensure
+         if empty?
+           populate!
+-          if File.writable?(options[:path])
+-            ::Marshal.dump(db, File.open(options[:path], 'wb'))
++          tmp = nil
++          begin
++            FileUtils.mkdir_p(File.dirname(options[:path]))
++            tmp = Tempfile.create(File.basename(options[:path]),
++                                  File.dirname(options[:path]),
++                                  mode: File::Constants::BINARY)
++            pth = tmp.path()
++            ::Marshal.dump(db, tmp)
++            tmp.close()
++            File.rename(tmp.path, options[:path]) # will overwrite if exists
++            tmp = nil
++          rescue SystemCallError => e
++            warn(e.message)
++          ensure
++            if tmp then
++              tmp.close()
++              tmp.unlink()
++            end
+           end
+         end
+       end
+-- 
+2.32.0
+
diff --git a/gnu/packages/ruby.scm b/gnu/packages/ruby.scm
index 32ba09d283..608b1ebe89 100644
--- a/gnu/packages/ruby.scm
+++ b/gnu/packages/ruby.scm
@@ -13514,6 +13514,9 @@ (define-public ruby-anystyle
                      (substitute* "spec/anystyle/parser_spec.rb"
                        (("language: 'en'," orig)
                         (string-append "# " orig " # no lanugage_detector")))))
+                (patches
+                 (search-patches
+                  "ruby-anystyle-fix-dictionary-populate.patch"))
                 (file-name (git-file-name name version))))
       (build-system ruby-build-system)
       (propagated-inputs
@@ -13543,7 +13546,33 @@ (define-public ruby-anystyle
               (lambda args
                 (substitute* "anystyle.gemspec"
                   (("`git ls-files spec`")
-                   "`find spec -type f | sort`")))))))
+                   "`find spec -type f | sort`"))))
+            (add-after 'wrap 'populate-dictionaries
+              (lambda args
+                ;; We must initiallize these files here, or they will never be
+                ;; usable with the default settings. A more flexible approach
+                ;; might use something like `Gem.find_files()` or
+                ;; XDG_DATA_DIRS.
+                (with-output-to-file "initialize-dictionaries.rb"
+                  (lambda ()
+                    (display "
+require 'anystyle/dictionary' # must come before 'anystyle/data'
+require 'anystyle/data'
+[:marshal, :gdbm].each do |adapter|
+  AnyStyle::Dictionary.create({adapter: adapter}).open().close()
+end
+")))
+                (let* ((old-gems (getenv "GEM_PATH"))
+                       (new-gems (string-append #$output
+                                                "/lib/ruby/vendor_ruby:"
+                                                old-gems)))
+                  (dynamic-wind
+                    (lambda ()
+                      (setenv "GEM_PATH" new-gems))
+                    (lambda ()
+                      (invoke "ruby" "initialize-dictionaries.rb"))
+                    (lambda ()
+                      (setenv "GEM_PATH" old-gems)))))))))
       (home-page "https://anystyle.io")
       (synopsis "Fast and smart citation reference parsing (Ruby library)")
       (description
-- 
2.32.0





  parent reply	other threads:[~2022-07-25 12:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-25 12:13 [bug#56759] [PATCH 00/20] gnu: Add AnyStyle Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 01/20] gnu: Add ruby-wapiti Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 02/20] gnu: Add ruby-namae Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 03/20] gnu: Add itex2mml Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 04/20] gnu: Add ruby-ritex Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 05/20] gnu: Add ruby-latex-decode Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 06/20] gnu: Add ruby-link-header Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 07/20] gnu: Add ruby-rdf Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 08/20] gnu: Add ruby-rdf-vocab Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 09/20] gnu: Add ruby-bibtex-ruby Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 10/20] gnu: Add ruby-unicode-scripts Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 11/20] gnu: Add ruby-citeproc Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 12/20] gnu: Add ruby-edtf Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 13/20] gnu: Add ruby-gli Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 14/20] gnu: Add ruby-anystyle-data Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 15/20] gnu: Add ruby-anystyle Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 16/20] gnu: Add anystyle Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 17/20] gnu: ruby-anystyle-data: Don't write to installed gem Philip McGrath
2022-07-25 12:16 ` Philip McGrath [this message]
2022-07-25 12:16 ` [bug#56759] [PATCH 19/20] gnu: anystyle: Add tests for dictionary adapters Philip McGrath
2022-07-25 12:16 ` [bug#56759] [PATCH 20/20] gnu: anystyle: Use GDBM by default Philip McGrath
2022-07-25 15:10 ` [bug#56759] [PATCH v2 05/20] gnu: Add ruby-latex-decode Philip McGrath
2022-08-04  9:57 ` bug#56759: [PATCH 00/20] gnu: Add AnyStyle Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=04d6abe5dd2afb63a531c749104b7f6a84f21e38.1658750358.git.philip@philipmcgrath.com \
    --to=philip@philipmcgrath.com \
    --cc=56759@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).