Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- 

# 

# Copyright (C) 2015 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 contextlib 

import jsonschema 

import logging 

import os 

import os.path 

import yaml 

 

import snapcraft.lifecycle 

import snapcraft.wiki 

from snapcraft import common 

 

 

logger = logging.getLogger(__name__) 

 

 

_DEPRECATION_LIST = [ 

    'ant-project', 

    'autotools-project', 

    'cmake-project', 

    'go-project', 

    'make-project', 

    'maven-project', 

    'python2-project', 

    'python3-project', 

] 

 

 

@jsonschema.FormatChecker.cls_checks('file-path') 

@jsonschema.FormatChecker.cls_checks('icon-path') 

def _validate_file_exists(instance): 

    return os.path.exists(instance) 

 

 

class SnapcraftYamlFileError(Exception): 

 

    @property 

    def file(self): 

        return self._file 

 

    def __init__(self, yaml_file): 

        self._file = yaml_file 

 

 

class SnapcraftLogicError(Exception): 

 

    @property 

    def message(self): 

        return self._message 

 

    def __init__(self, message): 

        self._message = message 

 

 

class SnapcraftSchemaError(Exception): 

 

    @property 

    def message(self): 

        return self._message 

 

    def __init__(self, message): 

        self._message = message 

 

 

class PluginNotDefinedError(Exception): 

 

    @property 

    def part(self): 

        return self._part 

 

    def __init__(self, part): 

        self._part = part 

 

 

class Config: 

 

    def __init__(self): 

        self.build_tools = [] 

        self.all_parts = [] 

        after_requests = {} 

 

        self.data = _snapcraft_yaml_load() 

        _validate_snapcraft_yaml(self.data) 

 

        self.build_tools = self.data.get('build-packages', []) 

 

        self._wiki = snapcraft.wiki.Wiki() 

 

        for part_name in self.data.get('parts', []): 

            properties = self.data['parts'][part_name] or {} 

 

            plugin_name = properties.pop('plugin', None) 

            # TODO search the wiki 

            if not plugin_name and 'type' in properties: 

                plugin_name = properties.pop('type') 

                logger.warning('DEPRECATED: Use "plugin" instead of "type"') 

            elif not plugin_name: 

                logger.info( 

                    'Searching the wiki to compose part "{}"'.format( 

                        part_name)) 

                with contextlib.suppress(KeyError): 

                    properties = self._wiki.compose(part_name, properties) 

                    plugin_name = properties.pop('plugin', None) 

 

121            if not plugin_name: 

                raise PluginNotDefinedError(part_name) 

 

124            if plugin_name in _DEPRECATION_LIST: 

                plugin_name = plugin_name.rsplit('-project')[0] 

                logger.warning( 

                    'DEPRECATED: plugin names ending in -project are ' 

                    'deprecated. Using {0} instead of {0}-project'.format( 

                        plugin_name)) 

 

            if 'after' in properties: 

                after_requests[part_name] = properties.pop('after') 

 

            properties['stage'] = _expand_filesets_for('stage', properties) 

            properties['snap'] = _expand_filesets_for('snap', properties) 

 

            if 'filesets' in properties: 

                del properties['filesets'] 

 

            self.load_plugin(part_name, plugin_name, properties) 

 

        self._compute_part_dependencies(after_requests) 

        self.all_parts = self._sort_parts() 

 

    def _compute_part_dependencies(self, after_requests): 

        '''Gather the lists of dependencies and adds to all_parts.''' 

 

        for part in self.all_parts: 

            dep_names = after_requests.get(part.name, []) 

            for dep in dep_names: 

                found = False 

                for i in range(len(self.all_parts)): 

                    if dep == self.all_parts[i].name: 

                        part.deps.append(self.all_parts[i]) 

                        found = True 

                        break 

                if not found: 

                    wiki_part = self._wiki.get_part(dep) 

                    found = True if wiki_part else False 

163                    if found: 

                        plugin_name = wiki_part.pop('plugin') 

                        part.deps.append(self.load_plugin( 

                            dep, plugin_name, wiki_part)) 

164                if not found: 

                    raise SnapcraftLogicError( 

                        'part name missing {}'.format(dep)) 

 

    def _sort_parts(self): 

        '''Performs an inneficient but easy to follow sorting of parts.''' 

        sorted_parts = [] 

 

        while self.all_parts: 

            top_part = None 

            for part in self.all_parts: 

                mentioned = False 

                for other in self.all_parts: 

                    if part in other.deps: 

                        mentioned = True 

                        break 

                if not mentioned: 

                    top_part = part 

                    break 

            if not top_part: 

                raise SnapcraftLogicError( 

                    'circular dependency chain found in parts definition') 

            sorted_parts = [top_part] + sorted_parts 

            self.all_parts.remove(top_part) 

 

        return sorted_parts 

 

    def load_plugin(self, part_name, plugin_name, properties): 

        part = snapcraft.lifecycle.load_plugin( 

            part_name, plugin_name, properties) 

 

        self.build_tools += part.code.build_packages 

        self.all_parts.append(part) 

        return part 

 

    def runtime_env(self, root): 

        env = [] 

        env.append('PATH="' + ':'.join([ 

            '{0}/bin', 

            '{0}/usr/bin', 

            '$PATH' 

        ]).format(root) + '"') 

        env.append('LD_LIBRARY_PATH="' + ':'.join([ 

            '{0}/lib', 

            '{0}/usr/lib', 

            '{0}/lib/{1}', 

            '{0}/usr/lib/{1}', 

            '$LD_LIBRARY_PATH' 

        ]).format(root, snapcraft.common.get_arch_triplet()) + '"') 

        return env 

 

    def build_env(self, root): 

        env = [] 

        env.append('CFLAGS="' + ' '.join([ 

            '-I{0}/include', 

            '-I{0}/usr/include', 

            '-I{0}/include/{1}', 

            '-I{0}/usr/include/{1}', 

            '$CFLAGS' 

        ]).format(root, snapcraft.common.get_arch_triplet()) + '"') 

        env.append('CPPFLAGS="' + ' '.join([ 

            '-I{0}/include', 

            '-I{0}/usr/include', 

            '-I{0}/include/{1}', 

            '-I{0}/usr/include/{1}', 

            '$CPPFLAGS' 

        ]).format(root, snapcraft.common.get_arch_triplet()) + '"') 

        env.append('LDFLAGS="' + ' '.join([ 

            '-L{0}/lib', 

            '-L{0}/usr/lib', 

            '-L{0}/lib/{1}', 

            '-L{0}/usr/lib/{1}', 

            '$LDFLAGS' 

        ]).format(root, snapcraft.common.get_arch_triplet()) + '"') 

        env.append('PKG_CONFIG_SYSROOT_DIR={0}'.format(root)) 

        env.append('PKG_CONFIG_PATH=' + ':'.join([ 

            '{0}/usr/lib/pkgconfig', 

            '{0}/usr/lib/{1}/pkgconfig', 

            '{0}/usr/share/pkgconfig', 

            '{0}/usr/local/lib/pkgconfig', 

            '{0}/usr/local/lib/{1}/pkgconfig', 

            '{0}/usr/local/share/pkgconfig', 

            '$PKG_CONFIG_PATH' 

        ]).format(root, snapcraft.common.get_arch_triplet())) 

        env.append('PERL5LIB={0}/usr/share/perl5/'.format(root)) 

        return env 

 

    def build_env_for_part(self, part, root_part=True): 

        """Return a build env of all the part's dependencies.""" 

 

        env = [] 

        stagedir = snapcraft.common.get_stagedir() 

        for dep_part in part.deps: 

            env += dep_part.env(stagedir) 

            env += self.build_env_for_part(dep_part, root_part=False) 

 

        if root_part: 

            env += part.env(part.installdir) 

            env += self.runtime_env(part.installdir) 

            env += self.build_env(part.installdir) 

        else: 

            env += part.env(stagedir) 

            env += self.runtime_env(stagedir) 

            env += self.build_env(stagedir) 

 

        return env 

 

    def stage_env(self): 

        root = common.get_stagedir() 

        env = [] 

 

        env += self.runtime_env(root) 

        env += self.build_env(root) 

        for part in self.all_parts: 

            env += part.env(root) 

 

        return env 

 

    def snap_env(self): 

        root = common.get_snapdir() 

        env = [] 

 

        env += self.runtime_env(root) 

        for part in self.all_parts: 

            env += part.env(root) 

 

        return env 

 

 

def _validate_snapcraft_yaml(snapcraft_yaml): 

    schema_file = os.path.abspath(os.path.join(common.get_schemadir(), 

                                               'snapcraft.yaml')) 

 

    try: 

        with open(schema_file) as fp: 

            schema = yaml.load(fp) 

            format_check = jsonschema.FormatChecker() 

            jsonschema.validate(snapcraft_yaml, schema, 

                                format_checker=format_check) 

    except FileNotFoundError: 

        raise SnapcraftSchemaError( 

            'snapcraft validation file is missing from installation path') 

    except jsonschema.ValidationError as e: 

        raise SnapcraftSchemaError(e.message) 

 

 

def _snapcraft_yaml_load(yaml_file='snapcraft.yaml'): 

    try: 

        with open(yaml_file) as fp: 

            return yaml.load(fp) 

    except FileNotFoundError: 

        raise SnapcraftYamlFileError(yaml_file) 

    except yaml.scanner.ScannerError as e: 

        raise SnapcraftSchemaError( 

            '{} on line {} of {}'.format( 

                e.problem, e.problem_mark.line, yaml_file)) 

 

 

def _expand_filesets_for(stage, properties): 

    filesets = properties.get('filesets', {}) 

    fileset_for_stage = properties.get(stage, {}) 

    new_stage_set = [] 

 

    for item in fileset_for_stage: 

        if item.startswith('$'): 

            try: 

                new_stage_set.extend(filesets[item[1:]]) 

            except KeyError: 

                raise SnapcraftLogicError( 

                    '\'{}\' referred to in the \'{}\' fileset but it is not ' 

                    'in filesets'.format(item, stage)) 

        else: 

            new_stage_set.append(item) 

 

    return new_stage_set