Skip to content

Commit

Permalink
Replace SNAPCRAFT_PART_INSTALL in the part attributes. (#841)
Browse files Browse the repository at this point in the history
LP: #1628915
  • Loading branch information
josepht authored and sergiusens committed Sep 30, 2016
1 parent 5d447ae commit 6cef6c0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions integration_tests/snaps/simple-cmake-replace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.2)
project(simple-cmake-replace C)
add_definitions(-DPART_INSTALL=${PART_INSTALL})
add_executable(simple-cmake-replace test.c)
install(TARGETS simple-cmake-replace RUNTIME DESTINATION bin)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions integration_tests/snaps/simple-cmake-replace/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: test-package
version: 0.1
summary: one line summary
description: a longer description
icon: icon.png
confinement: strict

build-packages: [gcc, libc6-dev]

parts:
cmake-project:
plugin: cmake
configflags: ['-DPART_INSTALL="$SNAPCRAFT_PART_INSTALL"']
source: .
5 changes: 5 additions & 0 deletions integration_tests/snaps/simple-cmake-replace/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int main() {
printf("When I was built I was installed to %s\n", PART_INSTALL);
}
15 changes: 15 additions & 0 deletions integration_tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import subprocess

from testtools.matchers import FileContains

Expand Down Expand Up @@ -56,3 +57,17 @@ def test_snapcraft_stage_env_replacement(self):
project_dir = 'stage_env'

self.run_snapcraft('stage', project_dir)

def test_stage_cmake_plugin_with_replace(self):
"""Replace SNAPCRAFT_PART_INSTALL in the part's attributes"""
project_dir = 'simple-cmake-replace'
self.run_snapcraft('stage', project_dir)

binary_output = subprocess.check_output([
os.path.join('stage', 'bin', 'simple-cmake-replace')],
cwd=project_dir)
path = os.path.join(os.getcwd(), project_dir, 'parts',
'cmake-project', 'install')
self.assertEqual(
"When I was built I was installed to {}\n".format(path),
binary_output.decode('utf-8'))
12 changes: 12 additions & 0 deletions snapcraft/internal/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
pluginhandler,
repo,
)
from snapcraft.internal.project_loader import replace_attr


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -106,6 +107,16 @@ def execute(step, project_options, part_names=None):
'type': config.data.get('type', '')}


def _replace_in_part(part):
for key, value in part.code.options.__dict__.items():
value = replace_attr(value, [
('$SNAPCRAFT_PART_INSTALL', part.code.installdir),
])
setattr(part.code.options, key, value)

return part


class _Executor:

def __init__(self, config, project_options):
Expand Down Expand Up @@ -181,6 +192,7 @@ def _run_step(self, step, part, part_names):
common.env = self.parts_config.build_env_for_part(part)
common.env.extend(self.config.project_env())

part = _replace_in_part(part)
getattr(part, step)()

def _create_meta(self, step, part_names):
Expand Down
19 changes: 19 additions & 0 deletions snapcraft/tests/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ def make_snapcraft_yaml(self, parts, snap_type=''):

super().make_snapcraft_yaml(yaml.format(parts=parts, type=snap_type))

def test__replace_in_parts(self):
class Options:
def __init__(self):
self.source = '$SNAPCRAFT_PART_INSTALL'

class Code:
def __init__(self):
self.options = Options()
self.installdir = '/tmp'

class Part:
def __init__(self):
self.code = Code()

part = Part()
new_part = lifecycle._replace_in_part(part)

self.assertEqual(part.code.installdir, new_part.code.options.source)

def test_exception_when_dependency_is_required(self):
self.make_snapcraft_yaml("""parts:
part1:
Expand Down

0 comments on commit 6cef6c0

Please sign in to comment.