Remove global state attributes from snapcraft.common #434

Merged
merged 13 commits into from Apr 11, 2016

Conversation

Projects
None yet
2 participants
Collaborator

sergiusens commented Apr 11, 2016

No description provided.

sergiusens added some commits Apr 8, 2016

Remove global state for architecture
WIP

LP: #1551849

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
Collaborator

sergiusens commented Apr 11, 2016

@kyrofa and @elopio should I add the same for get_snapdir, get_stagedir, get_local_plugindir and get_partsdir

Collaborator

sergiusens commented Apr 11, 2016

same for plugindir, schemadir and librariesdir but those are more complicated and not really part of a project but of snapcraft itself.

sergiusens added some commits Apr 11, 2016

Collaborator

sergiusens commented Apr 11, 2016

@elopio example tests fail straight out and for some reason I can't connect to the VPN 😞

Collaborator

sergiusens commented Apr 11, 2016

retest this please

sergiusens added some commits Apr 11, 2016

Merge branch 'bugfix/1551849/no-state-can-be-global-forever' of githu…
…b.com:sergiusens/snapcraft into bugfix/1551849/no-state-can-be-global-forever
snapcraft/_options.py
+def _find_machine(deb_arch):
+ for machine in _ARCH_TRANSLATIONS:
+ if _ARCH_TRANSLATIONS[machine].get('deb', '') == deb_arch:
+ logger.info('Setting target machine to {!r}'.format(machine))
@kyrofa

kyrofa Apr 11, 2016

Member

This function isn't actually setting anything-- this logger.info() probably belongs in _set_machine(), no?

@sergiusens

sergiusens Apr 11, 2016

Collaborator

yes, this was a refactor problem :-)

snapcraft/common.py
-
- return build_count
+ raise EnvironmentError(
+ "This plugin is outdated, use 'options.project.parallel_build_count'")
@kyrofa

kyrofa Apr 11, 2016

Member

Thanks for not just breaking completely here 💯 .

+ patcher = mock.patch('multiprocessing.cpu_count')
+ self.cpu_count = patcher.start()
+ self.cpu_count.return_value = 2
+ self.addCleanup(patcher.stop)
@kyrofa

kyrofa Apr 11, 2016

Member

Yeah good idea. I should have done that in the first place.

snapcraft/plugins/kernel.py
- def set_target_machine(self, machine):
- self._target_arch = get_machine_info(machine)
+ def set_target_machine(self):
@kyrofa

kyrofa Apr 11, 2016

Member

A "set" function with no parameter seems odd. Is this something that can be done in the initializer now?

@sergiusens

sergiusens Apr 11, 2016

Collaborator

I thought about this, but if we make plugins take the charge on this I can add something to manage this. The problem is plugins that don't support cross compiling.

@kyrofa

kyrofa Apr 11, 2016

Member

The problem is plugins that don't support cross compiling.

You mean as of right now if one tries cross-compiling with plugins that don't support it you get a good error? That's an excellent point.

snapcraft/pluginhandler.py
@@ -169,12 +169,15 @@ def _load_code(self, plugin_name, properties, part_schema):
plugin = _get_plugin(module)
options = _make_options(part_schema, properties, plugin.schema())
+ # For backwards compatibility we add the project options as an option
+ setattr(options, 'project', self._project_options)
@kyrofa

kyrofa Apr 11, 2016

Member

options currently have a pretty well-defined meaning: "options from the YAML." I like not breaking API in this way, but I also don't like diluting options. Particularly because: what happens when a local plugin declares project as a property in the schema?

Would it be better to actually set a __project attribute on the plugin instance and add a getter method on the base plugin (or something similar)? Or even just set a project attribute since people are used to using an options attribute.

@sergiusens

sergiusens Apr 11, 2016

Collaborator

This will need to go to the base plugin and it will break badly unless I add **kwargs, *args. What is your take?

@kyrofa

kyrofa Apr 11, 2016

Member

I'm not quite sure what you mean-- where are the **kwargs required?

Member

kyrofa commented Apr 11, 2016

Check the title, too 😉 .

sergiusens added some commits Apr 11, 2016

@sergiusens sergiusens changed the title from Bugfix/1551849/no state can be global forever to Remove global state attributes from snapcraft.common Apr 11, 2016

snapcraft/__init__.py
@@ -288,8 +289,8 @@ def env(self, root):
"""
return []
- def set_target_machine(self):
- """Set the target compilation architecture to machine."""
@kyrofa

kyrofa Apr 11, 2016

Member

Can we leave this function, continue taking the machine parameter, deprecate it, and simply have it call enable_cross_compilation()?

@sergiusens

sergiusens Apr 11, 2016

Collaborator

El 11/04/16 a las 14:27, Kyle Fazzari escribió:

In snapcraft/init.py
ubuntu-core#434 (comment):

@@ -288,8 +289,8 @@ def env(self, root):
"""
return []

  • def set_target_machine(self):
  •    """Set the target compilation architecture to machine."""
    

Can we leave this function, continue taking the |machine| parameter,
deprecate it, and simply have it call |enable_cross_compilation()|?
While not hard, the target-arch thing was marked API unstable, so not really compelled to do it. Counter thoughts?

@kyrofa

kyrofa Apr 11, 2016

Member

If people were warned regarding the instability then I'm good with it.

+ # This is for plugins that inherit from BasePlugin but don't have
+ # project in init.
+ if not self.code.project:
+ self.code.project = self._project_options
@kyrofa

kyrofa Apr 11, 2016

Member

Thanks for the comments here, very clear. We can probably make that deprecation a little more helpful-- how about something like:

logger.warning(
    'DEPRECATED: the plugin used by part {!r} needs to be updated '
    'to accept project options in its initializer. See <link to plugins.md> '
    'for more information'.format(self.name)

And we can add that third param to the docs/plugins.md document.

Member

kyrofa commented Apr 11, 2016

Looks great! 👍

@sergiusens sergiusens merged commit 46c46d3 into snapcore:master Apr 11, 2016

3 of 4 checks passed

Examples tests Started
Details
autopkgtest Success
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.04%) to 95.882%
Details

@sergiusens sergiusens deleted the sergiusens:bugfix/1551849/no-state-can-be-global-forever branch Apr 11, 2016

kyrofa added a commit that referenced this pull request Apr 11, 2016

Remove global state attributes from snapcraft.common (#434)
This includes work to move architecture and parallel build related
options to `snapcraft.ProjectOptions` in an effort to move global state
away. This work also includes messages to support users of the API
affected by the change and a transparent migration to include these
`project` options into the plugin attributes.

LP: #1551849

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>

kalikiana pushed a commit to kalikiana/snapcraft that referenced this pull request Apr 6, 2017

Remove global state attributes from snapcraft.common (#434)
This includes work to move architecture and parallel build related
options to `snapcraft.ProjectOptions` in an effort to move global state
away. This work also includes messages to support users of the API
affected by the change and a transparent migration to include these
`project` options into the plugin attributes.

LP: #1551849

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment