~dholbach/snapcraft/1497582

« back to all changes in this revision

Viewing changes to snapcraft/plugin.py

  • Committer: Snappy Tarmac
  • Author(s): Sergio Schvezov
  • Date: 2015-09-17 12:31:52 UTC
  • mfrom: (169.1.1 snapcraft)
  • Revision ID: snappy_tarmac-20150917123152-v9po07dxtvpyvm1c
Robustness for stage and snap which follow the spec more closely now by sergiusens approved by chipaca

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import importlib
19
19
import logging
20
20
import os
 
21
import shutil
21
22
import sys
22
23
 
23
24
import yaml
213
214
        self.mark_done('build')
214
215
        return True
215
216
 
 
217
    def _migratable_fileset_for(self, stage):
 
218
        plugin_fileset = self.code.snap_fileset()
 
219
        fileset = getattr(self.code.options, stage, ['*']) or ['*']
 
220
        fileset.extend(plugin_fileset)
 
221
        return migratable_filesets(fileset, self.installdir)
 
222
 
216
223
    def stage(self, force=False):
217
224
        if not self.should_stage_run('stage', force):
218
225
            return True
221
228
            return True
222
229
 
223
230
        self.notify_stage("Staging")
224
 
        fileset = getattr(self.code.options, 'stage', ['*']) or ['*']
225
 
        _migrate_files(fileset, self.installdir, self.stagedir)
 
231
        snap_files, snap_dirs = self._migratable_fileset_for('stage')
 
232
 
 
233
        try:
 
234
            _migrate_files(snap_files, snap_dirs, self.installdir, self.stagedir)
 
235
        except FileNotFoundError as e:
 
236
            logger.error('Could not find file %s defined in stage',
 
237
                         os.path.relpath(e.filename, os.path.curdir))
 
238
            return False
 
239
 
226
240
        self.mark_done('stage')
227
241
 
228
242
        return True
233
247
        self.makedirs()
234
248
 
235
249
        self.notify_stage("Snapping")
236
 
        plugin_fileset = self.code.snap_fileset()
237
 
        fileset = getattr(self.code.options, 'snap', ['*']) or ['*']
238
 
        fileset.extend(plugin_fileset)
239
 
        _migrate_files(fileset, self.installdir, self.snapdir)
 
250
        snap_files, snap_dirs = self._migratable_fileset_for('snap')
 
251
 
 
252
        try:
 
253
            _migrate_files(snap_files, snap_dirs, self.stagedir, self.snapdir)
 
254
        except FileNotFoundError as e:
 
255
                logger.error('Could not find file %s defined in snap',
 
256
                             os.path.relpath(e.filename, os.path.curdir))
 
257
                return False
 
258
 
240
259
        self.mark_done('snap')
241
260
 
242
261
        return True
273
292
    return snap_files, snap_dirs
274
293
 
275
294
 
276
 
def _migrate_files(fileset, srcdir, dstdir):
277
 
    snap_files, snap_dirs = migratable_filesets(fileset, srcdir)
 
295
def _migrate_files(snap_files, snap_dirs, srcdir, dstdir):
 
296
    for directory in snap_dirs:
 
297
        os.makedirs(os.path.join(dstdir, directory), exist_ok=True)
278
298
 
279
 
    if snap_dirs:
280
 
        common.run(['mkdir', '-p'] + list(snap_dirs), cwd=dstdir)
281
 
    if snap_files:
282
 
        common.run(['cp', '-a', '--parent'] + list(snap_files) + [dstdir], cwd=srcdir)
 
299
    for snap_file in snap_files:
 
300
        src = os.path.join(srcdir, snap_file)
 
301
        dst = os.path.join(dstdir, snap_file)
 
302
        shutil.copy2(src, dst, follow_symlinks=False)
283
303
 
284
304
 
285
305
def _get_file_list(stage_set):
304
324
def _generate_include_set(directory, includes):
305
325
    include_files = set()
306
326
    for include in includes:
307
 
        matches = glob.glob(os.path.join(directory, include))
308
 
        include_files |= set(matches)
 
327
        if '*' in include:
 
328
            matches = glob.glob(os.path.join(directory, include))
 
329
            include_files |= set(matches)
 
330
        else:
 
331
            include_files |= set([os.path.join(directory, include), ])
309
332
 
310
333
    include_dirs = [x for x in include_files if os.path.isdir(x)]
311
334
    include_files = set([os.path.relpath(x, directory) for x in include_files])