author Olli Pettay <>
Sat, 26 Feb 2011 18:30:57 +0200
changeset 63137 151d55505c10300e89e4cca795d79597e1705e0f
parent 55996 942830bbb94c02d13dcfbe2ff0d956eb818d1d5a
child 63526 c39ecf6d18ba9f309638f319fd4d3c438afdccfa
permissions -rw-r--r--
Backout Bug 632835 in order to fix Bug 632835, a=backout

# Basic commands implemented in Python
import sys, os, shutil, time
from getopt import getopt, GetoptError

from process import PythonException

__all__ = ["rm", "sleep", "touch"]

def rm(args):
  Emulate most of the behavior of rm(1).
  Only supports the -r (--recursive) and -f (--force) arguments.
    opts, args = getopt(args, "rRf", ["force", "recursive"])
  except GetoptError, e:
    raise PythonException, ("rm: %s" % e, 1)
  force = False
  recursive = False
  for o, a in opts:
    if o in ('-f', '--force'):
      force = True
    elif o in ('-r', '-R', '--recursive'):
      recursive = True
  for f in args:
    if os.path.isdir(f):
      if not recursive:
        raise PythonException, ("rm: cannot remove '%s': Is a directory" % f, 1)
        shutil.rmtree(f, force)
    elif os.path.exists(f):
        if not force:
          raise PythonException, ("rm: failed to remove '%s': %s" % (f, sys.exc_info()[0]), 1)
    elif not force:
      raise PythonException, ("rm: cannot remove '%s': No such file or directory" % f, 1)

def sleep(args):
    Emulate the behavior of sleep(1).
    total = 0
    values = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
    for a in args:
        multiplier = 1
        for k, v in values.iteritems():
            if a.endswith(k):
                a = a[:-1]
                multiplier = v
            f = float(a)
            total += f * multiplier
        except ValueError:
            raise PythonException, ("sleep: invalid time interval '%s'" % a, 1)

def touch(args):
    Emulate the behavior of touch(1).
    for f in args:
        if os.path.exists(f):
            os.utime(f, None)
            open(f, 'w').close()