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
+ last_process_stdout = None
22
+
23
+ for command in commands :
24
+ process = subprocess .Popen (
25
+ command ,
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
+
11
44
def main ():
12
45
parser = argparse .ArgumentParser (description = 'Imports the contents of a source RPM into a git repository' )
13
46
parser .add_argument ('source_rpm' , help = 'local path to source RPM' )
@@ -28,6 +61,10 @@ def main():
28
61
}[args .verbose ]
29
62
logging .basicConfig (format = '[%(levelname)s] %(message)s' , level = loglevel )
30
63
64
+ for dep in ['cpio' , 'rpm2cpio' ]:
65
+ if shutil .which (dep ) is None :
66
+ parser .error (f"{ dep } can't be found." )
67
+
31
68
# check that the source RPM file exists
32
69
if not os .path .isfile (args .source_rpm ):
33
70
parser .error ("File %s does not exist." % args .source_rpm )
@@ -76,9 +113,10 @@ def main():
76
113
print (" extracting SRPM..." )
77
114
78
115
os .chdir ('SOURCES' )
79
- os . system ( 'rpm2cpio "%s" | cpio -idmv' % source_rpm_abs )
116
+ pipe_commands ([ 'rpm2cpio' , source_rpm_abs ], [ ' cpio' , ' -idmv'] )
80
117
os .chdir ('..' )
81
- os .system ('mv SOURCES/*.spec SPECS/' )
118
+ for f in glob ('SOURCES/*.spec' ):
119
+ shutil .move (f , 'SPECS' )
82
120
83
121
print (" removing trademarked or copyrighted files..." )
84
122
0 commit comments