2
2
import argparse
3
3
import logging
4
4
import os
5
+ import shutil
5
6
import subprocess
7
+ from glob import glob
8
+
6
9
7
10
def call_process (args ):
8
11
logging .debug ("$ %s" , args )
9
12
subprocess .check_call (args )
10
13
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
+
11
41
def main ():
12
42
parser = argparse .ArgumentParser (description = 'Imports the contents of a source RPM into a git repository' )
13
43
parser .add_argument ('source_rpm' , help = 'local path to source RPM' )
@@ -28,6 +58,10 @@ def main():
28
58
}[args .verbose ]
29
59
logging .basicConfig (format = '[%(levelname)s] %(message)s' , level = loglevel )
30
60
61
+ for dep in ['cpio' , 'rpm2cpio' ]:
62
+ if shutil .which (dep ) is None :
63
+ parser .error (f"{ dep } can't be found." )
64
+
31
65
# check that the source RPM file exists
32
66
if not os .path .isfile (args .source_rpm ):
33
67
parser .error ("File %s does not exist." % args .source_rpm )
@@ -76,9 +110,10 @@ def main():
76
110
print (" extracting SRPM..." )
77
111
78
112
os .chdir ('SOURCES' )
79
- os . system ( 'rpm2cpio "%s" | cpio -idmv' % source_rpm_abs )
113
+ pipe_commands ([ 'rpm2cpio' , source_rpm_abs ], [ ' cpio' , ' -idmv'] )
80
114
os .chdir ('..' )
81
- os .system ('mv SOURCES/*.spec SPECS/' )
115
+ for f in glob ('SOURCES/*.spec' ):
116
+ shutil .move (f , 'SPECS' )
82
117
83
118
print (" removing trademarked or copyrighted files..." )
84
119
0 commit comments