Merge lp:~mvo/snapcraft/python3-project into lp:~snappy-dev/snapcraft/core

Proposed by Michael Vogt on 2015-07-13
Status: Merged
Approved by: Michael Terry on 2015-07-17
Approved revision: 88
Merged at revision: 90
Proposed branch: lp:~mvo/snapcraft/python3-project
Merge into: lp:~snappy-dev/snapcraft/core
Diff against target: 146 lines (+109/-0)
7 files modified
examples/py3-project/meta/package.yaml (+5/-0)
examples/py3-project/meta/readme.md (+1/-0)
examples/py3-project/sha3sum.py (+15/-0)
examples/py3-project/snapcraft.yaml (+6/-0)
plugins/python3-project.yaml (+8/-0)
snapcraft/plugins/python3.py (+43/-0)
snapcraft/plugins/python3_project.py (+31/-0)
To merge this branch: bzr merge lp:~mvo/snapcraft/python3-project
Reviewer Review Type Date Requested Status
Michael Terry (community) 2015-07-13 Approve on 2015-07-17
Review via email: mp+264521@code.launchpad.net

Commit Message

Add python3 plugin

Description of the Change

This is a bit of a RFC branch.

It adds a python3-project plugin for python3 parts. It is not using pip/pypi (for projects not on pypi). My idea was to have a python3-project and a pip3-project. But I do wonder if they should be separate or if python3-project should simply support both and we infer that its pip from the source: option (i.e. "source: pip:pkgname").

The other question I have is what the best layout for my py3-sponeshaker example is. The idea is that its a example that uses a github py3 module to do sha3 and builds a simple cmdline tool on top of this that uses the sha3 python-module. The sha3sum.py is just a single file that needs to be in the top-level of the snap. I wonder if we should have "copy" plugin or something like this that just puts the one file into the snap. What do you think is the best approach here?

And finally - python3 seems to be not have a official linux binary download. Which is a bit of a bummer. But it seems its not too terrible as building on my box takes only ~2min (and ~170mb diskspace). I will look into this next.

To post a comment you must log in.
Michael Terry (mterry) wrote :

Spongeshaker is such a good project name. :)

I think it makes sense to have a "python3-project" plugin for setup.py-style projects.

And it probably makes sense to also have a "pip3" (or pypi3?) plugin for the use case of using the parts list as dependencies for your own project. I'm also not 100% on whether that's better or worse than "source: pip:spongeshaker"

As for the layout... I think it does make sense to offer a simple "copy" or "files" plugin. Could take a map of input files to output path?

I bet the snapcraft.yaml for your example project COULD look like:

"""
parts:
  spongeshaker:
    plugin: pip3
project:
  plugin: copy
  files:
    sha3sum.py: sha3sum
snappy-metadata: meta
"""

(Uses some concepts from my recent "discussion items" email.) How does that feel?

And as for how to get python3... Maybe we could use the ubuntu package? But getting it and building it from upstream is also pretty nice if it's not too bad. Does it have many dependencies?

A couple small comments inline too.

Michael Vogt (mvo) wrote :

Thanks! I renamed the example and addressed the inline comments. Having a python3-project and a pypi3/pip3 plugin sounds reasonable. I create a branch on top of this one for the copy plugin so that the example is complete (lp:~mvo/snapcraft/copy-plugin). The example snapcraft.yaml is slightly different from the above proposal as there is no "project" yet AIUI. Should be trivial to migrate though.

Barry Warsaw (barry) wrote :

On Jul 14, 2015, at 12:33 PM, Michael Vogt wrote:

>And finally - python3 seems to be not have a official linux binary
>download. Which is a bit of a bummer.

It's pretty much impossible to provide that upstream, given the wide range of
ABI differences among the Linux landscape.

Michael Terry (mterry) wrote :

More comments.

Michael Vogt (mvo) wrote :

@Barry: aha, thanks! Thats useful to know :)

@Michael: Thanks a bunch for the review. I addressed the points (hopefully) and this should be ready.

Michael Terry (mterry) wrote :

A couple comments, mostly about the environment stuff.

Looks nice and clean. Encouraging, in terms of how hard it is to write plugins.

Michael Terry (mterry) wrote :

Oh hrm. I asked about testing the snap. Snapcraft's default "wrap the user's binaries in a script that sets up the environment for them" logic wouldn't find your sha3sum script, because it's not in bin/ or usr/bin/...

Now that Snapcraft will know the metadata for the snap ahead of time... Maybe it should only wrap the binaries listed in package.yaml. That way it could find them anywhere.

Michael Vogt (mvo) wrote :

Thanks for your comments, I replied inline and fixed the issues.

As for:
> Snapcraft's default "wrap the user's binaries in a script that sets up the environment for them
> logic wouldn't find your sha3sum script, because it's not in bin/ or usr/bin/...

I copy the sha3sum.py into bin/ in the copy-plugin branch now. I think the idea to only wrap stuff that is mentioned in binaries in package.yaml is great.

Michael Terry (mterry) wrote :

OK, looks nice. :)

Here's what I found out about import paths: Ubuntu's version of python (and maybe upstream's too?) looks at where it's running from to figure out it's prefix. And installs a bunch of standard import locations into sys.path.

I had a hard time believing your imports were working ("PYTHONPATH=%s:$PYTHONPATH" % root" in particular should be %s/usr/lib/python2.7/dist-packages/ or the like). So I wanted to make a python2 version, something I knew wouldn't be on my install of snappy.

Here's what sys.path looks like with NO extra variables set (besides PATH to find python2 in the first place):

['/apps/spongeshaker.sideload/0/bin', '/apps/spongeshaker.sideload/0', '/apps/spongeshaker.sideload/0/usr/lib/python2.7', '/apps/spongeshaker.sideload/0/usr/lib/python2.7/plat-x86_64-linux-gnu', '/apps/spongeshaker.sideload/0/usr/lib/python2.7/lib-tk', '/apps/spongeshaker.sideload/0/usr/lib/python2.7/lib-old', '/apps/spongeshaker.sideload/0/usr/lib/python2.7/lib-dynload', '/apps/spongeshaker.sideload/0/usr/lib/python2.7/dist-packages']

So... I think we don't need ANY of the env() lines in this branch. PATH is automatically done for bin and usr/bin by snapcraft. PYTHONHOME and PYTHONPATH are automatically discovered by python.

So that's nice...!

So I guess drop the env() bits and I'm good with this branch. I can later propose my python2 version (which is largely a copy/paste).

Ideally the python3-project plugin would also get all dependencies listed in requirements.txt or in setup.py and install them into the same part. But that can come along with the pip3 plugin (similar logic I'm guessing).

Michael Vogt (mvo) wrote :

Thanks a lot Mike for digging into the details of this. Thats indeed great news. I removed them all now (except for PATH).

Michael Terry (mterry) wrote :

Nice! :) Setting PATH can also be left out, since snapcraft core does the standard PATH locations (bin and usr/bin) for all the parts. But it's harmless.

review: Approve
Michael Terry (mterry) wrote :

This needs a commit message and I also noticed one thing! You still have snapFiles, when you want snap_files now.

review: Needs Fixing
Michael Terry (mterry) wrote :

And pullBranch is now pull_branch

Michael Vogt (mvo) wrote :

Thanks again. I fixed snap_files() in r87 and removed setting PATH along the way.

lp:~mvo/snapcraft/python3-project updated on 2015-07-17
87. By Michael Vogt on 2015-07-17

fix snapFiles->snap_files and remove env() complettely (snapcraft sets this already). Thanks mterry!

88. By Michael Vogt on 2015-07-17

merged lp:snapcraft and renamed pullBranch -> pull_branch

Michael Terry (mterry) wrote :

Looks great!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'examples/py3-project'
2=== added directory 'examples/py3-project/meta'
3=== added file 'examples/py3-project/meta/package.yaml'
4--- examples/py3-project/meta/package.yaml 1970-01-01 00:00:00 +0000
5+++ examples/py3-project/meta/package.yaml 2015-07-17 07:14:10 +0000
6@@ -0,0 +1,5 @@
7+name: spongeshaker
8+version: 0
9+vendor: michael.vogt@ubuntu.com
10+binaries:
11+ - name: ./sha3sum
12
13=== added file 'examples/py3-project/meta/readme.md'
14--- examples/py3-project/meta/readme.md 1970-01-01 00:00:00 +0000
15+++ examples/py3-project/meta/readme.md 2015-07-17 07:14:10 +0000
16@@ -0,0 +1,1 @@
17+A python sha3 implementation
18
19=== added file 'examples/py3-project/sha3sum.py'
20--- examples/py3-project/sha3sum.py 1970-01-01 00:00:00 +0000
21+++ examples/py3-project/sha3sum.py 2015-07-17 07:14:10 +0000
22@@ -0,0 +1,15 @@
23+#!/usr/bin/env python3
24+
25+import sys
26+
27+import spongeshaker.sha3
28+
29+
30+if __name__ == "__main__":
31+ # 224 is the default from sha3sum
32+ h = spongeshaker.sha3.sha3_224()
33+ with open(sys.argv[1], "rb") as fp:
34+ data = fp.read()
35+ h.update(data)
36+ print(h.hexdigest())
37+
38
39=== added file 'examples/py3-project/snapcraft.yaml'
40--- examples/py3-project/snapcraft.yaml 1970-01-01 00:00:00 +0000
41+++ examples/py3-project/snapcraft.yaml 2015-07-17 07:14:10 +0000
42@@ -0,0 +1,6 @@
43+parts:
44+ spongeshaker:
45+ plugin: python3-project
46+ source: git://github.com/markokr/spongeshaker.git
47+snap:
48+ meta: meta
49
50=== added file 'plugins/python3-project.yaml'
51--- plugins/python3-project.yaml 1970-01-01 00:00:00 +0000
52+++ plugins/python3-project.yaml 2015-07-17 07:14:10 +0000
53@@ -0,0 +1,8 @@
54+module: python3_project
55+requires:
56+ - python3
57+options:
58+ source:
59+ required: true
60+ configflags:
61+ required: false
62
63=== added file 'plugins/python3.yaml'
64=== added file 'snapcraft/plugins/python3.py'
65--- snapcraft/plugins/python3.py 1970-01-01 00:00:00 +0000
66+++ snapcraft/plugins/python3.py 2015-07-17 07:14:10 +0000
67@@ -0,0 +1,43 @@
68+# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
69+#
70+# Copyright (C) 2015 Canonical Ltd
71+#
72+# This program is free software: you can redistribute it and/or modify
73+# it under the terms of the GNU General Public License version 3 as
74+# published by the Free Software Foundation.
75+#
76+# This program is distributed in the hope that it will be useful,
77+# but WITHOUT ANY WARRANTY; without even the implied warranty of
78+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79+# GNU General Public License for more details.
80+#
81+# You should have received a copy of the GNU General Public License
82+# along with this program. If not, see <http://www.gnu.org/licenses/>.
83+
84+import snapcraft
85+from snapcraft.plugins.ubuntu import UbuntuPlugin
86+
87+
88+class Python3Plugin(snapcraft.BasePlugin):
89+
90+ def __init__(self, name, options):
91+ super().__init__(name, options)
92+
93+ class Py3Options:
94+ package = "python3-dev"
95+ self.ubuntu = UbuntuPlugin(name, Py3Options())
96+
97+ # note that we don't need to set PYTHONHOME here,
98+ # python discovers this automatically from it installed
99+ # location, see https://code.launchpad.net/~mvo/snapcraft/python3-project/+merge/264521/comments/664308
100+ #
101+ # PATH is automatically set by snapcraft
102+
103+ def pull(self):
104+ return self.ubuntu.pull()
105+
106+ def build(self):
107+ return self.ubuntu.build()
108+
109+ def snap_files(self):
110+ return self.ubuntu.snap_files()
111
112=== added file 'snapcraft/plugins/python3_project.py'
113--- snapcraft/plugins/python3_project.py 1970-01-01 00:00:00 +0000
114+++ snapcraft/plugins/python3_project.py 2015-07-17 07:14:10 +0000
115@@ -0,0 +1,31 @@
116+# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
117+#
118+# Copyright (C) 2015 Canonical Ltd
119+#
120+# This program is free software: you can redistribute it and/or modify
121+# it under the terms of the GNU General Public License version 3 as
122+# published by the Free Software Foundation.
123+#
124+# This program is distributed in the hope that it will be useful,
125+# but WITHOUT ANY WARRANTY; without even the implied warranty of
126+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
127+# GNU General Public License for more details.
128+#
129+# You should have received a copy of the GNU General Public License
130+# along with this program. If not, see <http://www.gnu.org/licenses/>.
131+
132+import snapcraft
133+
134+
135+class Python3ProjectPlugin(snapcraft.BasePlugin):
136+
137+ # note that we don't need to setup env(), python figures it out
138+ # see python3.py for more details
139+
140+ def pull(self):
141+ return self.pull_branch(self.options.source)
142+
143+ def build(self):
144+ return self.run(
145+ ["python3", "setup.py", "install", "--install-layout=deb",
146+ "--prefix=%s/usr" % self.installdir])

Subscribers

People subscribed via source and target branches