provision: Simplify is_force codepaths.

I remove `is_force` from `file_or_package_hash_updated`
and modernize its mypy annotations.

If `is_force` is `True`, we just now run the thing
we want to force-run without having to call
`file_or_package_hash_updated` to expensively
and riskily return `True`.

Another nice outcome of this change is that if
`file_or_package_hash_updated` returns `True`,
you can know that the file or package has
indeed been updated.

For the case of `build_pygments_data` we also
skip an `os.path.exists` check when `is_force`
is `True`.

We will short-circuit more logic in the next
few commits, as well as cleaning up some of
the long/wrapper lines in the `if` statements.
This commit is contained in:
Steve Howell
2020-04-16 11:49:21 +00:00
committed by Tim Abbott
parent f98843d197
commit 067196c508
3 changed files with 12 additions and 10 deletions

View File

@@ -390,8 +390,9 @@ def os_families() -> Set[str]:
distro_info = parse_os_release()
return {distro_info["ID"], *distro_info.get("ID_LIKE", "").split()}
def file_or_package_hash_updated(paths, hash_name, is_force, package_versions=[]):
# type: (List[str], str, bool, List[str]) -> bool
def file_or_package_hash_updated(paths: List[str],
hash_name: str,
package_versions: List[str]=[]) -> bool:
# Check whether the files or package_versions passed as arguments
# changed compared to the last execution.
sha1sum = hashlib.sha1()
@@ -410,7 +411,7 @@ def file_or_package_hash_updated(paths, hash_name, is_force, package_versions=[]
hash_file.seek(0)
last_hash = hash_file.read()
if is_force or (new_hash != last_hash):
if (new_hash != last_hash):
hash_file.seek(0)
hash_file.truncate()
hash_file.write(new_hash)