Skip to content

Commit

Permalink
gnulib-tool.py: Simplify running some commands in a given directory.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bhaible committed Apr 19, 2024
1 parent 6adcc62 commit 344f868
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2024-04-19 Bruno Haible <[email protected]>

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 <[email protected]>

gnulib-tool.py: Update authors list.
Expand Down
3 changes: 0 additions & 3 deletions gnulib-tool.py.TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 1 addition & 3 deletions pygnulib/GLImport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions pygnulib/GLModuleSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 2 additions & 6 deletions pygnulib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() ]
Expand Down

0 comments on commit 344f868

Please sign in to comment.