bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Collin Funk <collin.funk1@gmail.com>
To: bug-gnulib@gnu.org
Subject: gnulib-tool.py: Remove a redundant function.
Date: Sun, 14 Apr 2024 18:23:48 -0700	[thread overview]
Message-ID: <c7033250-db71-4b45-b9b8-6fac4075c511@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1154 bytes --]

The GLImport class has two functions that are the same,
GLImport.rewrite_old_files() and GLImport.rewrite_new_files().

The GLImport.rewrite_old_files() function does this extra step before
processing the list:

        files = [ '%s%s' % (file, os.path.sep)
                   for file in files ]

But before appending it to the resulting list os.path.normpath() is
used.

Since the following is true:

      os.path.normpath('abc') == 'abc'
      os.path.normpath('abc/') == 'abc'

both of these functions are the same.

Therefore, we can remove GLImport.rewrite_old_files() and rename
GLImport.rewrite_new_files() to GLImport.rewrite_files().

Also, I noticed we have:

         for src in old_files:
             dest = self.rewrite_files([src])[-1]
             old_table.append(tuple([dest, src]))

This is looping over a list, creating a new list with one item,
calling GLImport.rewrite_files(), which then calls sorted(set(...))
twice, and then appending the result to a list.

We should be able to create a new list from that function and zip()
the two together. I'll submit another patch for that since it requires
some sorting changes.

Collin

[-- Attachment #2: 0001-gnulib-tool.py-Remove-a-redundant-function.patch --]
[-- Type: text/x-patch, Size: 4193 bytes --]

From ec3d2f70a06e93b6cd730dd1f3629d5a6ad386fc Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.funk1@gmail.com>
Date: Sun, 14 Apr 2024 18:09:39 -0700
Subject: [PATCH] gnulib-tool.py: Remove a redundant function.

* pygnulib/GLImport.py (GLImport.rewrite_old_files): Remove function.
(GLImport.rewrite_new_files): Rename to rewrite_files.
(GLImport.prepare): Use rewrite_files instead of rewrite_old_files and
rewrite_new_files.
---
 ChangeLog            |  8 ++++++++
 pygnulib/GLImport.py | 44 +++-----------------------------------------
 2 files changed, 11 insertions(+), 41 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5b7b3a36fc..82c4a5f838 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-04-14  Collin Funk  <collin.funk1@gmail.com>
+
+	gnulib-tool.py: Remove a redundant function.
+	* pygnulib/GLImport.py (GLImport.rewrite_old_files): Remove function.
+	(GLImport.rewrite_new_files): Rename to rewrite_files.
+	(GLImport.prepare): Use rewrite_files instead of rewrite_old_files and
+	rewrite_new_files.
+
 2024-04-14  Collin Funk  <collin.funk1@gmail.com>
 
 	gnulib-tool.py: Fix incorrect type hint.
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index c6a4693c90..430691efbd 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -314,45 +314,7 @@ def __repr__(self) -> str:
         result = '<pygnulib.GLImport %s>' % hex(id(self))
         return result
 
-    def rewrite_old_files(self, files: list[str]) -> list[str]:
-        '''Replace auxdir, docbase, sourcebase, m4base and testsbase from default
-        to their version from cached config.'''
-        if type(files) is not list:
-            raise TypeError('files argument must has list type, not %s'
-                            % type(files).__name__)
-        for file in files:
-            if type(file) is not str:
-                raise TypeError('each file must be a string instance')
-        files = sorted(set(files))
-        files = [ '%s%s' % (file, os.path.sep)
-                  for file in files ]
-        auxdir = self.cache['auxdir']
-        docbase = self.cache['docbase']
-        sourcebase = self.cache['sourcebase']
-        m4base = self.cache['m4base']
-        testsbase = self.cache['testsbase']
-        result = []
-        for file in files:
-            if file.startswith('build-aux/'):
-                path = constants.substart('build-aux/', '%s/' % auxdir, file)
-            elif file.startswith('doc/'):
-                path = constants.substart('doc/', '%s/' % docbase, file)
-            elif file.startswith('lib/'):
-                path = constants.substart('lib/', '%s/' % sourcebase, file)
-            elif file.startswith('m4/'):
-                path = constants.substart('m4/', '%s/' % m4base, file)
-            elif file.startswith('tests/'):
-                path = constants.substart('tests/', '%s/' % testsbase, file)
-            elif file.startswith('tests=lib/'):
-                path = constants.substart('tests=lib/', '%s/' % testsbase, file)
-            elif file.startswith('top/'):
-                path = constants.substart('top/', '', file)
-            else:  # file is not a special file
-                path = file
-            result.append(os.path.normpath(path))
-        return sorted(set(result))
-
-    def rewrite_new_files(self, files: list[str]) -> list[str]:
+    def rewrite_files(self, files: list[str]) -> list[str]:
         '''Replace auxdir, docbase, sourcebase, m4base and testsbase from
         default to their version from config.'''
         if type(files) is not list:
@@ -959,10 +921,10 @@ def prepare(self) -> tuple[dict[str, list[str]], dict[str, str]]:
         old_table = []
         new_table = []
         for src in old_files:
-            dest = self.rewrite_old_files([src])[-1]
+            dest = self.rewrite_files([src])[-1]
             old_table.append(tuple([dest, src]))
         for src in new_files:
-            dest = self.rewrite_new_files([src])[-1]
+            dest = self.rewrite_files([src])[-1]
             new_table.append(tuple([dest, src]))
         old_table = sorted(set(old_table))
         new_table = sorted(set(new_table))
-- 
2.44.0


             reply	other threads:[~2024-04-15  1:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-15  1:23 Collin Funk [this message]
2024-04-15  2:20 ` gnulib-tool.py: Remove a redundant function Collin Funk
2024-04-15 14:58   ` Bruno Haible
2024-04-15 15:24     ` Collin Funk
2024-04-16 15:09       ` Bruno Haible
2024-04-16 16:23         ` Collin Funk
2024-04-17  1:12         ` Refactoring rewrite_filename functions Collin Funk
2024-04-17  1:35           ` Bruno Haible
2024-04-17  2:08             ` Collin Funk
2024-04-17 14:03               ` Bruno Haible
2024-04-15 11:47 ` gnulib-tool.py: Remove a redundant function Bruno Haible
2024-04-15 14:07   ` Collin Funk

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://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=c7033250-db71-4b45-b9b8-6fac4075c511@gmail.com \
    --to=collin.funk1@gmail.com \
    --cc=bug-gnulib@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.
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).