Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.18.0 adding code hash sum #48

Merged
merged 15 commits into from
Apr 9, 2024
Merged

2.18.0 adding code hash sum #48

merged 15 commits into from
Apr 9, 2024

Conversation

herbberg
Copy link
Contributor

@herbberg herbberg commented Mar 21, 2024

Adding hash of source and VHDL templates as additional meta tag.

@arnobaer
Copy link
Member

This is a significant change, consider incrementing the minor version for this feature, e.g. 2.18.0, see also https://semver.org/

@@ -1 +1 @@
__version__ = "2.17.1"
__version__ = "2.17.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A proper version would be 2.18.0 as this is a new feature and not a bug fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Comment on lines 1702 to 1703
file_path = os.path.dirname(__file__)
files_hash_value = vhdlhelper.get_files_hash_value(file_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculating the software hash is not part of the resource distribution, consider moving this into module vhdlproducer or __main__.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved get_files_hash_value to constants.py to avoid circular references

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constants module is not ideal, I moved the code to the __main__ module (entry point for hash calculation).

Comment on lines 387 to 388
file_path = os.path.dirname(__file__)
self.files_hash_value = get_files_hash_value(file_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid calculating the hash multiple times, this gives potential for discrepancies and introduces an overhead. Try to calculate the hash once in __main__ and pass it like the __version__ variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 272 to 273
for i in range(0, len(filename)):
with open(filename[i], 'rb') as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for a range iterator.

for filename in filenames:
    with open(filename, "rb") as f:
        ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


x = hashlib.sha256()

filename = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filenames = [](plural for lists)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

py_files = dir_path+"/**/*.py"
vhd_files = dir_path+"/**/*.vhd"

x = hashlib.sha256()
Copy link
Member

@arnobaer arnobaer Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h = hashlib.sha256() or sha256_hash = hashlib.sha256() would increase code readability (note hash is a built in function, so try to avoid hash = hashlib.sha256()).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand! Why is "h" better than "x"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h for hash. While x is a perfectly valid variable name, it is quite generic and provides no hints about the variable's intended use. In contrast, h or sha256_hash offers immediate context. See also the Zen of Python's "Explicit is better than implicit" principle (PEP 20), https://peps.python.org/pep-0020/

@arnobaer arnobaer changed the title merge dev_v2.17.2 to master 2.18.0 adding code hash sum Mar 22, 2024
@arnobaer
Copy link
Member

arnobaer commented Mar 22, 2024

To enhance the hash calculation I moved the calculation to the __main__ module and calculate the actual hash only once in the main entry point, passing down the value to the VHDLProducer, MenuHelper and InfoHelper classes.

To provide a flexible solution the value is passed in form of a generic config dict, so additional values can be inserted with ease.

producer = VhdlProducer(template_dir)
producer.config.update({"sw_hash": sw_hash})

@arnobaer arnobaer added the enhancement New feature or request label Mar 22, 2024
@arnobaer arnobaer added this to the 2.18.x milestone Mar 22, 2024
@herbberg herbberg merged commit 4920cf2 into master Apr 9, 2024
3 checks passed
@herbberg herbberg deleted the dev_v2.17.2 branch April 9, 2024 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants