bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* gnulib-tool.py: Simplify running some commands in a given directory
@ 2024-04-19 16:24 Bruno Haible
  2024-04-19 16:41 ` Collin Funk
  0 siblings, 1 reply; 2+ messages in thread
From: Bruno Haible @ 2024-04-19 16:24 UTC (permalink / raw)
  To: bug-gnulib

This patch optimizes some subprocess invocations.

I did not change the os.chdir calls around larger blocks of code.


2024-04-19  Bruno Haible  <bruno@clisp.org>

	gnulib-tool.py: Simplify running some commands in a given directory.
	* pygnulib/GLImport.py (GLImport.execute): Use sp.call with a cwd
	argument, instead of calling chdir twice.
	* pygnulib/GLModuleSystem.py (GLModuleSystem.list): Likewise.
	* pygnulib/main.py (mode=='find'): Likewise.

diff --git a/gnulib-tool.py.TODO b/gnulib-tool.py.TODO
index 160f7b7465..581aec6487 100644
--- a/gnulib-tool.py.TODO
+++ b/gnulib-tool.py.TODO
@@ -5,9 +5,6 @@ Bugs:
   - error message missing when gl_DOC_BASE missing
     https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00160.html
 
-Optimize:
-  - os.chdir around subprocess creation -> cwd=... argument instead.
-
 Various other refactorings, as deemed useful:
   - Use an enum for 'all', 'old', 'new', 'added', 'removed' in GLImport.py.
 
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index 0a19d50e00..b38808a353 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -1200,11 +1200,9 @@ def execute(self, filetable: dict[str, list[str]], transformers: dict[str, str])
             TP_URL = 'https://translationproject.org/latest/'
             if not self.config['dryrun']:
                 print('Fetching gnulib PO files from %s' % TP_URL)
-                os.chdir(joinpath(destdir, pobase))
                 args = ['wget', '--no-verbose', '--mirror', '--level=1', '-nd', '-A.po', '-P', '.',
                         '%sgnulib/' % TP_URL]
-                sp.call(args)
-                os.chdir(DIRS['cwd'])
+                sp.call(args, cwd=joinpath(destdir, pobase))
             else:  # if self.config['dryrun']
                 print('Fetch gnulib PO files from %s' % TP_URL)
 
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index efc39ec925..112a6bc0e6 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -133,16 +133,12 @@ def list(self) -> list[str]:
         find_args = ['find', 'modules', '-type', 'f', '-print']
 
         # Read modules from gnulib root directory.
-        os.chdir(constants.DIRS['root'])
-        result += sp.run(find_args, text=True, capture_output=True, check=False).stdout
-        os.chdir(DIRS['cwd'])
+        result += sp.run(find_args, cwd=constants.DIRS['root'], text=True, capture_output=True, check=False).stdout
 
         # Read modules from local directories.
         if len(localpath) > 0:
             for localdir in localpath:
-                os.chdir(localdir)
-                result += sp.run(find_args, text=True, capture_output=True, check=False).stdout
-                os.chdir(DIRS['cwd'])
+                result += sp.run(find_args, cwd=localdir, text=True, capture_output=True, check=False).stdout
 
         listing = [ line
                     for line in result.split('\n')
diff --git a/pygnulib/main.py b/pygnulib/main.py
index 8316c0abf2..a333994525 100644
--- a/pygnulib/main.py
+++ b/pygnulib/main.py
@@ -872,18 +872,14 @@ def main() -> None:
                 filename_line_regex = '^' + filename_regex + '$'
                 # Read module candidates from gnulib root directory.
                 command = "find modules -type f -print | xargs -n 100 grep -l %s /dev/null | sed -e 's,^modules/,,'" % shlex.quote(filename_line_regex)
-                os.chdir(constants.DIRS['root'])
-                with sp.Popen(command, shell=True, stdout=sp.PIPE) as proc:
+                with sp.Popen(command, shell=True, cwd=constants.DIRS['root'], stdout=sp.PIPE) as proc:
                     result = proc.stdout.read().decode('UTF-8')
-                os.chdir(DIRS['cwd'])
                 # Read module candidates from local directories.
                 if localpath != None and len(localpath) > 0:
                     command = "find modules -type f -print | xargs -n 100 grep -l %s /dev/null | sed -e 's,^modules/,,' -e 's,\\.diff$,,'" % shlex.quote(filename_line_regex)
                     for localdir in localpath:
-                        os.chdir(localdir)
-                        with sp.Popen(command, shell=True, stdout=sp.PIPE) as proc:
+                        with sp.Popen(command, shell=True, cwd=localdir, stdout=sp.PIPE) as proc:
                             result += proc.stdout.read().decode('UTF-8')
-                        os.chdir(DIRS['cwd'])
                 listing = [ line
                             for line in result.split('\n')
                             if line.strip() ]





^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: gnulib-tool.py: Simplify running some commands in a given directory
  2024-04-19 16:24 gnulib-tool.py: Simplify running some commands in a given directory Bruno Haible
@ 2024-04-19 16:41 ` Collin Funk
  0 siblings, 0 replies; 2+ messages in thread
From: Collin Funk @ 2024-04-19 16:41 UTC (permalink / raw)
  To: bug-gnulib

On 4/19/24 9:24 AM, Bruno Haible wrote:
> This patch optimizes some subprocess invocations.
> 
> I did not change the os.chdir calls around larger blocks of code.

Looks good. I had a look at doing this previously but in GLTestDir for
example, we use 'constants.execute' to deal with the:

     Executing autoheader...
     Executing touch config.h.in...

messages. I sort of focused on other changes instead of worrying about
breaking those...

Collin


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-19 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-19 16:24 gnulib-tool.py: Simplify running some commands in a given directory Bruno Haible
2024-04-19 16:41 ` Collin Funk

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).