Back to index

enigmail  1.4.3
builtins.py
Go to the documentation of this file.
00001 # Basic commands implemented in Python
00002 import errno, sys, os, shutil, time
00003 from getopt import getopt, GetoptError
00004 
00005 from process import PythonException
00006 
00007 __all__ = ["mkdir", "rm", "sleep", "touch"]
00008 
00009 def mkdir(args):
00010   """
00011   Emulate some of the behavior of mkdir(1).
00012   Only supports the -p (--parents) argument.
00013   """
00014   try:
00015     opts, args = getopt(args, "p", ["parents"])
00016   except GetoptError, e:
00017     raise PythonException, ("mkdir: %s" % e, 1)
00018   parents = False
00019   for o, a in opts:
00020     if o in ('-p', '--parents'):
00021       parents = True
00022   for f in args:
00023     try:
00024       if parents:
00025         os.makedirs(f)
00026       else:
00027         os.mkdir(f)
00028     except OSError, e:
00029       if e.errno == errno.EEXIST and parents:
00030         pass
00031       else:
00032         raise PythonException, ("mkdir: %s" % e, 1)
00033 
00034 def rm(args):
00035   """
00036   Emulate most of the behavior of rm(1).
00037   Only supports the -r (--recursive) and -f (--force) arguments.
00038   """
00039   try:
00040     opts, args = getopt(args, "rRf", ["force", "recursive"])
00041   except GetoptError, e:
00042     raise PythonException, ("rm: %s" % e, 1)
00043   force = False
00044   recursive = False
00045   for o, a in opts:
00046     if o in ('-f', '--force'):
00047       force = True
00048     elif o in ('-r', '-R', '--recursive'):
00049       recursive = True
00050   for f in args:
00051     if os.path.isdir(f):
00052       if not recursive:
00053         raise PythonException, ("rm: cannot remove '%s': Is a directory" % f, 1)
00054       else:
00055         shutil.rmtree(f, force)
00056     elif os.path.exists(f):
00057       try:
00058         os.unlink(f)
00059       except:
00060         if not force:
00061           raise PythonException, ("rm: failed to remove '%s': %s" % (f, sys.exc_info()[0]), 1)
00062     elif not force:
00063       raise PythonException, ("rm: cannot remove '%s': No such file or directory" % f, 1)
00064 
00065 def sleep(args):
00066     """
00067     Emulate the behavior of sleep(1).
00068     """
00069     total = 0
00070     values = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
00071     for a in args:
00072         multiplier = 1
00073         for k, v in values.iteritems():
00074             if a.endswith(k):
00075                 a = a[:-1]
00076                 multiplier = v
00077                 break
00078         try:
00079             f = float(a)
00080             total += f * multiplier
00081         except ValueError:
00082             raise PythonException, ("sleep: invalid time interval '%s'" % a, 1)
00083     time.sleep(total)
00084 
00085 def touch(args):
00086     """
00087     Emulate the behavior of touch(1).
00088     """
00089     for f in args:
00090         if os.path.exists(f):
00091             os.utime(f, None)
00092         else:
00093             open(f, 'w').close()