From patchwork Mon Jul 25 12:16:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Philip McGrath X-Patchwork-Id: 40940 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DFFC527BBEA; Mon, 25 Jul 2022 13:20:47 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 365C227BBE9 for ; Mon, 25 Jul 2022 13:20:47 +0100 (BST) Received: from localhost ([::1]:51148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oFx4o-0001i5-Bg for patchwork@mira.cbaines.net; Mon, 25 Jul 2022 08:20:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47788) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oFx3B-0008OP-I7 for guix-patches@gnu.org; Mon, 25 Jul 2022 08:19:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:59711) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oFx3B-0005PE-5P for guix-patches@gnu.org; Mon, 25 Jul 2022 08:19:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oFx3B-0002yX-1j for guix-patches@gnu.org; Mon, 25 Jul 2022 08:19:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56759] [PATCH 18/20] gnu: ruby-anystyle: Initialize dictionary files. Resent-From: Philip McGrath Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 25 Jul 2022 12:19:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56759 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56759@debbugs.gnu.org Cc: Philip McGrath Received: via spool by 56759-submit@debbugs.gnu.org id=B56759.165875152111333 (code B ref 56759); Mon, 25 Jul 2022 12:19:05 +0000 Received: (at 56759) by debbugs.gnu.org; 25 Jul 2022 12:18:41 +0000 Received: from localhost ([127.0.0.1]:49447 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oFx2l-0002wc-UA for submit@debbugs.gnu.org; Mon, 25 Jul 2022 08:18:40 -0400 Received: from mail-qt1-f180.google.com ([209.85.160.180]:42623) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oFx2i-0002vk-9N for 56759@debbugs.gnu.org; Mon, 25 Jul 2022 08:18:36 -0400 Received: by mail-qt1-f180.google.com with SMTP id w29so8017352qtv.9 for <56759@debbugs.gnu.org>; Mon, 25 Jul 2022 05:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=philipmcgrath.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wwoAN0yBLwkrM0spbKqXjmYgXpNGz9LvkzATCLS9rcY=; b=jQXbOLHJaE4kMPWuPDK7QcueQgwsP27762JlY4EOHHl4tLRop9+W6X82LeDPb4ibiQ KqB9LlT/JTjGGPM93W+ABFVF4+7aGRBPvfQ12iabfe2jSgshOO3vY5byN7ggVa2dCT8H yUub58kZfxaAh8za6RW1jbcCno5llLwxVEife7CoF406O9KoK4C8/Q+V4abWaAizfLhC KLhg/kd66ZfPy41uBi6M8dhUJbWypWfT+EmAGNxHBP2fIMs7SfB7LZP6UDZaTDX5JvRG 5VLDcGscl+Y/0vbaFNE3DD4UtWjDEK3QfQRlEI31x89Beozs2EOJ6//TS/zThgRejCRv 3dWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wwoAN0yBLwkrM0spbKqXjmYgXpNGz9LvkzATCLS9rcY=; b=b9utt0G7BJrbzdxvOWBzeR/dxieJlU0/uxQYqwv/9irji+A5VyCEoGv2cupZ7PbBms YKMXeLIlUR9LwT+CDh7E0QrDWV4KTTb8ziPDvlsAKYkNnyEyoSd0HgnpKPxvh3eDCPnX nL7CzUGV7iAwlDGTbF2NEefgYa0FrVIP7deMBZy3GT798zMvTJUqb33d6HU7Hol4Iun5 ckSetqYITiW3/Qr65hmQwhpc8E2nj5F5wafuZK64HYsqb5mvCPdoqDto9oiRPgUwqVMK xAzwYWj9AXTFcL8KIEif970PKt3IWRFcawr/G5WVEjntu25icnAnh/mKD8bG1GvLMKbu hBOQ== X-Gm-Message-State: AJIora+sExhPtm996fpV9sHsld3MhMjxaYhCFuqUolF3I1b8eJ81B8ZJ HZPb23cB6w3TCgojuf+JLz0BlPcVRymx/t9D X-Google-Smtp-Source: AGRyM1uz7u93e56VDoSu/AuYXDBwlQ5k0mY9+NYYXBBOSq7ZSXZNdj8EyZpyByxa5Va1QdbJ7NQB1w== X-Received: by 2002:a05:622a:1448:b0:31f:165b:f270 with SMTP id v8-20020a05622a144800b0031f165bf270mr10119728qtx.98.1658751510565; Mon, 25 Jul 2022 05:18:30 -0700 (PDT) Received: from localhost (c-73-125-98-51.hsd1.fl.comcast.net. [73.125.98.51]) by smtp.gmail.com with UTF8SMTPSA id w24-20020a05620a095800b006a6d74f8fc9sm8504219qkw.127.2022.07.25.05.18.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Jul 2022 05:18:30 -0700 (PDT) From: Philip McGrath Date: Mon, 25 Jul 2022 08:16:33 -0400 Message-Id: <04d6abe5dd2afb63a531c749104b7f6a84f21e38.1658750358.git.philip@philipmcgrath.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches 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 +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