Bug 637034 - Add a native mkdir to pymake.builtins. r=ted
authorMitchell Field <mitchell.field@live.com.au>
Sun, 20 Mar 2011 20:12:36 +1100
changeset 63526 c39ecf6d18ba9f309638f319fd4d3c438afdccfa
parent 63525 944066f3e3f258ff85cba4d543513521d6b1c532
child 63527 6aeab14f8db5a93fde7c9f7d074b5b2ea3f491a4
push id19231
push userkhuey@kylehuey.com
push dateWed, 23 Mar 2011 00:22:04 +0000
treeherdermozilla-central@57a0f8967cce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs637034
milestone2.0b13pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 637034 - Add a native mkdir to pymake.builtins. r=ted
build/pymake/pymake/builtins.py
build/pymake/pymake/implicit.py
build/pymake/tests/mkdir-fail.mk
build/pymake/tests/mkdir.mk
build/pymake/tests/rm.mk
--- a/build/pymake/pymake/builtins.py
+++ b/build/pymake/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/build/pymake/pymake/implicit.py
+++ b/build/pymake/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',
     }
 
new file mode 100644
--- /dev/null
+++ b/build/pymake/tests/mkdir-fail.mk
@@ -0,0 +1,7 @@
+#T returncode: 1
+all:
+	mkdir newdir/subdir
+	test ! -d newdir/subdir
+	test ! -d newdir
+	rm -r newdir
+	@echo TEST-PASS
new file mode 100644
--- /dev/null
+++ b/build/pymake/tests/mkdir.mk
@@ -0,0 +1,27 @@
+MKDIR ?= mkdir
+
+all:
+	$(MKDIR) newdir
+	test -d newdir
+	# subdir, parent exists
+	$(MKDIR) newdir/subdir
+	test -d newdir/subdir
+	# -p, existing dir
+	$(MKDIR) -p newdir
+	# -p, existing subdir
+	$(MKDIR) -p newdir/subdir
+	# multiple subdirs, existing parent
+	$(MKDIR) newdir/subdir1 newdir/subdir2
+	test -d newdir/subdir1 -a -d newdir/subdir2
+	rm -r newdir
+	# -p, subdir, no existing parent
+	$(MKDIR) -p newdir/subdir
+	test -d newdir/subdir
+	rm -r newdir
+	# -p, multiple subdirs, no existing parent
+	$(MKDIR) -p newdir/subdir1 newdir/subdir2
+	test -d newdir/subdir1 -a -d newdir/subdir2
+	# -p, multiple existing subdirs
+	$(MKDIR) -p newdir/subdir1 newdir/subdir2
+	rm -r newdir
+	@echo TEST-PASS
--- a/build/pymake/tests/rm.mk
+++ b/build/pymake/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