Skip to content

Commit fa8bab3

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 60dcc93 commit fa8bab3

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

scripts/import_srpm.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,42 @@
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+
next_process_stdin = None
22+
23+
for command in commands:
24+
process = subprocess.Popen(
25+
command,
26+
stdin=next_process_stdin,
27+
stdout=subprocess.PIPE,
28+
)
29+
processes.append(process)
30+
next_process_stdin = process.stdout
31+
32+
final_stdout, _final_stderr = processes[-1].communicate()
33+
34+
for cmd, process in zip(commands, processes):
35+
process.wait()
36+
if process.returncode != 0:
37+
raise subprocess.CalledProcessError(returncode=process.returncode, cmd=cmd)
38+
39+
return final_stdout
40+
1141
def main():
1242
parser = argparse.ArgumentParser(description='Imports the contents of a source RPM into a git repository')
1343
parser.add_argument('source_rpm', help='local path to source RPM')
@@ -28,6 +58,10 @@ def main():
2858
}[args.verbose]
2959
logging.basicConfig(format='[%(levelname)s] %(message)s', level=loglevel)
3060

61+
for dep in ['cpio', 'rpm2cpio']:
62+
if shutil.which(dep) is None:
63+
parser.error(f"{dep} can't be found.")
64+
3165
# check that the source RPM file exists
3266
if not os.path.isfile(args.source_rpm):
3367
parser.error("File %s does not exist." % args.source_rpm)
@@ -76,9 +110,10 @@ def main():
76110
print(" extracting SRPM...")
77111

78112
os.chdir('SOURCES')
79-
os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
113+
pipe_commands(['rpm2cpio', source_rpm_abs], ['cpio', '-idmv'])
80114
os.chdir('..')
81-
os.system('mv SOURCES/*.spec SPECS/')
115+
for f in glob('SOURCES/*.spec'):
116+
shutil.move(f, 'SPECS')
82117

83118
print(" removing trademarked or copyrighted files...")
84119

0 commit comments

Comments
 (0)