Skip to content

Commit a1f7b08

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 <gaetan.lehmann@vates.tech>
1 parent 1b9c715 commit a1f7b08

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

scripts/import_srpm.py

Lines changed: 36 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')
@@ -76,9 +109,10 @@ def main():
76109
print(" extracting SRPM...")
77110

78111
os.chdir('SOURCES')
79-
os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
112+
pipe_commands(['rpm2cpio', source_rpm_abs], ['cpio', '-idmv'])
80113
os.chdir('..')
81-
os.system('mv SOURCES/*.spec SPECS/')
114+
for f in glob('SOURCES/*.spec'):
115+
shutil.move(f, 'SPECS')
82116

83117
print(" removing trademarked or copyrighted files...")
84118

0 commit comments

Comments
 (0)