Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_jenkins.py

raw
1 # Copyright (c) 2022, Alexei Znamensky <[email protected]>
2 #
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
7 # Do not use this from other collections or standalone plugins/modules!
8
9 from __future__ import annotations
10
11 import os
12 import time
13
14
15 def download_updates_file(updates_expiration: int | float) -> tuple[str, bool]:
16 updates_filename = "jenkins-plugin-cache.json"
17 updates_dir = os.path.expanduser("~/.ansible/tmp")
18 updates_file = os.path.join(updates_dir, updates_filename)
19 download_updates = True
20
21 # Make sure the destination directory exists
22 if not os.path.isdir(updates_dir):
23 os.makedirs(updates_dir, 0o700)
24
25 # Check if we need to download new updates file
26 if os.path.isfile(updates_file):
27 # Get timestamp when the file was changed last time
28 ts_file = os.stat(updates_file).st_mtime
29 ts_now = time.time()
30
31 if ts_now - ts_file < updates_expiration:
32 download_updates = False
33
34 return updates_file, download_updates