Add get_size() method for local files.

This commit is contained in:
Bastian Kleineidam 2010-07-29 19:52:26 +02:00
parent 7536472797
commit 7d076e7129
3 changed files with 49 additions and 0 deletions

View file

@ -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

View file

@ -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]

38
tests/test_fileutil.py Normal file
View file

@ -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)