##// END OF EJS Templates
subprocess: use subprocessio helper to run various subprocess commands.
marcink -
r370:79380b7f default
parent child Browse files
Show More
@@ -377,30 +377,6 b' def git_pre_receive(unused_repo_path, re'
377 377 return _call_hook('pre_push', extras, GitMessageWriter())
378 378
379 379
380 def _run_command(arguments):
381 """
382 Run the specified command and return the stdout.
383
384 :param arguments: sequence of program arguments (including the program name)
385 :type arguments: list[str]
386 """
387
388 cmd = arguments
389 try:
390 gitenv = os.environ.copy()
391 _opts = {'env': gitenv, 'shell': False, 'fail_on_stderr': False}
392 p = subprocessio.SubprocessIOChunker(cmd, **_opts)
393 stdout = ''.join(p)
394 except (EnvironmentError, OSError) as err:
395 cmd = ' '.join(cmd) # human friendly CMD
396 tb_err = ("Couldn't run git command (%s).\n"
397 "Original error was:%s\n" % (cmd, err))
398 log.exception(tb_err)
399 raise Exception(tb_err)
400
401 return stdout
402
403
404 380 def git_post_receive(unused_repo_path, revision_lines, env):
405 381 """
406 382 Post push hook.
@@ -437,21 +413,26 b' def git_post_receive(unused_repo_path, r'
437 413 # Fix up head revision if needed
438 414 cmd = [settings.GIT_EXECUTABLE, 'show', 'HEAD']
439 415 try:
440 _run_command(cmd)
416 subprocessio.run_command(cmd, env=os.environ.copy())
441 417 except Exception:
442 418 cmd = [settings.GIT_EXECUTABLE, 'symbolic-ref', 'HEAD',
443 419 'refs/heads/%s' % push_ref['name']]
444 420 print("Setting default branch to %s" % push_ref['name'])
445 _run_command(cmd)
421 subprocessio.run_command(cmd, env=os.environ.copy())
446 422
447 cmd = [settings.GIT_EXECUTABLE, 'for-each-ref', '--format=%(refname)',
448 'refs/heads/*']
449 heads = _run_command(cmd)
423 cmd = [settings.GIT_EXECUTABLE, 'for-each-ref',
424 '--format=%(refname)', 'refs/heads/*']
425 stdout, stderr = subprocessio.run_command(
426 cmd, env=os.environ.copy())
427 heads = stdout
450 428 heads = heads.replace(push_ref['ref'], '')
451 429 heads = ' '.join(head for head in heads.splitlines() if head)
452 cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse', '--pretty=format:%H',
453 '--', push_ref['new_rev'], '--not', heads]
454 git_revs.extend(_run_command(cmd).splitlines())
430 cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse',
431 '--pretty=format:%H', '--', push_ref['new_rev'],
432 '--not', heads]
433 stdout, stderr = subprocessio.run_command(
434 cmd, env=os.environ.copy())
435 git_revs.extend(stdout.splitlines())
455 436 elif push_ref['new_rev'] == empty_commit_id:
456 437 # delete branch case
457 438 git_revs.append('delete_branch=>%s' % push_ref['name'])
@@ -462,7 +443,9 b' def git_post_receive(unused_repo_path, r'
462 443 cmd = [settings.GIT_EXECUTABLE, 'log',
463 444 '{old_rev}..{new_rev}'.format(**push_ref),
464 445 '--reverse', '--pretty=format:%H']
465 git_revs.extend(_run_command(cmd).splitlines())
446 stdout, stderr = subprocessio.run_command(
447 cmd, env=os.environ.copy())
448 git_revs.extend(stdout.splitlines())
466 449 elif type_ == 'tags':
467 450 if push_ref['name'] not in tags:
468 451 tags.append(push_ref['name'])
@@ -482,3 +482,30 b' class SubprocessIOChunker(object):'
482 482
483 483 def __del__(self):
484 484 self.close()
485
486
487 def run_command(arguments, env=None):
488 """
489 Run the specified command and return the stdout.
490
491 :param arguments: sequence of program arguments (including the program name)
492 :type arguments: list[str]
493 """
494
495 cmd = arguments
496 log.debug('Running subprocessio command %s', cmd)
497 try:
498 _opts = {'shell': False, 'fail_on_stderr': False}
499 if env:
500 _opts.update({'env': env})
501 p = SubprocessIOChunker(cmd, **_opts)
502 stdout = ''.join(p)
503 stderr = ''.join(''.join(p.error))
504 except (EnvironmentError, OSError) as err:
505 cmd = ' '.join(cmd) # human friendly CMD
506 tb_err = ("Couldn't run subprocessio command (%s).\n"
507 "Original error was:%s\n" % (cmd, err))
508 log.exception(tb_err)
509 raise Exception(tb_err)
510
511 return stdout, stderr
@@ -22,7 +22,6 b' from urllib2 import URLError'
22 22 import logging
23 23 import posixpath as vcspath
24 24 import StringIO
25 import subprocess
26 25 import urllib
27 26 import traceback
28 27
@@ -33,11 +32,9 b' import svn.diff'
33 32 import svn.fs
34 33 import svn.repos
35 34
36 from vcsserver import svn_diff
37 from vcsserver import exceptions
35 from vcsserver import svn_diff, exceptions, subprocessio
38 36 from vcsserver.base import RepoFactory, raise_from_original
39 37
40
41 38 log = logging.getLogger(__name__)
42 39
43 40
@@ -164,10 +161,9 b' class SvnRemote(object):'
164 161 raise Exception(
165 162 "Path %s is not a valid Subversion repository." % repo_path)
166 163
167 load = subprocess.Popen(
168 ['svnadmin', 'info', repo_path],
169 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170 return ''.join(load.stdout)
164 cmd = ['svnadmin', 'info', repo_path]
165 stdout, stderr = subprocessio.run_command(cmd)
166 return stdout
171 167
172 168 def lookup(self, wire, revision):
173 169 if revision not in [-1, None, 'HEAD']:
@@ -343,7 +339,9 b' class SvnRemote(object):'
343 339 if not self.is_path_valid_repository(wire, repo_path):
344 340 raise Exception(
345 341 "Path %s is not a valid Subversion repository." % repo_path)
342
346 343 # TODO: johbo: URL checks ?
344 import subprocess
347 345 rdump = subprocess.Popen(
348 346 ['svnrdump', 'dump', '--non-interactive', src_url],
349 347 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
General Comments 0
You need to be logged in to leave comments. Login now