]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Simplify running some commands in a given directory.
authorBruno Haible <bruno@clisp.org>
Fri, 19 Apr 2024 16:21:26 +0000 (18:21 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 19 Apr 2024 16:21:26 +0000 (18:21 +0200)
* 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.

ChangeLog
gnulib-tool.py.TODO
pygnulib/GLImport.py
pygnulib/GLModuleSystem.py
pygnulib/main.py

index f9b957efefa4d87a0cc871651f0561a4352f1731..e84856cc05aae3fa9dfc83bc75217ac49ab59073 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+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.
+
 2024-04-19  Bruno Haible  <bruno@clisp.org>
 
        gnulib-tool.py: Update authors list.
index 160f7b7465443b86463a7fccb3c430156a139056..581aec6487444b6d251adb0f0d142f595c1d07d7 100644 (file)
@@ -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.
 
index 0a19d50e00e920edbb17e0474a38f7d9ebcabe37..b38808a353952ff95de0968b779a601d4db60cf2 100644 (file)
@@ -1200,11 +1200,9 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
             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)
 
index efc39ec92506b55aa0e85888b6a2c96438292935..112a6bc0e649b24e4dd0684dae3d6e43a313f7ea 100644 (file)
@@ -133,16 +133,12 @@ class GLModuleSystem:
         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')
index 8316c0abf28dd3ecc55fd87ff7f824a287d3db19..a3339945259d55ce87afe37bb4a92c4938d8faca 100644 (file)
@@ -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() ]