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

# -*- 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/>. 

 

"""The python3 plugin can be used for python 3 based parts. 

 

The python3 plugin can be used for python 3 projects where you would 

want to do: 

 

    - import python modules with a requirements.txt 

    - build a python project that has a setup.py 

    - install sources straight from pip 

 

This plugin uses the common plugin keywords as well as those for "sources". 

For more information check the 'plugins' topic for the former and the 

'sources' topic for the latter. 

 

Additionally, this plugin uses the following plugin-specific keywords: 

 

    - requirements: 

      (string) 

      path to a requirements.txt file 

    - python-packages: 

      (list) 

      A list of dependencies to get from PyPi 

""" 

 

import os 

 

import snapcraft 

 

 

class Python3Plugin(snapcraft.BasePlugin): 

 

    @classmethod 

    def schema(cls): 

        schema = super().schema() 

        schema['properties']['requirements'] = { 

            'type': 'string', 

        } 

        schema['properties']['python-packages'] = { 

            'type': 'array', 

            'minitems': 1, 

            'uniqueItems': True, 

            'items': { 

                'type': 'string' 

            }, 

            'default': [], 

        } 

        schema.pop('required') 

 

        return schema 

 

    def __init__(self, name, options): 

        super().__init__(name, options) 

        self.stage_packages.extend([ 

            'python3-dev', 

            'python3-pkg-resources', 

            'python3-setuptools', 

        ]) 

 

    def env(self, root): 

        return ['PYTHONPATH={}'.format(os.path.join( 

            root, 'usr', 'lib', self.python_version, 'dist-packages'))] 

 

    def pull(self): 

        super().pull() 

        self._pip() 

 

    def _pip(self): 

        setup = 'setup.py' 

        if os.listdir(self.sourcedir): 

            setup = os.path.join(self.sourcedir, 'setup.py') 

 

        if self.options.requirements: 

            requirements = os.path.join(os.getcwd(), self.options.requirements) 

 

        if not os.path.exists(setup) and not \ 

                (self.options.requirements or self.options.python_packages): 

            return 

 

        easy_install = os.path.join( 

            self.installdir, 'usr', 'bin', 'easy_install3') 

        prefix = os.path.join(self.installdir, 'usr') 

        site_packages_dir = os.path.join( 

            prefix, 'lib', self.python_version, 'site-packages') 

 

        if not os.path.exists(site_packages_dir): 

            os.symlink( 

                os.path.join(prefix, 'lib', 'python3', 'dist-packages'), 

                site_packages_dir) 

 

        self.run(['python3', easy_install, '--prefix', prefix, 'pip']) 

 

        pip3 = os.path.join(self.installdir, 'usr', 'bin', 'pip3') 

        pip_install = ['python3', pip3, 'install', '--target', 

                       site_packages_dir] 

 

        if self.options.requirements: 

            self.run(pip_install + ['--requirement', requirements]) 

 

        if self.options.python_packages: 

            self.run(pip_install + ['--upgrade'] + 

                     self.options.python_packages) 

 

        if os.path.exists(setup): 

            self.run(pip_install + ['.', ], cwd=self.sourcedir) 

 

    def build(self): 

        super().build() 

 

        # If setuptools is used, it tries to create files in the 

        # dist-packages dir and import from there, so it needs to exist 

        # and be in the PYTHONPATH. It's harmless if setuptools isn't 

        # used. 

 

        setup_file = os.path.join(self.builddir, 'setup.py') 

        if not os.path.exists(setup_file): 

            return 

 

        os.makedirs(self.dist_packages_dir, exist_ok=True) 

        self.run( 

            ['python3', setup_file, 'install', '--install-layout=deb', 

             '--prefix={}/usr'.format(self.installdir)], cwd=self.builddir) 

 

    @property 

    def dist_packages_dir(self): 

        return os.path.join( 

            self.installdir, 'usr', 'lib', self.python_version, 

            'dist-packages') 

 

    @property 

    def python_version(self): 

        return self.run_output(['py3versions', '-d']) 

 

    def snap_fileset(self): 

        fileset = super().snap_fileset() 

        fileset.append('-usr/bin/pip*') 

        fileset.append('-usr/lib/python*/dist-packages/easy-install.pth') 

        fileset.append('-usr/lib/python*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/*/*/*/*/__pycache__/*.pyc') 

        fileset.append('-usr/lib/python*/*/*/*/*/*/*/*/*/*/__pycache__/*.pyc') 

        return fileset