Skip to content

Commit 3782f97

Browse files
committed
Use pure python code in place of os.system
os.system calls the user's shell, and is not directly checking for errors. This should raise an error when cpio or rpm2cpio fail, or if they can't be found. Signed-off-by: Gaëtan Lehmann <[email protected]>
1 parent 1b9c715 commit 3782f97

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

scripts/import_srpm.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,45 @@
22
import argparse
33
import logging
44
import os
5+
import shutil
56
import subprocess
7+
from glob import glob
8+
69

710
def call_process(args):
811
logging.debug("$ %s", args)
912
subprocess.check_call(args)
1013

14+
def pipe_commands(*commands: list[str]) -> bytes:
15+
if not commands:
16+
raise ValueError("The 'commands' list cannot be empty.")
17+
if any(not cmd for cmd in commands):
18+
raise ValueError("All commands in the list must be non-empty.")
19+
20+
processes: list[subprocess.Popen[bytes]] = []
21+
last_process_stdout = None
22+
23+
for i, cmd_args in enumerate(commands):
24+
process = subprocess.Popen(
25+
cmd_args,
26+
stdin=last_process_stdout,
27+
stdout=subprocess.PIPE,
28+
)
29+
processes.append(process)
30+
# to prevent deadlocks if the current process finishes before the previous one.
31+
if last_process_stdout:
32+
last_process_stdout.close()
33+
last_process_stdout = process.stdout
34+
35+
final_stdout, _final_stderr = processes[-1].communicate()
36+
37+
for i, p in enumerate(processes):
38+
p.wait()
39+
if p.returncode != 0:
40+
raise subprocess.CalledProcessError(returncode=p.returncode, cmd=commands[i])
41+
42+
return final_stdout
43+
1144
def main():
1245
parser = argparse.ArgumentParser(description='Imports the contents of a source RPM into a git repository')
1346
parser.add_argument('source_rpm', help='local path to source RPM')
@@ -28,6 +61,10 @@ def main():
2861
}[args.verbose]
2962
logging.basicConfig(format='[%(levelname)s] %(message)s', level=loglevel)
3063

64+
for dep in ['cpio', 'rpm2cpio']:
65+
if shutil.which(dep) is None:
66+
parser.error(f"{dep} can't be found.")
67+
3168
# check that the source RPM file exists
3269
if not os.path.isfile(args.source_rpm):
3370
parser.error("File %s does not exist." % args.source_rpm)
@@ -76,9 +113,10 @@ def main():
76113
print(" extracting SRPM...")
77114

78115
os.chdir('SOURCES')
79-
os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
116+
pipe_commands(['rpm2cpio', source_rpm_abs], ['cpio', '-idmv'])
80117
os.chdir('..')
81-
os.system('mv SOURCES/*.spec SPECS/')
118+
for f in glob('SOURCES/*.spec'):
119+
shutil.move(f, 'SPECS')
82120

83121
print(" removing trademarked or copyrighted files...")
84122

0 commit comments

Comments
 (0)