bug 482733 - nsinstall.py should support copying directories recursively. r=pike
authorTed Mielczarek <ted.mielczarek@gmail.com>
Thu, 12 Mar 2009 08:46:38 -0400
changeset 26094 6d2ef6b135fa996edca8d3c28d88579b97b6909b
parent 26093 f01a5fd071f3b55a5a4a6b928a883e853637cda8
child 26095 297c00e7245b1eba41a68bdea35ca4c9760c59e1
push id5894
push usertmielczarek@mozilla.com
push dateThu, 12 Mar 2009 12:44:04 +0000
treeherdermozilla-central@6d2ef6b135fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspike
bugs482733
milestone1.9.2a1pre
bug 482733 - nsinstall.py should support copying directories recursively. r=pike
config/nsinstall.py
config/tests/unit-nsinstall.py
--- a/config/nsinstall.py
+++ b/config/nsinstall.py
@@ -107,38 +107,48 @@ def nsinstall(argv):
     else:
       os.makedirs(args[0])
     return 0
 
   # nsinstall arg1 [...] directory
   if len(args) < 2:
     p.error('not enough arguments')
 
+  def copy_all_entries(entries, target):
+    for e in entries:
+      dest = os.path.join(target,
+                          os.path.basename(os.path.normpath(e)))
+      handleTarget(e, dest)
+      if options.m:
+        os.chmod(dest, options.m)
+
   # set up handler
   if options.d:
     # we're supposed to create directories
     def handleTarget(srcpath, targetpath):
       # target directory was already created, just use mkdir
-      os.mkdir(dest)
+      os.mkdir(targetpath)
   else:
     # we're supposed to copy files
     def handleTarget(srcpath, targetpath):
-      if options.t:
+      if os.path.isdir(srcpath):
+        os.mkdir(targetpath)
+        entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
+        copy_all_entries(entries, targetpath)
+        # options.t is not relevant for directories
+        if options.m:
+          os.chmod(targetpath, options.m)
+      elif options.t:
         shutil.copy2(srcpath, targetpath)
       else:
         shutil.copy(srcpath, targetpath)
 
   # the last argument is the target directory
   target = args.pop()
   # ensure target directory
   if not os.path.isdir(target):
     os.makedirs(target)
 
-  for f in args:
-    dest = os.path.join(target,
-                        os.path.basename(os.path.normpath(f)))
-    handleTarget(f, dest)
-    if options.m:
-      os.chmod(dest, options.m)
+  copy_all_entries(args, target)
   return 0
 
 if __name__ == '__main__':
   sys.exit(nsinstall(sys.argv[1:]))
--- a/config/tests/unit-nsinstall.py
+++ b/config/tests/unit-nsinstall.py
@@ -38,16 +38,26 @@ class TestNsinstall(unittest.TestCase):
 
     def test_nsinstall_basic(self):
         "Test nsinstall <file> <dir>"
         testfile = self.touch("testfile")
         testdir = self.mkdirs("testdir")
         self.assertEqual(nsinstall([testfile, testdir]), 0)
         self.assert_(os.path.isfile(os.path.join(testdir, "testfile")))
 
+    def test_nsinstall_basic_recursive(self):
+        "Test nsinstall <dir> <dest dir>"
+        sourcedir = self.mkdirs("sourcedir")
+        self.touch("testfile", sourcedir)
+        destdir = self.mkdirs("destdir")
+        self.assertEqual(nsinstall([sourcedir, destdir]), 0)
+        testdir = os.path.join(destdir, "sourcedir")
+        self.assert_(os.path.isdir(testdir))
+        self.assert_(os.path.isfile(os.path.join(testdir, "testfile")))
+
     def test_nsinstall_multiple(self):
         "Test nsinstall <three files> <dest dir>"
         testfiles = [self.touch("testfile1"),
                      self.touch("testfile2"),
                      self.touch("testfile3")]
         testdir = self.mkdirs("testdir")
         self.assertEqual(nsinstall(testfiles + [testdir]), 0)
         for f in testfiles: