Kaydet (Commit) c2b71889 authored tarafından Tarek Ziadé's avatar Tarek Ziadé

Merged revisions 73814-73815 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r73814 | tarek.ziade | 2009-07-03 21:01:12 +0200 (Fri, 03 Jul 2009) | 1 line

  basic tests to raise distutils.file_util coverage
........
  r73815 | tarek.ziade | 2009-07-03 21:14:49 +0200 (Fri, 03 Jul 2009) | 1 line

  cleaned distutils.file_util
........
üst 89b89170
...@@ -10,17 +10,18 @@ from distutils.errors import DistutilsFileError ...@@ -10,17 +10,18 @@ from distutils.errors import DistutilsFileError
from distutils import log from distutils import log
# for generating verbose output in 'copy_file()' # for generating verbose output in 'copy_file()'
_copy_action = { None: 'copying', _copy_action = {None: 'copying',
'hard': 'hard linking', 'hard': 'hard linking',
'sym': 'symbolically linking' } 'sym': 'symbolically linking'}
def _copy_file_contents(src, dst, buffer_size=16*1024): def _copy_file_contents(src, dst, buffer_size=16*1024):
"""Copy the file 'src' to 'dst'; both must be filenames. Any error """Copy the file 'src' to 'dst'.
opening either file, reading from 'src', or writing to 'dst', raises
DistutilsFileError. Data is read/written in chunks of 'buffer_size' Both must be filenames. Any error opening either file, reading from
bytes (default 16k). No attempt is made to handle anything apart from 'src', or writing to 'dst', raises DistutilsFileError. Data is
regular files. read/written in chunks of 'buffer_size' bytes (default 16k). No attempt
is made to handle anything apart from regular files.
""" """
# Stolen from shutil module in the standard library, but with # Stolen from shutil module in the standard library, but with
# custom error-handling added. # custom error-handling added.
...@@ -68,15 +69,16 @@ def _copy_file_contents(src, dst, buffer_size=16*1024): ...@@ -68,15 +69,16 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
link=None, verbose=1, dry_run=0): link=None, verbose=1, dry_run=0):
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is """Copy a file 'src' to 'dst'.
copied there with the same name; otherwise, it must be a filename. (If
the file exists, it will be ruthlessly clobbered.) If 'preserve_mode' If 'dst' is a directory, then 'src' is copied there with the same name;
is true (the default), the file's mode (type and permission bits, or otherwise, it must be a filename. (If the file exists, it will be
whatever is analogous on the current platform) is copied. If ruthlessly clobbered.) If 'preserve_mode' is true (the default),
'preserve_times' is true (the default), the last-modified and the file's mode (type and permission bits, or whatever is analogous on
last-access times are copied as well. If 'update' is true, 'src' will the current platform) is copied. If 'preserve_times' is true (the
only be copied if 'dst' does not exist, or if 'dst' does exist but is default), the last-modified and last-access times are copied as well.
older than 'src'. If 'update' is true, 'src' will only be copied if 'dst' does not exist,
or if 'dst' does exist but is older than 'src'.
'link' allows you to make hard links (os.link) or symbolic links 'link' allows you to make hard links (os.link) or symbolic links
(os.symlink) instead of copying: set it to "hard" or "sym"; if it is (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
...@@ -166,13 +168,12 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, ...@@ -166,13 +168,12 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
# XXX I suspect this is Unix-specific -- need porting help! # XXX I suspect this is Unix-specific -- need porting help!
def move_file (src, dst, def move_file(src, dst, verbose=1, dry_run=0):
verbose=1, """Move a file 'src' to 'dst'.
dry_run=0):
"""Move a file 'src' to 'dst'. If 'dst' is a directory, the file will If 'dst' is a directory, the file will be moved into it with the same
be moved into it with the same name; otherwise, 'src' is just renamed name; otherwise, 'src' is just renamed to 'dst'. Return the new
to 'dst'. Return the new full name of the file. full name of the file.
Handles cross-device moves on Unix using 'copy_file()'. What about Handles cross-device moves on Unix using 'copy_file()'. What about
other systems??? other systems???
...@@ -229,7 +230,7 @@ def move_file (src, dst, ...@@ -229,7 +230,7 @@ def move_file (src, dst,
return dst return dst
def write_file (filename, contents): def write_file(filename, contents):
"""Create a file with the specified name and write 'contents' (a """Create a file with the specified name and write 'contents' (a
sequence of strings without line terminators) to it. sequence of strings without line terminators) to it.
""" """
......
...@@ -3,7 +3,7 @@ import unittest ...@@ -3,7 +3,7 @@ import unittest
import os import os
import shutil import shutil
from distutils.file_util import move_file from distutils.file_util import move_file, write_file, copy_file
from distutils import log from distutils import log
from distutils.tests import support from distutils.tests import support
...@@ -55,6 +55,21 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase): ...@@ -55,6 +55,21 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
wanted = ['moving %s -> %s' % (self.source, self.target_dir)] wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
self.assertEquals(self._logs, wanted) self.assertEquals(self._logs, wanted)
def test_write_file(self):
lines = ['a', 'b', 'c']
dir = self.mkdtemp()
foo = os.path.join(dir, 'foo')
write_file(foo, lines)
content = [line.strip() for line in open(foo).readlines()]
self.assertEquals(content, lines)
def test_copy_file(self):
src_dir = self.mkdtemp()
foo = os.path.join(src_dir, 'foo')
write_file(foo, 'content')
dst_dir = self.mkdtemp()
copy_file(foo, dst_dir)
self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo')))
def test_suite(): def test_suite():
return unittest.makeSuite(FileUtilTestCase) return unittest.makeSuite(FileUtilTestCase)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment