##// END OF EJS Templates
subprocess: use subprocessio helper to run various subprocess commands.
marcink -
r374:58336b17 stable
parent child Browse files
Show More
@@ -377,30 +377,6 b' def git_pre_receive(unused_repo_path, re'
377 return _call_hook('pre_push', extras, GitMessageWriter())
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 def git_post_receive(unused_repo_path, revision_lines, env):
380 def git_post_receive(unused_repo_path, revision_lines, env):
405 """
381 """
406 Post push hook.
382 Post push hook.
@@ -437,21 +413,26 b' def git_post_receive(unused_repo_path, r'
437 # Fix up head revision if needed
413 # Fix up head revision if needed
438 cmd = [settings.GIT_EXECUTABLE, 'show', 'HEAD']
414 cmd = [settings.GIT_EXECUTABLE, 'show', 'HEAD']
439 try:
415 try:
440 _run_command(cmd)
416 subprocessio.run_command(cmd, env=os.environ.copy())
441 except Exception:
417 except Exception:
442 cmd = [settings.GIT_EXECUTABLE, 'symbolic-ref', 'HEAD',
418 cmd = [settings.GIT_EXECUTABLE, 'symbolic-ref', 'HEAD',
443 'refs/heads/%s' % push_ref['name']]
419 'refs/heads/%s' % push_ref['name']]
444 print("Setting default branch to %s" % push_ref['name'])
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)',
423 cmd = [settings.GIT_EXECUTABLE, 'for-each-ref',
448 'refs/heads/*']
424 '--format=%(refname)', 'refs/heads/*']
449 heads = _run_command(cmd)
425 stdout, stderr = subprocessio.run_command(
426 cmd, env=os.environ.copy())
427 heads = stdout
450 heads = heads.replace(push_ref['ref'], '')
428 heads = heads.replace(push_ref['ref'], '')
451 heads = ' '.join(head for head in heads.splitlines() if head)
429 heads = ' '.join(head for head in heads.splitlines() if head)
452 cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse', '--pretty=format:%H',
430 cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse',
453 '--', push_ref['new_rev'], '--not', heads]
431 '--pretty=format:%H', '--', push_ref['new_rev'],
454 git_revs.extend(_run_command(cmd).splitlines())
432 '--not', heads]
433 stdout, stderr = subprocessio.run_command(
434 cmd, env=os.environ.copy())
435 git_revs.extend(stdout.splitlines())
455 elif push_ref['new_rev'] == empty_commit_id:
436 elif push_ref['new_rev'] == empty_commit_id:
456 # delete branch case
437 # delete branch case
457 git_revs.append('delete_branch=>%s' % push_ref['name'])
438 git_revs.append('delete_branch=>%s' % push_ref['name'])
@@ -462,7 +443,9 b' def git_post_receive(unused_repo_path, r'
462 cmd = [settings.GIT_EXECUTABLE, 'log',
443 cmd = [settings.GIT_EXECUTABLE, 'log',
463 '{old_rev}..{new_rev}'.format(**push_ref),
444 '{old_rev}..{new_rev}'.format(**push_ref),
464 '--reverse', '--pretty=format:%H']
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 elif type_ == 'tags':
449 elif type_ == 'tags':
467 if push_ref['name'] not in tags:
450 if push_ref['name'] not in tags:
468 tags.append(push_ref['name'])
451 tags.append(push_ref['name'])
@@ -482,3 +482,30 b' class SubprocessIOChunker(object):'
482
482
483 def __del__(self):
483 def __del__(self):
484 self.close()
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 import logging
22 import logging
23 import posixpath as vcspath
23 import posixpath as vcspath
24 import StringIO
24 import StringIO
25 import subprocess
26 import urllib
25 import urllib
27 import traceback
26 import traceback
28
27
@@ -33,11 +32,9 b' import svn.diff'
33 import svn.fs
32 import svn.fs
34 import svn.repos
33 import svn.repos
35
34
36 from vcsserver import svn_diff
35 from vcsserver import svn_diff, exceptions, subprocessio
37 from vcsserver import exceptions
38 from vcsserver.base import RepoFactory, raise_from_original
36 from vcsserver.base import RepoFactory, raise_from_original
39
37
40
41 log = logging.getLogger(__name__)
38 log = logging.getLogger(__name__)
42
39
43
40
@@ -164,10 +161,9 b' class SvnRemote(object):'
164 raise Exception(
161 raise Exception(
165 "Path %s is not a valid Subversion repository." % repo_path)
162 "Path %s is not a valid Subversion repository." % repo_path)
166
163
167 load = subprocess.Popen(
164 cmd = ['svnadmin', 'info', repo_path]
168 ['svnadmin', 'info', repo_path],
165 stdout, stderr = subprocessio.run_command(cmd)
169 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
166 return stdout
170 return ''.join(load.stdout)
171
167
172 def lookup(self, wire, revision):
168 def lookup(self, wire, revision):
173 if revision not in [-1, None, 'HEAD']:
169 if revision not in [-1, None, 'HEAD']:
@@ -343,7 +339,9 b' class SvnRemote(object):'
343 if not self.is_path_valid_repository(wire, repo_path):
339 if not self.is_path_valid_repository(wire, repo_path):
344 raise Exception(
340 raise Exception(
345 "Path %s is not a valid Subversion repository." % repo_path)
341 "Path %s is not a valid Subversion repository." % repo_path)
342
346 # TODO: johbo: URL checks ?
343 # TODO: johbo: URL checks ?
344 import subprocess
347 rdump = subprocess.Popen(
345 rdump = subprocess.Popen(
348 ['svnrdump', 'dump', '--non-interactive', src_url],
346 ['svnrdump', 'dump', '--non-interactive', src_url],
349 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
347 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
General Comments 0
You need to be logged in to leave comments. Login now