bug 482733 - nsinstall.py should support copying directories recursively. r=pike

This commit is contained in:
Ted Mielczarek 2009-03-12 08:46:38 -04:00
Родитель 8d11ca53eb
Коммит b14eef496c
2 изменённых файлов: 28 добавлений и 8 удалений

Просмотреть файл

@ -112,16 +112,31 @@ def nsinstall(argv):
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)
@ -132,12 +147,7 @@ def nsinstall(argv):
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__':

Просмотреть файл

@ -43,6 +43,16 @@ class TestNsinstall(unittest.TestCase):
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"),