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

many: support the use of build-aux/snap #2496

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions snapcraft/cli/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def _install_multipass():


# TODO: when snap is a real step we can simplify the arguments here.
# fmt: off
def _execute( # noqa: C901
step: steps.Step,
parts: str,
Expand All @@ -93,22 +92,33 @@ def _execute( # noqa: C901
destructive_mode: bool = False,
**kwargs
) -> "Project":
# fmt: on
_clean_provider_error()
provider = "host" if destructive_mode else None
build_environment = env.BuilderEnvironmentConfig(force_provider=provider)
try:
conduct_build_environment_sanity_check(build_environment.provider)
except MultipassMissingInstallableError as e:
click.echo("You need multipass installed to build snaps "
"(https://github.com/CanonicalLtd/multipass).")
click.echo(
"You need multipass installed to build snaps "
"(https://github.com/CanonicalLtd/multipass)."
)
if click.confirm("Would you like to install it now?"):
_install_multipass()
else:
raise errors.SnapcraftEnvironmentError("multipass is required to continue.") from e
raise errors.SnapcraftEnvironmentError(
"multipass is required to continue."
) from e

project = get_project(is_managed_host=build_environment.is_managed_host, **kwargs)

echo.wrapped(
"Using {!r}: Project assets will be "
"searched for from the {!r} directory.".format(
project.info.snapcraft_yaml_file_path,
os.path.relpath(project._get_snapcraft_assets_dir(), project._project_dir),
)
)

conduct_project_sanity_check(project)

if build_environment.is_managed_host or build_environment.is_host:
Expand Down Expand Up @@ -146,8 +156,10 @@ def _execute( # noqa: C901
if project.debug:
instance.shell()
else:
echo.warning("Run the same command again with --debug to shell into the environment "
"if you wish to introspect this failure.")
echo.warning(
"Run the same command again with --debug to shell into the environment "
"if you wish to introspect this failure."
)
raise
else:
if shell or shell_after:
Expand Down Expand Up @@ -187,8 +199,10 @@ def init():
"""Initialize a snapcraft project."""
snapcraft_yaml_path = lifecycle.init()
echo.info("Created {}.".format(snapcraft_yaml_path))
echo.wrapped("Go to https://docs.snapcraft.io/the-snapcraft-format/8337 for more "
"information about the snapcraft.yaml format.")
echo.wrapped(
"Go to https://docs.snapcraft.io/the-snapcraft-format/8337 for more "
"information about the snapcraft.yaml format."
)


@lifecyclecli.command()
Expand Down
24 changes: 18 additions & 6 deletions snapcraft/internal/meta/_snap_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,29 +435,41 @@ def _record_manifest_and_source_snapcraft_yaml(self):
def write_snap_directory(self) -> None:
# First migrate the snap directory. It will overwrite any conflicting
# files.
for root, directories, files in os.walk("snap"):
for root, directories, files in os.walk(
self._project_config.project._get_snapcraft_assets_dir()
):
with contextlib.suppress(ValueError):
directories.remove(".snapcraft")
with contextlib.suppress(ValueError):
# The snapcraft.yaml is migrated later
files.remove("snapcraft.yaml")

for directory in directories:
source = os.path.join(root, directory)
destination = os.path.join(self._prime_dir, source)
source = os.path.relpath(
os.path.join(root, directory),
self._project_config.project._work_dir,
)
# Also build-aux from destination
destination = os.path.join(self._prime_dir, source.lstrip("build-aux/"))
file_utils.create_similar_directory(source, destination)

for file_path in files:
source = os.path.join(root, file_path)
destination = os.path.join(self._prime_dir, source)
source = os.path.relpath(
os.path.join(root, file_path),
self._project_config.project._work_dir,
)
# Also build-aux from destination
destination = os.path.join(self._prime_dir, source.lstrip("build-aux/"))
with contextlib.suppress(FileNotFoundError):
os.remove(destination)
file_utils.link_or_copy(source, destination)

# Now copy the assets contained within the snap directory directly into
# meta.
for origin in ["gui", "hooks"]:
src_dir = os.path.join("snap", origin)
src_dir = os.path.join(
self._project_config.project._get_snapcraft_assets_dir(), origin
)
dst_dir = os.path.join(self.meta_dir, origin)
if os.path.isdir(src_dir):
os.makedirs(dst_dir, exist_ok=True)
Expand Down
1 change: 1 addition & 0 deletions snapcraft/project/_get_snapcraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
def get_snapcraft_yaml(base_dir=None):
possible_yamls = [
os.path.join("snap", "snapcraft.yaml"),
os.path.join("build-aux", "snap", "snapcraft.yaml"),
"snapcraft.yaml",
".snapcraft.yaml",
]
Expand Down
8 changes: 8 additions & 0 deletions snapcraft/project/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def __init__(

super().__init__(target_deb_arch, debug, work_dir=work_dir)

def _get_snapcraft_assets_dir(self) -> str:
if self.info.snapcraft_yaml_file_path.endswith(
os.path.join("build-aux", "snap", "snapcraft.yaml")
):
return os.path.join(self._project_dir, "build-aux", "snap")
else:
return os.path.join(self._project_dir, "snap")

def _get_global_state_file_path(self) -> str:
if self._is_managed_host:
state_file_path = os.path.join(self._work_dir, "state")
Expand Down
42 changes: 42 additions & 0 deletions tests/spread/general/build-aux/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
summary: Use build-aux for snapcraft assets

# We currently only have core18 on a stable channel
systems: [ubuntu-18*]

environment:
SNAPCRAFT_BUILD_INFO: on

prepare: |
mkdir test-snap
cd test-snap
snapcraft init
mkdir snap/hooks
touch snap/hooks/install
mkdir snap/gui
touch snap/gui/icon.png

mkdir build-aux
mv snap build-aux

restore: |
rm -rf test-snap

execute: |
cd test-snap

snapcraft prime

if [ ! -f prime/meta/gui/icon.png ]; then
echo "Missing expected snap icon"
exit 1
fi

if [ ! -f prime/meta/hooks/install ]; then
echo "Missing expected snap install hook"
exit 1
fi

if [ ! -f prime/snap/snapcraft.yaml ]; then
echo "Missing snapcraft.yaml in snap dir"
exit 1
fi
11 changes: 11 additions & 0 deletions tests/unit/project/test_get_snapcraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class GetSnapcraftYamlTest(unit.TestCase):
scenarios = [
("snapcraft.yaml", dict(file_path="snapcraft.yaml")),
("snap/snapcraft.yaml", dict(file_path=os.path.join("snap", "snapcraft.yaml"))),
(
"build-aux",
dict(file_path=os.path.join("build-aux", "snap", "snapcraft.yaml")),
),
(".snapcraft.yaml", dict(file_path=".snapcraft.yaml")),
]

Expand Down Expand Up @@ -65,6 +69,13 @@ class GetSnapcraftYamlDuplicateErrorsTest(unit.TestCase):
file_path2=os.path.join("snap", "snapcraft.yaml"),
),
),
(
"build-aux and snap",
dict(
file_path1=os.path.join("build-aux", "snap", "snapcraft.yaml"),
file_path2=os.path.join("snap", "snapcraft.yaml"),
),
),
]

def test_duplicates(self):
Expand Down
Loading