Skip to content

Commit

Permalink
Merge pull request #349 from kyrofa/backport/source_subdir_still_copy…
Browse files Browse the repository at this point in the history
…_all

Backport to 1.x: Copy entire source, even if source-subdir is specified.
  • Loading branch information
Kyle Fazzari committed Feb 29, 2016
2 parents c9276c0 + eca386c commit 34f96cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
29 changes: 15 additions & 14 deletions snapcraft/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2015 Canonical Ltd
# Copyright (C) 2015, 2016 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
Expand Down Expand Up @@ -178,9 +178,15 @@ def __init__(self, name, options):
self.options = options
self.partdir = os.path.join(common.get_partsdir(), self.name)
self.sourcedir = os.path.join(self.partdir, 'src')
self.builddir = os.path.join(self.partdir, 'build')
self.installdir = os.path.join(self.partdir, 'install')

self.build_basedir = os.path.join(self.partdir, 'build')
source_subdir = getattr(self.options, 'source_subdir', None)
if source_subdir:
self.builddir = os.path.join(self.build_basedir, source_subdir)
else:
self.builddir = self.build_basedir

# The API
def pull(self):
"""Pull the source code and/or internal prereqs to build the part.
Expand All @@ -199,25 +205,20 @@ def pull(self):
enhance with custom pull logic.
"""
if getattr(self.options, 'source', None):
sources.get(self.sourcedir, self.builddir, self.options)
sources.get(self.sourcedir, self.build_basedir, self.options)

def build(self):
"""Build the source code retrieved from the pull phase.
The base implementation only copies sourcedir to builddir. Override
this method if you need to process the source code to make it runnable.
The base implementation only copies sourcedir to build_basedir.
Override this method if you need to process the source code to make it
runnable.
"""
if os.path.exists(self.builddir):
shutil.rmtree(self.builddir)

source_subdir = getattr(self.options, 'source_subdir', None)
if source_subdir:
sourcedir = os.path.join(self.sourcedir, source_subdir)
else:
sourcedir = self.sourcedir
if os.path.exists(self.build_basedir):
shutil.rmtree(self.build_basedir)

shutil.copytree(
sourcedir, self.builddir, symlinks=True,
self.sourcedir, self.build_basedir, symlinks=True,
ignore=lambda d, s: common.SNAPCRAFT_FILES
if d is self.sourcedir else [])

Expand Down
42 changes: 18 additions & 24 deletions snapcraft/tests/test_base_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2015 Canonical Ltd
# Copyright (C) 2015, 2016 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
Expand All @@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import tempfile
import unittest.mock

import snapcraft
Expand Down Expand Up @@ -57,48 +56,43 @@ def test_local_non_dir_source_path_must_raise_exception(self, mock_isdir):
self.assertEqual(raised.exception.__str__(),
'local source is not a directory')

def test_build_with_subdir_copies_subdir(self):
def test_build_with_subdir_copies_sourcedir(self):
class Options:
source_subdir = 'src'

plugin = snapcraft.BasePlugin('test-part', Options())
options = Options()
plugin = snapcraft.BasePlugin('test-part', options)

tmpdir = tempfile.TemporaryDirectory()
self.addCleanup(tmpdir.cleanup)
plugin.sourcedir = tmpdir.name
subdir = os.path.join(plugin.sourcedir, plugin.options.source_subdir)
os.mkdir(subdir)
open(os.path.join(subdir, 'file'), 'w').close()
os.makedirs(subdir)
open(os.path.join(plugin.sourcedir, 'file1'), 'w').close()
open(os.path.join(subdir, 'file2'), 'w').close()

tmpdir = tempfile.TemporaryDirectory()
self.addCleanup(tmpdir.cleanup)
plugin.builddir = tmpdir.name
self.assertEqual(
os.path.join(plugin.build_basedir, options.source_subdir),
plugin.builddir)

plugin.build()

self.assertTrue(os.path.exists(os.path.join(plugin.builddir, 'file')))
self.assertTrue(
os.path.exists(os.path.join(plugin.build_basedir, 'file1')))
self.assertTrue(os.path.exists(os.path.join(plugin.builddir, 'file2')))

def test_build_without_subdir_copies_sourcedir(self):
class Options:
pass

plugin = snapcraft.BasePlugin('test-part', Options())

tmpdir = tempfile.TemporaryDirectory()
self.addCleanup(tmpdir.cleanup)
plugin.sourcedir = tmpdir.name
subdir = os.path.join(plugin.sourcedir, 'src')
os.mkdir(subdir)
open(os.path.join(subdir, 'file'), 'w').close()
os.makedirs(plugin.sourcedir)
open(os.path.join(plugin.sourcedir, 'file'), 'w').close()

tmpdir = tempfile.TemporaryDirectory()
self.addCleanup(tmpdir.cleanup)
plugin.builddir = tmpdir.name
self.assertEqual(plugin.build_basedir, plugin.builddir)

plugin.build()

self.assertTrue(os.path.exists(
os.path.join(plugin.builddir, 'src', 'file')))
self.assertTrue(
os.path.exists(os.path.join(plugin.build_basedir, 'file')))


class GetSourceWithBranches(tests.TestCase):
Expand Down

0 comments on commit 34f96cb

Please sign in to comment.