From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-0.7 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id D01661F44D for ; Fri, 19 Apr 2024 16:24:45 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=clisp.org header.i=@clisp.org header.a=rsa-sha256 header.s=strato-dkim-0002 header.b=ckVynTtq; dkim=fail reason="signature verification failed" header.d=clisp.org header.i=@clisp.org header.a=ed25519-sha256 header.s=strato-dkim-0003 header.b=h6Sb8ItM; dkim-atps=neutral Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rxr2P-00004y-4f; Fri, 19 Apr 2024 12:24:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rxr1z-0008VB-SB for bug-gnulib@gnu.org; Fri, 19 Apr 2024 12:24:09 -0400 Received: from mo4-p00-ob.smtp.rzone.de ([81.169.146.221]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rxr1v-00054K-7X for bug-gnulib@gnu.org; Fri, 19 Apr 2024 12:24:05 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1713543841; cv=none; d=strato.com; s=strato-dkim-0002; b=p2bGWN2lzOwhgzMuw+R7RGkNSPh7TPpwQlU5aEaxb79GPM1OPjyOFb/GWJOKYLtmmK nHsMQ9QDUuzKBaUpkmceAlewDKKM8GamF276IUmwMqMJAIih27WroEmOdTm6JccM9xx7 lxjVji2Vh7DImsMftEgyUrF3Tg1eRn8EVpw5H/tTRcFIsWjIoZyWtUA97N/Hihczhi+y cfJ3JBRsKId423uZjSSyM2xJwMZaInVSMUG9TCS7tftU+6ri/psbk0SnV0sXFQHDpq+6 XctTtt8NrlQyyk56sIbWhx4VwC4S5BfHEnA5yRsbSgeIrsUlahTJUz9t5v+B7h1fROB5 pfVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1713543841; s=strato-dkim-0002; d=strato.com; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=K6cvrrpDA0Ayy2N90vMb4MGINoVAnfjKe1L8fOIOpnQ=; b=aO0IHiigtmzcoiL2YP3f78l4OGNHy3iZnYx9eUK5RGrXEZeks9vPeRdy5rP4yUfaEx vIF9WbXgUsFP+9QymXKreVFbOu0ytJR29qYCSRlhC/OiNkYSJGoYw5yL+yRR33E2ChbH bm8VwXr43wOqw1sm8M1C3uWjcJcjNIWcsAuzIMRjCCa466inP8tZwZkOfs0MVx/6dd1i hBa+L9ICYZ+GwtrMCXxk4FZOZ+EDAMhCyk6LH6y4TKE/v/oqFJStyUhSRkFPQgqLMTtQ tCVKwnsMUtUyQ40brTWGPUWBZEEFCo6JgnWu6r9EmpTjg0PQTax5PpZhlONsIqMfNgPC vAgA== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1713543841; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=K6cvrrpDA0Ayy2N90vMb4MGINoVAnfjKe1L8fOIOpnQ=; b=ckVynTtqBqiuWA8T1focjqXTrpttu7VrlvFTGomB03Bp0sX4BOUECaqOOzf/k8rnvl I6uI80SJ+f1bskWwbtVnTM//KuRBaX/qh1eM2NFWWIBcUykcc5e5ANMrN7/16uv6A9EN yv1FVaTjF6yn0yh7iGT15/cCbww4d/qcHfvQsPsBDRm+EKogS3tV92e/Bbp7R7gfnnky HU/g3tnXPAH+eGeuLndsXDNZmG7xfVk+CIF9wHRZJn8TZdaj/qpk013DLvKhUwLf5Yx2 IJrLVgSTqpfle3e4TcgcCME1+9unIInhjkh8veYlWB+eulLD8qbJZtncbyPe4/ho2SVh dSug== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1713543841; s=strato-dkim-0003; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=K6cvrrpDA0Ayy2N90vMb4MGINoVAnfjKe1L8fOIOpnQ=; b=h6Sb8ItM7u+IzR8V2dnjsYSzZiArD/nf7sAOu997N0z/lNxz/qIR7H6rwI5dwCW+Ey H813E7gGNOmT6lUcHnAQ== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpPF2POziCY9xERZIC/7LZ0RLpOvhA==" Received: from nimes.localnet by smtp.strato.de (RZmta 50.3.2 AUTH) with ESMTPSA id N8610003JGO14vD (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 19 Apr 2024 18:24:01 +0200 (CEST) From: Bruno Haible To: bug-gnulib@gnu.org Subject: gnulib-tool.py: Simplify running some commands in a given directory Date: Fri, 19 Apr 2024 18:24:00 +0200 Message-ID: <54154579.PhbG3uxBMr@nimes> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Received-SPF: none client-ip=81.169.146.221; envelope-from=bruno@clisp.org; helo=mo4-p00-ob.smtp.rzone.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org This patch optimizes some subprocess invocations. I did not change the os.chdir calls around larger blocks of code. 2024-04-19 Bruno Haible 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() ]