From 7d076e7129986ea4f43b7851e4829f702edcbf9e Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Thu, 29 Jul 2010 19:52:26 +0200 Subject: [PATCH] Add get_size() method for local files. --- doc/changelog.txt | 3 +++ linkcheck/fileutil.py | 8 ++++++++ tests/test_fileutil.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/test_fileutil.py diff --git a/doc/changelog.txt b/doc/changelog.txt index 41d866f4..828dc6e1 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -14,6 +14,9 @@ Changes: - logging: Use more memory-efficient wire-format for UrlBase, using __slots__. Closes: SF bug #2976995 +- checking: Get size from Content-Length HTTP header, from stat(2) + for local files so size information is available without downloading + the content data. Features: - ftp: Detect and support UTF-8 filename encoding capability of FTP diff --git a/linkcheck/fileutil.py b/linkcheck/fileutil.py index 14718b55..b76fc86e 100644 --- a/linkcheck/fileutil.py +++ b/linkcheck/fileutil.py @@ -138,6 +138,14 @@ def get_mtime (filename): return 0 +def get_size (filename): + """Return file size in Bytes, or -1 on error.""" + try: + return os.stat(filename)[stat.ST_SIZE] + except os.error: + return -1 + + # http://developer.gnome.org/doc/API/2.0/glib/glib-running.html if "G_FILENAME_ENCODING" in os.environ: FSCODING = os.environ["G_FILENAME_ENCODING"].split(",")[0] diff --git a/tests/test_fileutil.py b/tests/test_fileutil.py new file mode 100644 index 00000000..7e27665d --- /dev/null +++ b/tests/test_fileutil.py @@ -0,0 +1,38 @@ +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2010 Bastian Kleineidam +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Test file utility functions. +""" + +import unittest +import linkcheck.fileutil + +file_existing = __file__ +file_non_existing = "XXX.i_dont_exist" + +class TestFileutil (unittest.TestCase): + """Test file utility functions.""" + + def test_size (self): + self.assertTrue(linkcheck.fileutil.get_size(file_existing) > 0) + self.assertEqual(linkcheck.fileutil.get_size(file_non_existing), -1) + + + def test_mtime (self): + filename = __file__ + self.assertTrue(linkcheck.fileutil.get_mtime(file_existing) > 0) + self.assertEqual(linkcheck.fileutil.get_mtime(file_non_existing), 0)