Commit a657b371 authored by Cyril Roelandt's avatar Cyril Roelandt
Browse files

upt.Archive: add a filename attribute.

This is a nice helper attribute for backends. Otherwise, they would have
to use os.path.basename on Archive.filepath.
parent 24fbbfc8
......@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
Based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- upt.Archive: add a "filename" attribute
## [0.6] - 2019-02-08
### Added
- upt.Archive: a representation of archive files, such as source tarballs
......
......@@ -254,15 +254,23 @@ class TestArchive(unittest.TestCase):
archive._filepath = '/fake/path'
self.assertEqual(archive.size, 12)
@mock.patch('urllib.request.urlretrieve', return_value=('/path', None))
def test_filepath(self, urlretrieve_fn):
archive = upt.Archive('url')
self.assertEqual(archive.filepath, '/path')
def test_filename(self):
archive = upt.Archive('http://example.com/source.tar.gz')
self.assertEqual(archive.filename, 'source.tar.gz')
@mock.patch('urllib.request.urlretrieve',
return_value=('/tmpdir/source.tar.gz', None))
@mock.patch('tempfile.gettempdir', return_value='/tmpdir')
def test_filepath(self, gettempdir_fn, urlretrieve_fn):
archive = upt.Archive('http://example.com/source.tar.gz')
self.assertEqual(archive.filepath, '/tmpdir/source.tar.gz')
@mock.patch('upt.checksum.compute_checksum', return_value='hash-output')
@mock.patch('urllib.request.urlretrieve', return_value=('/path', None))
def test_checksum(self, urlretrieve_fn, compute_checksum_fn):
archive = upt.Archive('url')
@mock.patch('urllib.request.urlretrieve',
return_value=('/tmpdir/source.tar.gz', None))
@mock.patch('tempfile.gettempdir', return_value='/tmpdir')
def test_checksum(self, gettmpdir_fn, urlretrieve_fn, compute_checksum_fn):
archive = upt.Archive('http://example.com/source.tar.gz')
self.assertEqual(archive._checksum('fake-hash'), 'hash-output')
self.assertEqual(archive._checksum('fake-hash'), 'hash-output')
urlretrieve_fn.assert_called_once()
......
......@@ -7,6 +7,7 @@ import logging
import os
import urllib.request
import sys
import tempfile
import pkg_resources
......@@ -43,7 +44,8 @@ class Archive(object):
self.url = url
self.archive_type = archive_type
self._size = size
self._filepath = None
self._filepath = None # Absolute path on the local filesystem
self._filename = None # Filename on the local filesystem
self._hashes = {}
if md5 is not None:
self.md5 = md5
......@@ -55,9 +57,16 @@ class Archive(object):
@property
def filepath(self):
if self._filepath is None:
self._filepath, _ = urllib.request.urlretrieve(self.url)
self._filepath = os.path.join(tempfile.gettempdir(), self.filename)
urllib.request.urlretrieve(self.url, filename=self._filepath)
return self._filepath
@property
def filename(self):
if self._filename is None:
self._filename = os.path.basename(self.url)
return self._filename
@property
def size(self):
if self._size == 0:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment