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: if len(args) < 2:
p.error('not enough arguments') 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 # set up handler
if options.d: if options.d:
# we're supposed to create directories # we're supposed to create directories
def handleTarget(srcpath, targetpath): def handleTarget(srcpath, targetpath):
# target directory was already created, just use mkdir # target directory was already created, just use mkdir
os.mkdir(dest) os.mkdir(targetpath)
else: else:
# we're supposed to copy files # we're supposed to copy files
def handleTarget(srcpath, targetpath): 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) shutil.copy2(srcpath, targetpath)
else: else:
shutil.copy(srcpath, targetpath) shutil.copy(srcpath, targetpath)
@ -132,12 +147,7 @@ def nsinstall(argv):
if not os.path.isdir(target): if not os.path.isdir(target):
os.makedirs(target) os.makedirs(target)
for f in args: copy_all_entries(args, target)
dest = os.path.join(target,
os.path.basename(os.path.normpath(f)))
handleTarget(f, dest)
if options.m:
os.chmod(dest, options.m)
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':

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

@ -43,6 +43,16 @@ class TestNsinstall(unittest.TestCase):
self.assertEqual(nsinstall([testfile, testdir]), 0) self.assertEqual(nsinstall([testfile, testdir]), 0)
self.assert_(os.path.isfile(os.path.join(testdir, "testfile"))) 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): def test_nsinstall_multiple(self):
"Test nsinstall <three files> <dest dir>" "Test nsinstall <three files> <dest dir>"
testfiles = [self.touch("testfile1"), testfiles = [self.touch("testfile1"),