diff options
| author | Tarek Ziad? <tarek@ziade.org> | 2010-02-22 18:34:56 -0500 |
|---|---|---|
| committer | Tarek Ziad? <tarek@ziade.org> | 2010-02-22 18:34:56 -0500 |
| commit | 6905c500d6b0042c3718a22007d0eb9bcf0007e5 (patch) | |
| tree | 3e12f4d71da81056a7ea2d73ecd9499ccd818950 /src | |
| parent | 7783b45b2e45dd5d227a24949352ccf82f75ecc8 (diff) | |
| download | disutils2-6905c500d6b0042c3718a22007d0eb9bcf0007e5.tar.gz | |
removed file_util
Diffstat (limited to 'src')
| -rw-r--r-- | src/distutils2/cmd.py | 26 | ||||
| -rw-r--r-- | src/distutils2/command/bdist_rpm.py | 2 | ||||
| -rw-r--r-- | src/distutils2/command/install.py | 2 | ||||
| -rw-r--r-- | src/distutils2/command/sdist.py | 4 | ||||
| -rw-r--r-- | src/distutils2/compiler/ccompiler.py | 6 | ||||
| -rw-r--r-- | src/distutils2/compiler/cygwinccompiler.py | 2 | ||||
| -rw-r--r-- | src/distutils2/compiler/emxccompiler.py | 2 | ||||
| -rw-r--r-- | src/distutils2/file_util.py | 238 | ||||
| -rw-r--r-- | src/distutils2/tests/test_file_util.py | 78 | ||||
| -rw-r--r-- | src/distutils2/util.py | 9 |
10 files changed, 33 insertions, 336 deletions
diff --git a/src/distutils2/cmd.py b/src/distutils2/cmd.py index 4bcba4b..160b04c 100644 --- a/src/distutils2/cmd.py +++ b/src/distutils2/cmd.py @@ -8,11 +8,11 @@ __revision__ = "$Id: cmd.py 75192 2009-10-02 23:49:48Z tarek.ziade $" import sys, os, re from distutils2.errors import DistutilsOptionError -from distutils2 import util, file_util +from distutils2 import util from distutils2 import log # XXX see if we want to backport this -from distutils2._backport.shutil import copytree +from distutils2._backport.shutil import copytree, copyfile, move try: from shutil import make_archive @@ -373,13 +373,13 @@ class Command: """Copy a file respecting verbose, dry-run and force flags. (The former two default to whatever is in the Distribution object, and the latter defaults to false for commands that don't define it.)""" - - return file_util.copy_file( - infile, outfile, - preserve_mode, preserve_times, - not self.force, - link, - dry_run=self.dry_run) + if self.dry_run: + # XXX add a comment + return + if os.path.isdir(outfile): + outfile = os.path.join(outfile, os.path.split(infile)[-1]) + copyfile(infile, outfile) + return outfile, None # XXX def copy_tree(self, infile, outfile, preserve_mode=1, preserve_times=1, preserve_symlinks=0, @@ -391,11 +391,13 @@ class Command: return # see if we want to display something return copytree(infile, outfile, preserve_symlinks) - def move_file (self, src, dst, level=1): + def move_file(self, src, dst, level=1): """Move a file respectin dry-run flag.""" - return file_util.move_file(src, dst, dry_run = self.dry_run) + if self.dry_run: + return # XXX log ? + return move(src, dst) - def spawn (self, cmd, search_path=1, level=1): + def spawn(self, cmd, search_path=1, level=1): """Spawn an external command respecting dry-run flag.""" from distutils2.spawn import spawn spawn(cmd, search_path, dry_run= self.dry_run) diff --git a/src/distutils2/command/bdist_rpm.py b/src/distutils2/command/bdist_rpm.py index 6d9c7ad..ea7fd87 100644 --- a/src/distutils2/command/bdist_rpm.py +++ b/src/distutils2/command/bdist_rpm.py @@ -11,7 +11,7 @@ import string from distutils2.core import Command from distutils2.debug import DEBUG -from distutils2.file_util import write_file +from distutils2.util import write_file from distutils2.errors import (DistutilsOptionError, DistutilsPlatformError, DistutilsFileError, DistutilsExecError) from distutils2 import log diff --git a/src/distutils2/command/install.py b/src/distutils2/command/install.py index c532b2a..cc0e2e4 100644 --- a/src/distutils2/command/install.py +++ b/src/distutils2/command/install.py @@ -16,7 +16,7 @@ from distutils2 import log from distutils2.core import Command from distutils2.debug import DEBUG from distutils2.errors import DistutilsPlatformError -from distutils2.file_util import write_file +from distutils2.util import write_file from distutils2.util import convert_path, change_root, get_platform from distutils2.errors import DistutilsOptionError diff --git a/src/distutils2/command/sdist.py b/src/distutils2/command/sdist.py index 9a3ee7f..70b888f 100644 --- a/src/distutils2/command/sdist.py +++ b/src/distutils2/command/sdist.py @@ -16,7 +16,7 @@ except ImportError: from distutils2._backport.shutil import get_archive_formats from distutils2.core import Command -from distutils2 import file_util +from distutils2 import util from distutils2.text_file import TextFile from distutils2.errors import (DistutilsPlatformError, DistutilsOptionError, DistutilsTemplateError) @@ -390,7 +390,7 @@ class sdist(Command): by 'add_defaults()' and 'read_template()') to the manifest file named by 'self.manifest'. """ - self.execute(file_util.write_file, + self.execute(util.write_file, (self.manifest, self.filelist.files), "writing manifest file '%s'" % self.manifest) diff --git a/src/distutils2/compiler/ccompiler.py b/src/distutils2/compiler/ccompiler.py index ecf11e3..025febd 100644 --- a/src/distutils2/compiler/ccompiler.py +++ b/src/distutils2/compiler/ccompiler.py @@ -12,9 +12,9 @@ import re from distutils2.errors import (CompileError, LinkError, UnknownFileError, DistutilsPlatformError, DistutilsModuleError) from distutils2.spawn import spawn -from distutils2.file_util import move_file from distutils2.util import split_quoted, execute, newer_group from distutils2 import log +from shutil import move try: import sysconfig @@ -929,7 +929,9 @@ main (int argc, char **argv) { spawn(cmd, dry_run=self.dry_run) def move_file(self, src, dst): - return move_file(src, dst, dry_run=self.dry_run) + if self.dry_run: + return # XXX log ? + return move(src, dst) def mkpath(self, name, mode=0777): name = os.path.normpath(name) diff --git a/src/distutils2/compiler/cygwinccompiler.py b/src/distutils2/compiler/cygwinccompiler.py index 2af3b6a..410288f 100644 --- a/src/distutils2/compiler/cygwinccompiler.py +++ b/src/distutils2/compiler/cygwinccompiler.py @@ -54,7 +54,7 @@ import re from warnings import warn from distutils2.compiler.unixccompiler import UnixCCompiler -from distutils2.file_util import write_file +from distutils2.util import write_file from distutils2.errors import DistutilsExecError, CompileError, UnknownFileError from distutils2.util import get_compiler_versions try: diff --git a/src/distutils2/compiler/emxccompiler.py b/src/distutils2/compiler/emxccompiler.py index e03377b..cb6fb16 100644 --- a/src/distutils2/compiler/emxccompiler.py +++ b/src/distutils2/compiler/emxccompiler.py @@ -25,7 +25,7 @@ import os, sys, copy from warnings import warn from distutils2.compiler.unixccompiler import UnixCCompiler -from distutils2.file_util import write_file +from distutils2.util import write_file from distutils2.errors import DistutilsExecError, CompileError, UnknownFileError from distutils2.util import get_compiler_versions diff --git a/src/distutils2/file_util.py b/src/distutils2/file_util.py deleted file mode 100644 index 54c96f3..0000000 --- a/src/distutils2/file_util.py +++ /dev/null @@ -1,238 +0,0 @@ -"""distutils.file_util - -Utility functions for operating on single files. -""" - -__revision__ = "$Id: file_util.py 73815 2009-07-03 19:14:49Z tarek.ziade $" - -import os -from distutils2.errors import DistutilsFileError -from distutils2 import log - -# for generating verbose output in 'copy_file()' -_copy_action = {None: 'copying', - 'hard': 'hard linking', - 'sym': 'symbolically linking'} - - -def _copy_file_contents(src, dst, buffer_size=16*1024): - """Copy the file 'src' to 'dst'. - - Both must be filenames. Any error opening either file, reading from - 'src', or writing to 'dst', raises DistutilsFileError. Data is - 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 - # custom error-handling added. - fsrc = None - fdst = None - try: - try: - fsrc = open(src, 'rb') - except os.error, (errno, errstr): - raise DistutilsFileError("could not open '%s': %s" % (src, errstr)) - - if os.path.exists(dst): - try: - os.unlink(dst) - except os.error, (errno, errstr): - raise DistutilsFileError( - "could not delete '%s': %s" % (dst, errstr)) - - try: - fdst = open(dst, 'wb') - except os.error, (errno, errstr): - raise DistutilsFileError( - "could not create '%s': %s" % (dst, errstr)) - - while 1: - try: - buf = fsrc.read(buffer_size) - except os.error, (errno, errstr): - raise DistutilsFileError( - "could not read from '%s': %s" % (src, errstr)) - - if not buf: - break - - try: - fdst.write(buf) - except os.error, (errno, errstr): - raise DistutilsFileError( - "could not write to '%s': %s" % (dst, errstr)) - - finally: - if fdst: - fdst.close() - if fsrc: - fsrc.close() - -def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, - link=None, verbose=1, dry_run=0): - """Copy a file 'src' to 'dst'. - - If 'dst' is a directory, then 'src' is copied there with the same name; - otherwise, it must be a filename. (If the file exists, it will be - ruthlessly clobbered.) If 'preserve_mode' is true (the default), - the file's mode (type and permission bits, or whatever is analogous on - the current platform) is copied. If 'preserve_times' is true (the - default), the last-modified and last-access times are copied as well. - 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 - (os.symlink) instead of copying: set it to "hard" or "sym"; if it is - None (the default), files are copied. Don't set 'link' on systems that - don't support it: 'copy_file()' doesn't check if hard or symbolic - linking is available. - - Under Mac OS, uses the native file copy function in macostools; on - other systems, uses '_copy_file_contents()' to copy file contents. - - Return a tuple (dest_name, copied): 'dest_name' is the actual name of - the output file, and 'copied' is true if the file was copied (or would - have been copied, if 'dry_run' true). - """ - # XXX if the destination file already exists, we clobber it if - # copying, but blow up if linking. Hmmm. And I don't know what - # macostools.copyfile() does. Should definitely be consistent, and - # should probably blow up if destination exists and we would be - # changing it (ie. it's not already a hard/soft link to src OR - # (not update) and (src newer than dst). - - from distutils2.util import newer - from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE - - if not os.path.isfile(src): - raise DistutilsFileError( - "can't copy '%s': doesn't exist or not a regular file" % src) - - if os.path.isdir(dst): - dir = dst - dst = os.path.join(dst, os.path.basename(src)) - else: - dir = os.path.dirname(dst) - - if update and not newer(src, dst): - if verbose >= 1: - log.debug("not copying %s (output up-to-date)", src) - return dst, 0 - - try: - action = _copy_action[link] - except KeyError: - raise ValueError("invalid value '%s' for 'link' argument" % link) - - if verbose >= 1: - if os.path.basename(dst) == os.path.basename(src): - log.info("%s %s -> %s", action, src, dir) - else: - log.info("%s %s -> %s", action, src, dst) - - if dry_run: - return (dst, 1) - - # On Mac OS, use the native file copy routine - if os.name == 'mac': - import macostools - try: - macostools.copy(src, dst, 0, preserve_times) - except os.error, exc: - raise DistutilsFileError( - "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])) - - # If linking (hard or symbolic), use the appropriate system call - # (Unix only, of course, but that's the caller's responsibility) - elif link == 'hard': - if not (os.path.exists(dst) and os.path.samefile(src, dst)): - os.link(src, dst) - elif link == 'sym': - if not (os.path.exists(dst) and os.path.samefile(src, dst)): - os.symlink(src, dst) - - # Otherwise (non-Mac, not linking), copy the file contents and - # (optionally) copy the times and mode. - else: - _copy_file_contents(src, dst) - if preserve_mode or preserve_times: - st = os.stat(src) - - # According to David Ascher <da@ski.org>, utime() should be done - # before chmod() (at least under NT). - if preserve_times: - os.utime(dst, (st[ST_ATIME], st[ST_MTIME])) - if preserve_mode: - os.chmod(dst, S_IMODE(st[ST_MODE])) - - return (dst, 1) - -# XXX I suspect this is Unix-specific -- need porting help! -def move_file (src, dst, verbose=1, dry_run=0): - """Move a file 'src' to 'dst'. - - If 'dst' is a directory, the file will be moved into it with the same - name; otherwise, 'src' is just renamed to 'dst'. Return the new - full name of the file. - - Handles cross-device moves on Unix using 'copy_file()'. What about - other systems??? - """ - from os.path import exists, isfile, isdir, basename, dirname - import errno - - if verbose >= 1: - log.info("moving %s -> %s", src, dst) - - if dry_run: - return dst - - if not isfile(src): - raise DistutilsFileError("can't move '%s': not a regular file" % src) - - if isdir(dst): - dst = os.path.join(dst, basename(src)) - elif exists(dst): - raise DistutilsFileError( - "can't move '%s': destination '%s' already exists" % - (src, dst)) - - if not isdir(dirname(dst)): - raise DistutilsFileError( - "can't move '%s': destination '%s' not a valid path" % \ - (src, dst)) - - copy_it = 0 - try: - os.rename(src, dst) - except os.error, (num, msg): - if num == errno.EXDEV: - copy_it = 1 - else: - raise DistutilsFileError( - "couldn't move '%s' to '%s': %s" % (src, dst, msg)) - - if copy_it: - copy_file(src, dst, verbose=verbose) - try: - os.unlink(src) - except os.error, (num, msg): - try: - os.unlink(dst) - except os.error: - pass - raise DistutilsFileError( - ("couldn't move '%s' to '%s' by copy/delete: " + - "delete '%s' failed: %s") % - (src, dst, src, msg)) - return dst - - -def write_file (filename, contents): - """Create a file with the specified name and write 'contents' (a - sequence of strings without line terminators) to it. - """ - f = open(filename, "w") - for line in contents: - f.write(line + "\n") - f.close() diff --git a/src/distutils2/tests/test_file_util.py b/src/distutils2/tests/test_file_util.py deleted file mode 100644 index 269db03..0000000 --- a/src/distutils2/tests/test_file_util.py +++ /dev/null @@ -1,78 +0,0 @@ -"""Tests for distutils.file_util.""" -import unittest2 -import os -import shutil - -from distutils2.file_util import move_file, write_file, copy_file -from distutils2 import log -from distutils2.tests import support - -class FileUtilTestCase(support.TempdirManager, unittest2.TestCase): - - def _log(self, msg, *args): - if len(args) > 0: - self._logs.append(msg % args) - else: - self._logs.append(msg) - - def setUp(self): - super(FileUtilTestCase, self).setUp() - self._logs = [] - self.old_log = log.info - log.info = self._log - tmp_dir = self.mkdtemp() - self.source = os.path.join(tmp_dir, 'f1') - self.target = os.path.join(tmp_dir, 'f2') - self.target_dir = os.path.join(tmp_dir, 'd1') - - def tearDown(self): - log.info = self.old_log - super(FileUtilTestCase, self).tearDown() - - def test_move_file_verbosity(self): - f = open(self.source, 'w') - f.write('some content') - f.close() - - move_file(self.source, self.target, verbose=0) - wanted = [] - self.assertEquals(self._logs, wanted) - - # back to original state - move_file(self.target, self.source, verbose=0) - - move_file(self.source, self.target, verbose=1) - wanted = ['moving %s -> %s' % (self.source, self.target)] - self.assertEquals(self._logs, wanted) - - # back to original state - move_file(self.target, self.source, verbose=0) - - self._logs = [] - # now the target is a dir - os.mkdir(self.target_dir) - move_file(self.source, self.target_dir, verbose=1) - wanted = ['moving %s -> %s' % (self.source, self.target_dir)] - 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(): - return unittest2.makeSuite(FileUtilTestCase) - -if __name__ == "__main__": - unittest2.main(defaultTest="test_suite") diff --git a/src/distutils2/util.py b/src/distutils2/util.py index adeb802..545e2fe 100644 --- a/src/distutils2/util.py +++ b/src/distutils2/util.py @@ -539,3 +539,12 @@ def newer_group(sources, target, missing='error'): return False +def write_file(filename, contents): + """Create a file with the specified name and write 'contents' (a + sequence of strings without line terminators) to it. + """ + f = open(filename, "w") + for line in contents: + f.write(line + "\n") + f.close() + |
