mirror of
https://github.com/openSUSE/osc.git
synced 2024-11-10 06:46:15 +01:00
- added new function core.run_external which can be used to execute an external program
Basically it's just a wrapper around subprocess.call which raises an ExtRuntimeError exception if subprocess.call raised an OSError with errno set to ENOENT (unfortunately the OSError's filename attribute is set to None therefore we cannot print a meaningful error message (that's why an ExtRuntimeError is raised)). Replaced all occurrences of subprocess.call with a corresponding run_external call.
This commit is contained in:
parent
ec595d361b
commit
93df866787
@ -149,7 +149,7 @@ def run(prg):
|
||||
print >>sys.stderr, e
|
||||
return 2
|
||||
except oscerr.ExtRuntimeError, e:
|
||||
print >>sys.stderr, e.msg
|
||||
print >>sys.stderr, e.file + ':', e.msg
|
||||
return 1
|
||||
except oscerr.WorkingCopyOutdated, e:
|
||||
print >>sys.stderr, e
|
||||
|
@ -13,7 +13,7 @@ import urlparse
|
||||
from tempfile import NamedTemporaryFile, mkdtemp
|
||||
from osc.fetch import *
|
||||
from osc.core import get_buildinfo, store_read_apiurl, store_read_project, store_read_package, meta_exists, quote_plus, get_buildconfig, is_package_dir
|
||||
from osc.core import get_binarylist, get_binary_file
|
||||
from osc.core import get_binarylist, get_binary_file, run_external
|
||||
from osc.util import rpmquery, debquery, archquery
|
||||
import osc.conf
|
||||
import oscerr
|
||||
@ -913,14 +913,15 @@ def main(apiurl, opts, argv):
|
||||
cmd = [ change_personality[bi.buildarch] ] + cmd;
|
||||
|
||||
try:
|
||||
rc = subprocess.call(cmd)
|
||||
rc = run_external(cmd[0], *cmd[1:])
|
||||
if rc:
|
||||
print
|
||||
print 'The buildroot was:', build_root
|
||||
sys.exit(rc)
|
||||
except KeyboardInterrupt, i:
|
||||
print "keyboard interrupt, killing build ..."
|
||||
subprocess.call(cmd + ["--kill"])
|
||||
cmd.append('--kill')
|
||||
run_external(cmd[0], *cmd[1:])
|
||||
raise i
|
||||
|
||||
pacdir = os.path.join(build_root, '.build.packages')
|
||||
|
@ -5416,7 +5416,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
pdir,
|
||||
"%s:%s" % (hostname, os.path.dirname(hostprefer))]
|
||||
print 'Run: %s' % " ".join(rsync_prefer_cmd)
|
||||
ret = subprocess.call(rsync_prefer_cmd)
|
||||
ret = run_external(rsync_prefer_cmd[0], *rsync_prefer_cmd[1:])
|
||||
if ret != 0:
|
||||
return ret
|
||||
|
||||
@ -5466,7 +5466,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
# 1.) rsync sources
|
||||
rsync_source_cmd = ['rsync', '-az', '-delete', '-e', 'ssh', cwd, "%s:%s" % (hostname, hostpath)]
|
||||
print 'Run: %s' % " ".join(rsync_source_cmd)
|
||||
ret = subprocess.call(rsync_source_cmd)
|
||||
ret = run_external(rsync_source_cmd[0], *rsync_source_cmd[1:])
|
||||
if ret != 0:
|
||||
return ret
|
||||
|
||||
@ -5497,7 +5497,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
local_args = " ".join(hostargs))
|
||||
]
|
||||
print 'Run: %s' % " ".join(ssh_cmd)
|
||||
build_ret = subprocess.call(ssh_cmd)
|
||||
build_ret = run_external(ssh_cmd[0], *ssh_cmd[1:])
|
||||
if build_ret != 0:
|
||||
return build_ret
|
||||
|
||||
@ -5505,7 +5505,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
if opts.keep_pkgs:
|
||||
ret = rsync_keep_cmd = ['rsync', '-az', '-e', 'ssh', "%s:%s" % (hostname, hostkeep), opts.keep_pkgs]
|
||||
print 'Run: %s' % " ".join(rsync_keep_cmd)
|
||||
ret = subprocess.call(rsync_keep_cmd)
|
||||
ret = run_external(rsync_keep_cmd[0], *rsync_keep_cmd[1:])
|
||||
if ret != 0:
|
||||
return ret
|
||||
|
||||
@ -7308,14 +7308,14 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
continue
|
||||
|
||||
o = open(os.path.join(destdir, name), 'wb')
|
||||
code = subprocess.call(['diff3', '-m', '-E',
|
||||
code = run_external('diff3', '-m', '-E',
|
||||
'-L', '.mine',
|
||||
os.path.join(destdir, name + '.mine'),
|
||||
'-L', '.old',
|
||||
os.path.join(destdir, name + '.old'),
|
||||
'-L', '.new',
|
||||
os.path.join(destdir, name + '.new'),
|
||||
], stdout=o)
|
||||
stdout=o)
|
||||
if code == 0:
|
||||
print statfrmt('G', name)
|
||||
os.unlink(os.path.join(destdir, name + '.mine'))
|
||||
@ -7436,11 +7436,11 @@ Please submit there instead, or use --nodevelproject to force direct submission.
|
||||
continue
|
||||
|
||||
o = open(name, 'wb')
|
||||
code = subprocess.call(['diff3', '-m', '-E',
|
||||
code = run_external('diff3', '-m', '-E',
|
||||
'-L', '.mine', name + '.mine',
|
||||
'-L', '.old', name + '.old',
|
||||
'-L', '.new', name + '.new',
|
||||
], stdout=o)
|
||||
stdout=o)
|
||||
if code == 0:
|
||||
print statfrmt('G', name)
|
||||
os.unlink(name + '.mine')
|
||||
|
51
osc/core.py
51
osc/core.py
@ -24,6 +24,7 @@ import conf
|
||||
import subprocess
|
||||
import re
|
||||
import socket
|
||||
import errno
|
||||
try:
|
||||
from xml.etree import cElementTree as ET
|
||||
except ImportError:
|
||||
@ -361,10 +362,10 @@ class Serviceinfo:
|
||||
name = call.split(None, 1)[0]
|
||||
if not os.path.exists("/usr/lib/obs/service/"+name):
|
||||
raise oscerr.PackageNotInstalled("obs-service-"+name)
|
||||
c = "/usr/lib/obs/service/" + call + " --outdir " + temp_dir
|
||||
cmd = "/usr/lib/obs/service/" + call + " --outdir " + temp_dir
|
||||
if conf.config['verbose'] > 1 or verbose:
|
||||
print "Run source service:", c
|
||||
r = subprocess.call(c, shell=True)
|
||||
print "Run source service:", cmd
|
||||
r = run_external(cmd, shell=True)
|
||||
|
||||
if r != 0:
|
||||
print "Aborting: service call failed: " + c
|
||||
@ -1458,8 +1459,7 @@ class Package:
|
||||
# try merging
|
||||
# diff3 OPTIONS... MINE OLDER YOURS
|
||||
merge_cmd = 'diff3 -m -E %s %s %s > %s' % (myfilename, storefilename, upfilename, filename)
|
||||
# we would rather use the subprocess module, but it is not availablebefore 2.4
|
||||
ret = subprocess.call(merge_cmd, shell=True)
|
||||
ret = run_external(merge_cmd, shell=True)
|
||||
|
||||
# "An exit status of 0 means `diff3' was successful, 1 means some
|
||||
# conflicts were found, and 2 means trouble."
|
||||
@ -3443,10 +3443,7 @@ def run_pager(message, tmp_suffix=''):
|
||||
tmpfile.flush()
|
||||
pager = os.getenv('PAGER', default=get_default_pager())
|
||||
try:
|
||||
try:
|
||||
subprocess.call('%s %s' % (pager, tmpfile.name), shell=True)
|
||||
except OSError, e:
|
||||
raise oscerr.ExtRuntimeError('cannot run pager \'%s\': %s' % (pager, e.strerror), pager)
|
||||
run_external(pager, tmpfile.name)
|
||||
finally:
|
||||
tmpfile.close()
|
||||
|
||||
@ -3454,10 +3451,7 @@ def run_editor(filename):
|
||||
editor = os.getenv('EDITOR', default=get_default_editor())
|
||||
cmd = editor.split(' ')
|
||||
cmd.append(filename)
|
||||
try:
|
||||
return subprocess.call(cmd)
|
||||
except OSError, e:
|
||||
raise oscerr.ExtRuntimeError('cannot run editor \'%s\': %s' % (editor, e.strerror), editor)
|
||||
return run_external(cmd[0], *cmd[1:])
|
||||
|
||||
def edit_message(footer='', template='', templatelen=30):
|
||||
delim = '--This line, and those below, will be ignored--\n'
|
||||
@ -5836,7 +5830,7 @@ def unpack_srcrpm(srpm, dir, *files):
|
||||
if os.path.isdir(dir):
|
||||
os.chdir(dir)
|
||||
cmd = 'rpm2cpio %s | cpio -i %s &> /dev/null' % (srpm, ' '.join(files))
|
||||
ret = subprocess.call(cmd, shell=True)
|
||||
ret = run_external(cmd, shell=True)
|
||||
if ret != 0:
|
||||
print >>sys.stderr, 'error \'%s\' - cannot extract \'%s\'' % (ret, srpm)
|
||||
sys.exit(1)
|
||||
@ -6413,7 +6407,7 @@ def request_interactive_review(apiurl, request, initial_cmd='', group=None, igno
|
||||
|
||||
def edit_submitrequest(apiurl, project, orequest, new_request=None):
|
||||
"""edit a submit action from orequest/new_request"""
|
||||
import tempfile, shutil, subprocess
|
||||
import tempfile, shutil
|
||||
actions = orequest.get_actions('submit')
|
||||
oactions = actions
|
||||
if not orequest is None:
|
||||
@ -6446,7 +6440,7 @@ def edit_submitrequest(apiurl, project, orequest, new_request=None):
|
||||
os.chdir(tmpdir)
|
||||
print 'Checked out package \'%s\' to %s. Started a new shell (%s).\n' \
|
||||
'Please fix the package and close the shell afterwards.' % (package, tmpdir, shell)
|
||||
subprocess.call(shell)
|
||||
run_external(shell)
|
||||
# the pkg might have uncommitted changes...
|
||||
cleanup = False
|
||||
os.chdir(olddir)
|
||||
@ -6529,6 +6523,31 @@ def raw_input(*args):
|
||||
# interpret ctrl-d as user abort
|
||||
raise oscerr.UserAbort()
|
||||
|
||||
def run_external(filename, *args, **kwargs):
|
||||
"""Executes the program filename via subprocess.call.
|
||||
|
||||
*args are additional arguments which are passed to the
|
||||
program filename. **kwargs specify additional arguments for
|
||||
the subprocess.call function.
|
||||
if no args are specified the plain filename is passed
|
||||
to subprocess.call (this can be used to execute a shell
|
||||
command). Otherwise [filename] + list(args) is passed
|
||||
to the subprocess.call function.
|
||||
|
||||
"""
|
||||
# unless explicitly specified use shell=False
|
||||
kwargs.setdefault('shell', False)
|
||||
if args:
|
||||
cmd = [filename] + list(args)
|
||||
else:
|
||||
cmd = filename
|
||||
try:
|
||||
return subprocess.call(cmd, **kwargs)
|
||||
except OSError, e:
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
raise oscerr.ExtRuntimeError(e.strerror, filename)
|
||||
|
||||
# backward compatibility: local role filtering
|
||||
def filter_role(meta, user, role):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user