Éric Araujo avatar Éric Araujo committed 28b1840

Basic code for util.read_file

Comments (0)

Files changed (2)

src/distutils2/tests/test_util.py

 from distutils2.util import (_MAC_OS_X_LD_VERSION, _nt_quote_args,
                              byte_compile, change_root, convert_path,
                              find_packages, get_compiler_versions,
-                             resolve_name, rfc822_escape, spawn,
+                             read_file, resolve_name, rfc822_escape, spawn,
                              split_quoted, strtobool)
 
 from distutils2 import util
         os.chmod(exe, 0777)
         spawn([exe])  # should work without any error
 
+    def test_read_file(self):
+        # torture-test read_file
+        basedir = self.mkdtemp()
+        fn1 = os.path.join(basedir, 'test1')
+        os.chdir(basedir)
+        self.write_file(fn, 'success1')
+
+        # test basic use for a normal case
+        self.assertEqual(read_file(fn1), 'success1')
+        self.assertEqual(read_file(fn1, check=True), 'success1')
+        self.assertRaises(DistutilsFileError, read_file, fn1, relative=True)
+        self.assertEqual(read_file('test1', relative=True), 'success1')
+        self.assertEqual(read_file('test1', check=True, relative=True),
+                                   'success1')
+        self.assertEqual(read_file('test1', encoding='ascii',
+                                   check=True, relative=True), 'success1')
+
+        # TODO existing but non-readable
+
 
 def test_suite():
     return unittest.makeSuite(UtilTestCase)

src/distutils2/util.py

 __revision__ = "$Id: util.py 77761 2010-01-26 22:46:15Z tarek.ziade $"
 
 import os
+import sys
+import codecs
 import posixpath
 import re
 import string
-import sys
 import shutil
 import tarfile
 import zipfile
     return False
 
 
-def read_file(filename, safe=False):
-    pass
+def read_file(path, encoding='utf-8', check=True, relative=False,
+              dry_run=False):
+    path = os.path.normpath(path)
+    # Refuse absolute or parent paths if relative is true
+    if relative:
+        if os.path.isabs(path) or path.startswith(os.pardir):
+            raise DistutilsFileError('path must be relative (got %r)' % path)
+    try:
+        try:
+            fp = codecs.open(path, encoding=encoding)
+            content = fp.read()
+        except EnvironmentError, exc:
+            # Let errors raise only if check is true
+            if check:
+                raise DistutilsFileError(exc)
+    finally:
+        fp.close()
+
+    return content
 
 
 def write_file(filename, contents):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.