mercurial/util.py
changeset 508 42a660abaf75
parent 464 50da4bb9cab6
child 515 03f27b1381f9
equal deleted inserted replaced
507:dd8b19114fe7 508:42a660abaf75
     4 #
     4 #
     5 # This software may be used and distributed according to the terms
     5 # This software may be used and distributed according to the terms
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
     8 import os
     8 import os
       
     9 
       
    10 class CommandError(Exception): pass
       
    11 
       
    12 def explain_exit(code):
       
    13     """return a 2-tuple (desc, code) describing a process's status"""
       
    14     if os.WIFEXITED(code):
       
    15         val = os.WEXITSTATUS(code)
       
    16         return "exited with status %d" % val, val
       
    17     elif os.WIFSIGNALED(code):
       
    18         val = os.WTERMSIG(code)
       
    19         return "killed by signal %d" % val, val
       
    20     elif os.WIFSTOPPED(code):
       
    21         val = os.STOPSIG(code)
       
    22         return "stopped by signal %d" % val, val
       
    23     raise ValueError("invalid exit code")
       
    24     
       
    25 def system(cmd, errprefix = "abort"):
       
    26     """execute a shell command that must succeed"""
       
    27     rc = os.system(cmd)
       
    28     if rc:
       
    29         errmsg = "%s: %s %s" % (errprefix, os.path.basename(cmd.split(None, 1)[0]),
       
    30                                 explain_exit(rc)[0])
       
    31         raise CommandError(errmsg)
     9 
    32 
    10 def rename(src, dst):
    33 def rename(src, dst):
    11     try:
    34     try:
    12         os.rename(src, dst)
    35         os.rename(src, dst)
    13     except:
    36     except: