Bug 637034 - Add a native mkdir to pymake.builtins. r=ted
authorMitchell Field <mitchell.field@live.com.au>
Tue, 22 Mar 2011 23:57:12 +1100
changeset 288 562adc586a2460f0b45c003003cc97a8bc243ae5
parent 287 b2bcdfce1e3d868d2f0cf4082c88934a28b74d78
child 289 944fd46f8f787dd6b6b2b0a4002e754f0e8064e7
push id170
push usermitchell.field@live.com.au
push dateTue, 22 Mar 2011 13:04:51 +0000
reviewersted
bugs637034
Bug 637034 - Add a native mkdir to pymake.builtins. r=ted
pymake/builtins.py
pymake/implicit.py
tests/rm.mk
--- a/pymake/builtins.py
+++ b/pymake/builtins.py
@@ -1,15 +1,40 @@
 # Basic commands implemented in Python
-import sys, os, shutil, time
+import errno, sys, os, shutil, time
 from getopt import getopt, GetoptError
 
 from process import PythonException
 
-__all__ = ["rm", "sleep", "touch"]
+__all__ = ["mkdir", "rm", "sleep", "touch"]
+
+def mkdir(args):
+  """
+  Emulate some of the behavior of mkdir(1).
+  Only supports the -p (--parents) argument.
+  """
+  try:
+    opts, args = getopt(args, "p", ["parents"])
+  except GetoptError, e:
+    raise PythonException, ("mkdir: %s" % e, 1)
+  parents = False
+  for o, a in opts:
+    if o in ('-p', '--parents'):
+      parents = True
+  for f in args:
+    try:
+      if parents:
+        os.makedirs(f)
+      else:
+        os.mkdir(f)
+    except OSError, e:
+      if e.errno == errno.EEXIST and parents:
+        pass
+      else:
+        raise PythonException, ("mkdir: %s" % e, 1)
 
 def rm(args):
   """
   Emulate most of the behavior of rm(1).
   Only supports the -r (--recursive) and -f (--force) arguments.
   """
   try:
     opts, args = getopt(args, "rRf", ["force", "recursive"])
--- a/pymake/implicit.py
+++ b/pymake/implicit.py
@@ -1,13 +1,14 @@
 """
 Implicit variables; perhaps in the future this will also include some implicit
 rules, at least match-anything cancellation rules.
 """
 
 variables = {
+    'MKDIR': '%pymake.builtins mkdir',
     'RM': '%pymake.builtins rm -f',
     'SLEEP': '%pymake.builtins sleep',
     'TOUCH': '%pymake.builtins touch',
     '.LIBPATTERNS': 'lib%.so lib%.a',
     '.PYMAKE': '1',
     }
 
--- a/tests/rm.mk
+++ b/tests/rm.mk
@@ -3,11 +3,19 @@ all:
 	$(RM) nosuchfile
 	touch newfile
 	test -f newfile
 	$(RM) newfile
 	test ! -f newfile
 	mkdir newdir
 	test -d newdir
 	touch newdir/newfile
+	mkdir newdir/subdir
+	$(RM) -r newdir/subdir
+	test ! -d newdir/subdir
+	test -d newdir
+	mkdir newdir/subdir1 newdir/subdir2
+	$(RM) -r newdir/subdir1 newdir/subdir2
+	test ! -d newdir/subdir1 -a ! -d newdir/subdir2
+	test -d newdir
 	$(RM) -r newdir
 	test ! -d newdir
 	@echo TEST-PASS