Skip to content

Commit

Permalink
Refactor checksum variables syntaxfor better clarity
Browse files Browse the repository at this point in the history
- <checksum_mismatch> is now <checksum_match>
  • Loading branch information
Hugo Cartwright committed Nov 8, 2019
1 parent 6e767ad commit 872e92f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
52 changes: 26 additions & 26 deletions lib/ansible/modules/net_tools/basics/get_url.py
Expand Up @@ -529,37 +529,37 @@ def main():
module.fail_json(msg='The checksum format is invalid', **result)

if not dest_is_dir and os.path.exists(dest):
checksum_mismatch = False
checksum_match = True

# If the download is not forced and there is a checksum, allow
# checksum match to skip the download.
if not force and checksum != '':
destination_checksum = module.digest_from_file(dest, algorithm)
if not force:
# If the download is not forced and there is a checksum, allow
# checksum match to skip the download.
if checksum != '':
destination_checksum = module.digest_from_file(dest, algorithm)

if checksum != destination_checksum:
checksum_mismatch = True
if checksum != destination_checksum:
checksum_match = False

# Not forcing redownload, unless checksum does not match
if not force and checksum and not checksum_mismatch:
# Not forcing redownload, unless checksum does not match
# allow file attribute changes
module.params['path'] = dest
file_args = module.load_file_common_arguments(module.params)
file_args['path'] = dest
result['changed'] = module.set_fs_attributes_if_different(file_args, False)
if result['changed']:
module.exit_json(msg="file already exists but file attributes changed", **result)
module.exit_json(msg="file already exists", **result)

# If the file already exists, prepare the last modified time for the
# request.
mtime = os.path.getmtime(dest)
last_mod_time = datetime.datetime.utcfromtimestamp(mtime)

# If the checksum does not match we have to force the download
# because last_mod_time may be newer than on remote
if checksum_mismatch:
force = True
if checksum and checksum_match:
module.params['path'] = dest
file_args = module.load_file_common_arguments(module.params)
file_args['path'] = dest
result['changed'] = module.set_fs_attributes_if_different(file_args, False)
if result['changed']:
module.exit_json(msg="file already exists but file attributes changed", **result)
module.exit_json(msg="file already exists", **result)

# If the file already exists, prepare the last modified time for the
# request.
mtime = os.path.getmtime(dest)
last_mod_time = datetime.datetime.utcfromtimestamp(mtime)

# If the checksum does not match we have to force the download
# because last_mod_time may be newer than on remote
if not checksum_match:
force = True

# download to tmpsrc
start = datetime.datetime.utcnow()
Expand Down
7 changes: 4 additions & 3 deletions test/integration/targets/get_url/tasks/main.yml
Expand Up @@ -482,17 +482,18 @@

- name: Define test file for force==no shouldnt redownload dsturl
set_fact:
geturl_dstfile: "{{ remote_tmp_dir }}/force_no_redownload_test.txt"
geturl_dstfile: "{{ remote_tmp_dir }}/test_force_no"

- name: Create dstfile by getting dsturl
get_url:
url: 'https://{{ httpbin_host }}/html'
url: 'https://{{ httpbin_host }}/get'
dest: "{{ geturl_dstfile }}"
force: yes
register: download_check

- name: Run get_url a second time but this time with dstfile already existing and force==no
get_url:
url: 'https://{{ httpbin_host }}/html'
url: 'https://{{ httpbin_host }}/get'
dest: "{{ geturl_dstfile }}"
force: no
register: result
Expand Down

0 comments on commit 872e92f

Please sign in to comment.