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

Vendoring-compatible imports #316

Merged
merged 18 commits into from
Feb 17, 2021

Conversation

jku
Copy link
Collaborator

@jku jku commented Jan 14, 2021

This is a sister PR for theupdateframework/python-tuf#1261: it modifies all imports in securesystemlibs to be compatible with vendoring tool (see TUF PR for explanation).

Notes

  • Multiple solutions were used, but usually the module was directly imported into scope (so instead of referring to securesystemslib.settings code now refers to settings). In some cases this individual items from the module are imported instead
  • I've tried very hard to ensure no namespace conflicts happen by grepping for every new top level name
  • This is still a draft: I have yet to test that vendoring actually works with this
  • The change is surprisingly large, >500 lines

Jussi Kukkonen added 3 commits January 14, 2021 12:08
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Make hash module imports compatible with the vendoring tool.

This is trickier than the other cases:
* "hash" is a builtin function, overloading that with module name
  seems bad
* the resulting code "hash.digest(algorithm)" reads like hash is an
  object... Not ideal

Luckily there are few functions in the module: Import the functions
directly to avoid importing the module.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 98.535% when pulling f0d8fbb on jku:vendoring-compatible-imports into 0a8bde5 on secure-systems-lab:master.

@coveralls
Copy link

coveralls commented Jan 14, 2021

Coverage Status

Coverage increased (+0.004%) to 98.535% when pulling 25f112b on jku:vendoring-compatible-imports into dff4425 on secure-systems-lab:master.

@jku
Copy link
Collaborator Author

jku commented Jan 21, 2021

This is ready for review: I've tested this version with pip (it now works without patching securesystemslib) and I've read through the PR again... and decided not to modify it further (without review comments anyway).

As a whole PR there is some inconsistency (that I consciously left there but could change):

  • I think it would usually make sense to import individual exceptions from securesystemslib.exceptions but doing that may make the PR more likely to break things: I'd rather do that in smaller PRs where possible mistakes are not lost in a sea of changes. The practical fear I have is that exceptions are sometimes not tested, so typos and name clashes could slip by without test suite noticing -- maybe linting takes care of all these cases though?
  • however, I have imported smaller number of individual exceptions from other sources (from cryptography or securesystemslib.gpg) where it made sense -- this leads to some exceptions being namespaced and some not
  • importing individual schemas from formats seems likewise a reasonable choice: I did not do that based on Lukas comment in the TUF bug but either way works for me

All of the changes are essentially modifying the import style, the only real changes are

  • resolved a cyclical import in gpg modules
  • renamed process variables to gpg_process

@jku jku marked this pull request as ready for review January 21, 2021 10:57
@jku jku requested review from lukpueh and joshuagl January 21, 2021 10:57
Copy link
Collaborator

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

I don't think we need to address it in this PR, but I just noticed that the style guide says that we should not import names from a module:

Use import statements for packages and modules only, not for individual classes or functions.

We will want to think about a follow-on PR that addresses the issues arising from this style guide requirement, or documenting why we are not adhering to this aspect of the style guide. Exceptions, formats, and hash are obvious places where this is an issue hash. Perhaps we should file an issue about this?

I noticed that several comments are still using fully-qualified names, especially noticeable where we have comments that state which Exceptions a format module function will throw. I do not thing this is a blocker for this PR, but we should figure out how to handle this. I think it is probably bad practice in general to state in the code comments which exceptions some code might raise, it feels like it would be an unnecessary maintenance burden.

securesystemslib/keys.py Outdated Show resolved Hide resolved
securesystemslib/interface.py Outdated Show resolved Hide resolved
securesystemslib/hash.py Show resolved Hide resolved
securesystemslib/interface.py Show resolved Hide resolved
securesystemslib/util.py Show resolved Hide resolved
securesystemslib/gpg/rsa.py Outdated Show resolved Hide resolved
securesystemslib/gpg/rsa.py Outdated Show resolved Hide resolved
securesystemslib/gpg/functions.py Outdated Show resolved Hide resolved
securesystemslib/gpg/util.py Outdated Show resolved Hide resolved
securesystemslib/gpg/util.py Outdated Show resolved Hide resolved
@jku
Copy link
Collaborator Author

jku commented Jan 25, 2021

I don't think we need to address it in this PR, but I just noticed that the style guide says that we should not import names from a module:

Use import statements for packages and modules only, not for individual classes or functions.

We will want to think about a follow-on PR that addresses the issues arising from this style guide requirement, or documenting why we are not adhering to this aspect of the style guide. Exceptions, formats, and hash are obvious places where this is an issue. Perhaps we should file an issue about this?

I admit I had either forgotten or misunderstood the google style guide in this. I'm not totally against doing what the style guide says (it would make the PR more consistent and having less exceptions to the style guide is better) but it does feel like a hard requirement -- not having to namespace uniquely named things that are used over and over in the importing module does improve readability. Admittedly there are cases where names aren't quite unique enough and conflicts are more likely.

The other case where importing a module seems counterproductive is when there's only one or two things in the module:

from tuf.client import updater # import the module, not the class
updater = updater.Updater()    # that's not just repetitive, it's also a name clash

Jussi Kukkonen added 15 commits February 11, 2021 10:46
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Import only the FilesystemBackend class when needed: this is
compatible with the vendoring tool.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
The aim is to avoid potential conflict with 'process' module.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Rename the import to make it clear this is the python module, use an
import line that is compatible with vendoring tool.
Use gpg_util as local name to avoid potential conflicts with the other
util module.
Importing individual errors avoids conflict with the other exceptions
module in securesystemslib.
Use "from securesystemslib import <mod>" instead of
"import securesystemslib.<mod>" to make securesystemslib compatible with
the vendoring tool.
Importing the two functions individually seems reasonable here.
The results of the import is same, but new style is compatible with
vendoring tool.
Modify the way nacl and cryptography imports are made to make them
compatible with vendoring tool.
There is a circular dependency in
  gpg.constants -> gpg.rsa -> gpg.util -> gpg.constants

This is not a problem on python3 or python2 when the imports are done
with "import securesystemslib.<mod>" style. However with the
"from securesystemslib import <mod>" style python2 decides this is a
ImportError.

Remove the circular dependency by moving the module variables from
constants to another file.
@jku jku force-pushed the vendoring-compatible-imports branch from ae1f611 to 25f112b Compare February 11, 2021 08:51
@jku
Copy link
Collaborator Author

jku commented Feb 11, 2021

Applied all suggestions and rebased as none of the changes were functional, just style.

Lukas, please take a look: The resolved discussion is trivial but do check the last comment for the style guide discussion.

The latest test failure is a little baffling. I think it's unrelated but... have you seen this before?

  File "/home/travis/build/secure-systems-lab/securesystemslib/tests/test_process.py", line 121, in test_run_duplicate_streams_timeout
265    securesystemslib.process.run_duplicate_streams("python --version",
266AssertionError: TimeoutExpired not raised

@lukpueh
Copy link
Member

lukpueh commented Feb 11, 2021

Lukas, please take a look: The resolved discussion is trivial but do check the last comment for the style guide discussion.

Thanks, will do!

The latest test failure is a little baffling. I think it's unrelated but... have you seen this before?

No, I have not. And as coincidence would have it, @SolidifiedRay just notified me about a different and very likely also unrelated issue in the same process module.

Let me try if this issue here "goes away" if I restart the build, so that we can merge this PR and troubleshoot the process module separately.

@lukpueh
Copy link
Member

lukpueh commented Feb 11, 2021

Let me try if this issue here "goes away" if I restart the build ...

Looks like it did. But we should still find the cause.

@joshuagl
Copy link
Collaborator

This PR looks good to me. I believe the places we break the style guide are pragmatic choices to minimise the number of changes and/or the potential for problematic changes (for example, reducing the number of new symbols in a scope).

Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this tedious task, @jku! LGTM. All the decisions you made make sense in their context, even if you deviate from the style guide.

FYI, I also ran pylint with import-related checks only...

pylint --disable all \
       --enable $(pylint --list-msgs | grep import | grep -Eo "[A-Z][0-9]{4}" | tr "\n" ",") \
       securesystemslib

... and didn't notice anything suspicious.

@@ -79,7 +79,7 @@
import six

from securesystemslib import exceptions
import securesystemslib.schema as SCHEMA
from securesystemslib import schema as SCHEMA
Copy link
Member

Choose a reason for hiding this comment

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

I guess you kept as SCHEMA to make the commit less invasive? This might be something to note in the commit message.

Copy link
Member

Choose a reason for hiding this comment

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

Let me know if you want to reword the commit message. Otherwise, I'll just go ahead and merge. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes exactly. And I think I can live with that message if you can :)

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants