Fix handling of dangling symlink

There may be broken symlinks within the log directories, those fail with
an error when os.stat() is executed on them. So if/else is replaced with
try/except while TypeError used to catch when self.full_path is None

Change-Id: Iffee97760a39fa4f7760bd67fb63c5f0905064bd
This commit is contained in:
Dmitriy Rabotyagov 2019-08-26 19:57:57 +03:00
parent 8e3fec5388
commit fa2cbeaae2
2 changed files with 32 additions and 3 deletions

View File

@ -20,10 +20,12 @@ __metaclass__ = type
import os
import testtools
import time
import stat
import fixtures
from bs4 import BeautifulSoup
from .zuul_swift_upload import FileList, Indexer
from .zuul_swift_upload import FileList, Indexer, FileDetail
FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
@ -357,3 +359,24 @@ class TestFileList(testtools.TestCase):
self.assertEqual(rows[0].find('a').get('href'), 'subdir.txt')
self.assertEqual(rows[0].find('a').text, 'subdir.txt')
class TestFileDetail(testtools.TestCase):
def test_get_file_detail(self):
'''Test files info'''
path = os.path.join(FIXTURE_DIR, 'logs/job-output.json')
file_detail = FileDetail(path, '')
path_stat = os.stat(path)
self.assertEqual(
time.gmtime(path_stat[stat.ST_MTIME]),
file_detail.last_modified)
self.assertEqual(16, file_detail.size)
def test_get_file_detail_missing_file(self):
'''Test files that go missing during a walk'''
file_detail = FileDetail('missing/file/that/we/cant/find', '')
self.assertEqual(time.gmtime(0), file_detail.last_modified)
self.assertEqual(0, file_detail.size)

View File

@ -177,6 +177,12 @@ class FileDetail():
used for links.
filename (str): An optional alternate filename in links.
"""
# Make FileNotFoundError exception to be compatible with python2
try:
FileNotFoundError # noqa: F823
except NameError:
FileNotFoundError = OSError
self.full_path = full_path
if filename is None:
self.filename = os.path.basename(full_path)
@ -193,11 +199,11 @@ class FileDetail():
self.mimetype = 'application/directory'
self.encoding = None
self.folder = True
if self.full_path:
try:
st = os.stat(self.full_path)
self.last_modified = time.gmtime(st[stat.ST_MTIME])
self.size = st[stat.ST_SIZE]
else:
except (FileNotFoundError, TypeError):
self.last_modified = time.gmtime(0)
self.size = 0