Here is two small patches to fix some pylint warnings. The first is
'consider-using-with'. This recommends to use context managers that
handle cleanup for you. In this case two Popen calls, but here is an
example with open():
with open(file_name, 'r') as file:
data = file.read()
# File is closed for us.
is cleaner than:
file = open(file_name, 'r')
data = file.read()
file.close()
In the case of Popen, "Popen objects are supported as context managers
via the with statement: on exit, standard file descriptors are closed,
and the process is waited for [1]."
I've just changed these to 'sp.run()' since that function deals with
everything for us and is recommended.
I've left it wrapped in os.chdir() calls since I rather convert all
occurrences of those to sp.run(cwd=directory) in one patch. Things seem
less likely to break that way.
The second is 'consider-using-set-comprehension'. Instead of calling
set() on a list comprehension we can just use a set comprehension.
I've additionally simplified this case:
- version = sorted(set([ float(version)
- for version in versions ]))[-1]
+ version = max({ float(version)
+ for version in versions })
The previous method is just a less clear way of finding the max:
print([1, 2, 3, 4][-1])
4
[1] https://docs.python.org/3/library/subprocess.html#popen-constructor
Collin
From 7cd6b490d374077b67b5f82f7f1333b974ba5a30 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Thu, 4 Apr 2024 20:32:55 -0700
Subject: [PATCH 1/2] gnulib-tool.py: Fix 'consider-using-with' pylint
warnings.
* pygnulib/GLModuleSystem.py (GLModuleSystem.list): Use run() instead of
Popen() from the subprocess module. This function handles cleanup
internally instead of as a context manager via the 'with' statement.
---
ChangeLog | 7 +++++++
pygnulib/GLModuleSystem.py | 6 ++----
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 64f1af1000..0238480144 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-04-04 Collin Funk <collin.fu...@gmail.com>
+
+ gnulib-tool.py: Fix 'consider-using-with' pylint warnings.
+ * pygnulib/GLModuleSystem.py (GLModuleSystem.list): Use run() instead of
+ Popen() from the subprocess module. This function handles cleanup
+ internally instead of as a context manager via the 'with' statement.
+
2024-04-04 Collin Funk <collin.fu...@gmail.com>
posix-modules, all-modules: Fix --version output using git options.
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index 1f5d536fe3..98a21c4696 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -136,16 +136,14 @@ class GLModuleSystem:
# Read modules from gnulib root directory.
os.chdir(constants.DIRS['root'])
- find = sp.Popen(find_args, stdout=sp.PIPE)
- result += find.stdout.read().decode("UTF-8")
+ result += sp.run(find_args, text=True, capture_output=True, check=False).stdout
os.chdir(DIRS['cwd'])
# Read modules from local directories.
if len(localpath) > 0:
for localdir in localpath:
os.chdir(localdir)
- find = sp.Popen(find_args, stdout=sp.PIPE)
- result += find.stdout.read().decode("UTF-8")
+ result += sp.run(find_args, text=True, capture_output=True, check=False).stdout
os.chdir(DIRS['cwd'])
listing = [ line
--
2.44.0
From a35d8fe1d79742add3054eed9e434c4ec39b0cb1 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Thu, 4 Apr 2024 20:42:09 -0700
Subject: [PATCH 2/2] gnulib-tool.py: Fix 'consider-using-set-comprehension'
warnings.
* pygnulib/GLImport.py (GLImport.prepare): Create a set directly instead
of creating a list and passing it to a call of set().
(GLImport.__init__): Likewise. Use max() instead of getting the last
index of a sorted list.
---
ChangeLog | 8 ++++++++
pygnulib/GLImport.py | 8 ++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 0238480144..6e4be3e76c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-04-04 Collin Funk <collin.fu...@gmail.com>
+
+ gnulib-tool.py: Fix 'consider-using-set-comprehension' warnings.
+ * pygnulib/GLImport.py (GLImport.prepare): Create a set directly instead
+ of creating a list and passing it to a call of set().
+ (GLImport.__init__): Likewise. Use max() instead of getting the last
+ index of a sorted list.
+
2024-04-04 Collin Funk <collin.fu...@gmail.com>
gnulib-tool.py: Fix 'consider-using-with' pylint warnings.
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index c6d1a758a4..ee238a1615 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -107,8 +107,8 @@ class GLImport:
pattern = re.compile(r'.*AC_PREREQ\((.*)\)', re.M)
versions = cleaner(pattern.findall(data))
if versions:
- version = sorted(set([ float(version)
- for version in versions ]))[-1]
+ version = max({ float(version)
+ for version in versions })
self.config.setAutoconfVersion(version)
if version < 2.64:
raise GLError(4, version)
@@ -810,8 +810,8 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
m4base = self.config['m4base']
lgpl = self.config['lgpl']
verbose = self.config['verbosity']
- base_modules = sorted(set([ self.modulesystem.find(m)
- for m in modules ]))
+ base_modules = sorted({ self.modulesystem.find(m)
+ for m in modules })
# Perform transitive closure.
final_modules = self.moduletable.transitive_closure(base_modules)
--
2.44.0