Skip to content

Commit

Permalink
Merge pull request #55191 from brejoc/master-list-downloaded
Browse files Browse the repository at this point in the history
list_downloaded for apt Module
  • Loading branch information
dwoz committed Jan 6, 2020
2 parents 8bcf845 + ea184fb commit 4fed7e0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
41 changes: 41 additions & 0 deletions salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import re
import logging
import time
import fnmatch
import datetime


# Import third party libs
# pylint: disable=no-name-in-module,import-error,redefined-builtin
Expand Down Expand Up @@ -384,6 +387,7 @@ def install(name=None,
pkgs=None,
sources=None,
reinstall=False,
downloadonly=False,
ignore_epoch=False,
**kwargs):
'''
Expand Down Expand Up @@ -730,6 +734,9 @@ def install(name=None,
cmd.extend(downgrade)
cmds.append(cmd)

if downloadonly:
cmd.append("--download-only")

if to_reinstall:
all_pkgs.extend(to_reinstall)
cmd = copy.deepcopy(cmd_prefix)
Expand Down Expand Up @@ -2846,3 +2853,37 @@ def _get_http_proxy_url():
)

return http_proxy_url


def list_downloaded(root=None, **kwargs):
'''
.. versionadded:: 3000?
List prefetched packages downloaded by apt in the local disk.
root
operate on a different root directory.
CLI example:
.. code-block:: bash
salt '*' pkg.list_downloaded
'''
CACHE_DIR = '/var/cache/apt'
if root:
CACHE_DIR = os.path.join(root, os.path.relpath(CACHE_DIR, os.path.sep))

ret = {}
for root, dirnames, filenames in salt.utils.path.os_walk(CACHE_DIR):
for filename in fnmatch.filter(filenames, '*.deb'):
package_path = os.path.join(root, filename)
pkg_info = __salt__['lowpkg.bin_pkg_info'](package_path)
pkg_timestamp = int(os.path.getctime(package_path))
ret.setdefault(pkg_info['name'], {})[pkg_info['version']] = {
'path': package_path,
'size': os.path.getsize(package_path),
'creation_date_time_t': pkg_timestamp,
'creation_date_time': datetime.datetime.utcfromtimestamp(pkg_timestamp).isoformat(),
}
return ret
4 changes: 2 additions & 2 deletions salt/states/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ def downloaded(name,
(if specified).
Currently supported for the following pkg providers:
:mod:`yumpkg <salt.modules.yumpkg>` and :mod:`zypper <salt.modules.zypper>`
:mod:`yumpkg <salt.modules.yumpkg>`, :mod:`zypper <salt.modules.zypper>` and :mod:`zypper <salt.modules.aptpkg>`
:param str name:
The name of the package to be downloaded. This parameter is ignored if
Expand Down Expand Up @@ -2174,7 +2174,7 @@ def downloaded(name,

if not ret['changes'] and not ret['comment']:
ret['result'] = True
ret['comment'] = 'Packages are already downloaded: ' \
ret['comment'] = 'Packages downloaded: ' \
'{0}'.format(', '.join(targets))

return ret
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/modules/test_aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,32 @@ def test_show(self):
self.assert_called_once(refresh_mock)
refresh_mock.reset_mock()

@patch('salt.utils.path.os_walk', MagicMock(return_value=[('test', 'test', 'test')]))
@patch('os.path.getsize', MagicMock(return_value=123456))
@patch('os.path.getctime', MagicMock(return_value=1234567890.123456))
@patch('fnmatch.filter', MagicMock(return_value=['/var/cache/apt/archive/test_package.rpm']))
def test_list_downloaded(self):
'''
Test downloaded packages listing.
:return:
'''
DOWNLOADED_RET = {
'test-package': {
'1.0': {
'path': '/var/cache/apt/archive/test_package.rpm',
'size': 123456,
'creation_date_time_t': 1234567890,
'creation_date_time': '2009-02-13T23:31:30',
}
}
}

with patch.dict(aptpkg.__salt__, {'lowpkg.bin_pkg_info': MagicMock(return_value={'name': 'test-package',
'version': '1.0'})}):
list_downloaded = aptpkg.list_downloaded()
self.assertEqual(len(list_downloaded), 1)
self.assertDictEqual(list_downloaded, DOWNLOADED_RET)


@skipIf(pytest is None, 'PyTest is missing')
class AptUtilsTestCase(TestCase, LoaderModuleMockMixin):
Expand Down

0 comments on commit 4fed7e0

Please sign in to comment.