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
many: add snapcraftctl command for scriptlets #2002
Merged
kyrofa
merged 18 commits into
canonical:master
from
kyrofa:feature/1675/snapcraft_yaml_functions
Mar 30, 2018
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
85dc17e
many: add snapcraftctl command for scriptlets
kyrofa e750ea6
cli: chain snapcraftctl's debug to snapcraft's
kyrofa edebe21
Set alias when running snapped
kyrofa d8954e1
Merge remote-tracking branch 'origin/master' into feature/1675/snapcr…
kyrofa 51dab81
Use fake snapcraftctl in unit tests
kyrofa 93c39c0
Merge remote-tracking branch 'origin/master' into feature/1675/snapcr…
kyrofa 09dbbe1
Reset SNAPCRAFT_DEBUG between tests
kyrofa f2afc9a
Fix alias
kyrofa c2ad25b
Make snapcraftctl work with python2 parts
kyrofa 25f3db2
Make snapcraftctl independent of the environment
kyrofa 12f6bd0
Merge remote-tracking branch 'origin/master' into feature/1675/snapcr…
kyrofa 455acdb
Make fake snapcraftctl work even if not installed
kyrofa c94bc5f
Make snapcraftctl find snapcraft when snapped
kyrofa 6cd5feb
Merge remote-tracking branch 'origin/master' into feature/1675/snapcr…
kyrofa fe332a3
many: implement override-build scriptlet
kyrofa 15a1a88
snapcraftctl: only define SNAP* vars if set
kyrofa ffff440
Merge remote-tracking branch 'origin/master' into feature/1675/snapcr…
kyrofa 61cb6ee
Merge branch 'master' into feature/1675/snapcraft_yaml_functions
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/sh | ||
# | ||
# This file exists because snapcraftctl must be run using a clean environment | ||
# that is uninfluenced by the environment of the part using it. There are a few | ||
# reasons for this: | ||
# | ||
# 1. snapcraftctl is a python3 utility, but Snapcraft supports building python2 | ||
# parts, where PYTHONPATH et. al. are set for python2. | ||
# 2. snapcraftctl is part of snapcraft, which loads up various libraries that | ||
# can be influenced with LD_LIBRARY_PATH, which is set for many parts. | ||
# | ||
# Not only this, but the only way snapcraftctl works reliably is if it's run | ||
# by exactly the same interpreter as snapcraft itself (otherwise it won't find | ||
# snapcraft). To that end, this script will use the interpreter defined within | ||
# the SNAPCRAFT_INTERPRETER environment variable. | ||
|
||
# Which python3 are we using? By default, the one from the PATH. If | ||
# SNAPCRAFT_INTERPRETER is specified, use that one instead. | ||
python3_command="${SNAPCRAFT_INTERPRETER:-$(which python3)}" | ||
|
||
snapcraftctl_command="""$python3_command"" -c ' | ||
import snapcraft.cli.__main__ | ||
|
||
# Click strips off the first arg by default, so the -c will not be passed | ||
snapcraft.cli.__main__.run_snapcraftctl(prog_name=\"snapcraftctl\") | ||
' ""$@" | ||
|
||
|
||
# We don't actually want a 100% clean environment. Pass on the SNAP variables, | ||
# locale settings, and environment variables required by snapcraftctl itself. | ||
/usr/bin/env -i -- sh -<<END | ||
# Required for snapcraftctl to actually find snapcraft when snapped via | ||
# sitecustomize | ||
if [ -n "$SNAP" ]; then | ||
export SNAP="$SNAP" | ||
fi | ||
if [ -n "$SNAP_NAME" ]; then | ||
export SNAP_NAME="$SNAP_NAME" | ||
fi | ||
if [ -n "$SNAP_VERSION" ]; then | ||
export SNAP_VERSION="$SNAP_VERSION" | ||
fi | ||
if [ -n "$SNAP_ARCH" ]; then | ||
export SNAP_ARCH="$SNAP_ARCH" | ||
fi | ||
|
||
# Required so Click doesn't whine about lack of a locale | ||
export LC_ALL="$LC_ALL" | ||
export LANG="$LANG" | ||
|
||
# Required for snapcraftctl to work | ||
export SNAPCRAFTCTL_CALL_FIFO="$SNAPCRAFTCTL_CALL_FIFO" | ||
export SNAPCRAFTCTL_FEEDBACK_FIFO="$SNAPCRAFTCTL_FEEDBACK_FIFO" | ||
$snapcraftctl_command | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/usr/bin/snapcraft | ||
/usr/bin/snapcraftctl | ||
/usr/lib/python* | ||
/usr/share/snapcraft | ||
/usr/share/snapcraft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2017 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import functools | ||
import json | ||
import logging | ||
import os | ||
import sys | ||
|
||
import click | ||
|
||
from snapcraft.internal.errors import SnapcraftEnvironmentError | ||
from snapcraft.cli._errors import exception_handler | ||
from snapcraft.internal import log | ||
|
||
|
||
@click.group() | ||
@click.option('--debug', '-d', is_flag=True, envvar='SNAPCRAFT_DEBUG') | ||
def run(debug): | ||
"""snapcraftctl is how snapcraft.yaml can communicate with snapcraft""" | ||
|
||
if debug: | ||
log_level = logging.DEBUG | ||
else: | ||
log_level = logging.INFO | ||
|
||
# Setup global exception handler (to be called for unhandled exceptions) | ||
sys.excepthook = functools.partial(exception_handler, debug=debug) | ||
|
||
# In an ideal world, this logger setup would be replaced | ||
log.configure(log_level=log_level) | ||
|
||
|
||
@run.command() | ||
def build(): | ||
"""Run the 'build' step of the calling part's plugin""" | ||
_call_function('build') | ||
|
||
|
||
def _call_function(function_name, args=None): | ||
if not args: | ||
args = {} | ||
|
||
data = { | ||
'function': function_name, | ||
'args': args, | ||
} | ||
|
||
# We could load the FIFOs in `run` and shove them in the context, but | ||
# that's too early to error out if these variables aren't defined. Doing it | ||
# here allows one to run e.g. `snapcraftctl build --help` without needing | ||
# these variables defined, which is a win for usability. | ||
try: | ||
call_fifo = os.environ['SNAPCRAFTCTL_CALL_FIFO'] | ||
feedback_fifo = os.environ['SNAPCRAFTCTL_FEEDBACK_FIFO'] | ||
except KeyError as e: | ||
raise SnapcraftEnvironmentError( | ||
"{!s} environment variable must be defined. Note that this " | ||
"utility is only designed for use within a snapcraft.yaml".format( | ||
e)) from e | ||
|
||
with open(call_fifo, 'w') as f: | ||
f.write(json.dumps(data)) | ||
f.flush() | ||
|
||
with open(feedback_fifo, 'r') as f: | ||
f.readline() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, never mind, console_scripts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, didn't want to duplicate the whole "set UTF-8" thing.