Enhance distribution reuse: android_api#2031
Conversation
|
Thanks @opacam, that's a great improvement. Just to let you know I've seen this, but probably won't be able to review it for a few days. |
AndreMiras
left a comment
There was a problem hiding this comment.
I missed that PR. I made a quick first look and seems overall good to me.
I have nothing against merging it as it is after inclement view it
| return dists | ||
|
|
||
| @classmethod | ||
| def get_dist_from_dist_info(cls, ctx, dist_info, dist_dir): |
There was a problem hiding this comment.
I like that you split concerns making a dedicated method for this
pythonforandroid/distribution.py
Outdated
| ''' | ||
| with current_directory(dirn): | ||
| info('Saving distribution info') | ||
| with open('dist_info.json', 'w') as fileh: |
There was a problem hiding this comment.
I feel like we could reuse the newly created dist_info_file property here
| sort_keys=True, | ||
| ) | ||
|
|
||
| def update_dist_info(self, key, value): |
There was a problem hiding this comment.
it feels like this method could share some code with save_info(), could it?
tests/test_distribution.py
Outdated
| ) | ||
| mock_open.side_effect = [ | ||
| mock.mock_open(read_data=json.dumps(dist_info_data)).return_value | ||
| ] |
There was a problem hiding this comment.
Using mock open this way is somehow bugging me, I guess I'm not used to it.
How about something more conventional?
def test_get_dist_info(self):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_dist_info`
calls the proper methods and returns the proper value."""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
mock_open = mock.mock_open(read_data=json.dumps(dist_info_data))
with mock.patch("pythonforandroid.distribution.open", mock_open):
dist_info = self.ctx.bootstrap.distribution.get_dist_info("/fake_dir")
mock_open.assert_called_once_with("/fake_dir/dist_info.json", "r")
self.assertIsInstance(dist_info, dict)It's really passing the mock_open.side_effect with the other return_value that is bugging me, it feels unnatural.
tests/test_distribution.py
Outdated
| ] | ||
| dist_info = self.ctx.bootstrap.distribution.get_dist_info("/fake_dir") | ||
| mock_open.assert_called_once_with("/fake_dir/dist_info.json", "r") | ||
| self.assertIsInstance(dist_info, dict) |
There was a problem hiding this comment.
Would it make sense to go as far as:
self.assertEqual(dist_info, dist_info_data)
@inclement, no problem at all, this way we can polish this thing thanks to the @AndreMiras review 😉 @AndreMiras, I will take care of your comments ASAP...I did a quick look to your comments and I see that we can do a little refactoring, nice !! 😆 |
Because later we will add a little more complexity to this part of the code and it would be too much.
So it's easy for us to read it. Note: Also formatted lines to be PEP8 friendly (below 79 characters)
So we can later check if the dist was created with an different `android api`
So the gradle build can reuse an existing dist and build the apk with the new target api.
So we can reduce the a little more the complexity of cls `Distribution`
Which is in this case: `'pythonforandroid.util.current_directory' imported but unused`
Caused by the latest changes to `Distribution.save_info`
d5dacf1 to
de8c2d5
Compare
|
Just a thought - could this dist-info.json also be used to hold the environment variables for debugging purposes? Currently they only exist in the temp file during build, so some time ago I raised a PR to keep this file permanently inside the dist dir: #1902 |
inclement
left a comment
There was a problem hiding this comment.
Sorry for the especially bad delay on this one. The change is good, but I wonder if it would be better to simply move the android api setting to a templated file like everything else, rather than adding a build toolchain step for it? Maybe I missed a reason not to do that.
In here we solve a problem we have when changing
android_apiif we already have a build distribution that we can reuse. The problem is that the changes are not reflected in the final APK, because there is some hardcodedandroid_apiin some of the distribution files. So in here we will update those files so when we perform the build of the APK with gradle it will change the necessary stuff with the newandroid_api.We also:
android_apivalue todist_info.jsonfile (and update it if we reuse the dist with a newandroid_api)dist_info.json(to be easier to read it for us)Distribution.get_distributionsHow to test this
Well, it will require two builds, the first one with one
android_apiand then another with a newandroid_api. Once the second build complete you will see that the distribution file:AndroidManifest.xmlhas the value of the second build (btw, we never modify this file in this PR, this file is modified by gradle). You can check around line 22, for something like:The value of
android:targetSdkVersionshould be the one you used withandroid_apiin your second build.How will look like the
dist_info.jsonfileAs you see, the keys are sorted and indented with 4 spaces 😉...imho, far readable than all in one line, right?
Notes
Some parts of the code where taken from #1926, so this PR is a portion of what we presented there, but refreshed and enhanced.