Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

pybuilder / pybuilder / 1516

21 Apr 2018 - 0:20 coverage increased (+0.02%) to 71.823%
1516

Pull #588

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Do make PyB 0.11 work with PIP 10.

Add version-matching hack.

fixes #586
Pull Request #588: Do make PyB 0.11 work with PIP 10.

942 of 1419 branches covered (66.38%)

6 of 6 new or added lines in 1 file covered. (100.0%)

28 existing lines in 10 files now uncovered.

3250 of 4525 relevant lines covered (71.82%)

3.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.82
/src/main/python/pybuilder/scaffolding.py
1
#   -*- coding: utf-8 -*-
2
#
3
#   This file is part of PyBuilder
4
#
5
#   Copyright 2011-2015 PyBuilder Team
6
#
7
#   Licensed under the Apache License, Version 2.0 (the "License");
8
#   you may not use this file except in compliance with the License.
9
#   You may obtain a copy of the License at
10
#
11
#       http://www.apache.org/licenses/LICENSE-2.0
12
#
13
#   Unless required by applicable law or agreed to in writing, software
14
#   distributed under the License is distributed on an "AS IS" BASIS,
15
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
#   See the License for the specific language governing permissions and
17
#   limitations under the License.
18

19
import os
5×
20
import string
5×
21

22
from pybuilder.terminal import print_text_line
5×
23

24
try:
5×
25
    _input = raw_input
5×
UNCOV
26
except NameError:
all except 1516.2 - 4×
UNCOV
27
    _input = input
all except 1516.2 - 4×
28

29
DEFAULT_SOURCE_DIRECTORY = 'src/main/python'
5×
30
DEFAULT_UNITTEST_DIRECTORY = 'src/unittest/python'
5×
31
DEFAULT_SCRIPTS_DIRECTORY = 'src/main/scripts'
5×
32
DEFAULT_DOCS_DIRECTORY = 'docs'
5×
33
PLUGINS_TO_SUGGEST = ['python.flake8', 'python.coverage', 'python.distutils']
5×
34

35

36
def prompt_user(description, default):
5×
37
    message = "{0} (default: '{1}') : ".format(description, default)
!
38
    return _input(message)
!
39

40

41
def collect_project_information():
5×
42
    default_project_name = os.path.basename(os.getcwd())
5×
43
    project_name = prompt_user('Project name', default_project_name) or default_project_name
5×
44
    scaffolding = PythonProjectScaffolding(project_name)
5×
45

46
    dir_source_main_python = prompt_user('Source directory', DEFAULT_SOURCE_DIRECTORY)
5×
47
    dir_docs = prompt_user('Docs directory', DEFAULT_DOCS_DIRECTORY)
5×
48
    dir_source_unittest_python = prompt_user(
5×
49
        'Unittest directory', DEFAULT_UNITTEST_DIRECTORY)
50
    dir_source_main_scripts = prompt_user("Scripts directory", DEFAULT_SCRIPTS_DIRECTORY)
5×
51

52
    plugins = suggest_plugins(PLUGINS_TO_SUGGEST)
5×
53
    scaffolding.add_plugins(plugins)
5×
54

55
    if dir_source_main_python:
Branches [[0, 57]] missed. 5×
56
        scaffolding.dir_source_main_python = dir_source_main_python
5×
57
    if dir_source_unittest_python:
Branches [[0, 59]] missed. 5×
58
        scaffolding.dir_source_unittest_python = dir_source_unittest_python
5×
59
    if dir_source_main_scripts:
Branches [[0, 61]] missed. 5×
60
        scaffolding.dir_source_main_scripts = dir_source_main_scripts
5×
61
    if dir_docs:
Branches [[0, 64]] missed. 5×
62
        scaffolding.dir_docs = dir_docs
5×
63

64
    return scaffolding
5×
65

66

67
def suggest_plugins(plugins):
5×
68
    chosen_plugins = [plugin for plugin in [suggest(plugin) for plugin in plugins] if plugin]
5×
69
    return chosen_plugins
5×
70

71

72
def suggest(plugin):
5×
73
    choice = prompt_user('Use plugin %s (Y/n)?' % plugin, 'y')
5×
74
    plugin_enabled = not choice or choice.lower() == 'y'
5×
75
    return plugin if plugin_enabled else None
5×
76

77

78
def start_project():
5×
79
    try:
!
80
        scaffolding = collect_project_information()
!
81
    except KeyboardInterrupt:
!
82
        print_text_line('\nCanceled.')
!
83
        return 1
!
84

85
    descriptor = scaffolding.render_build_descriptor()
!
86

87
    with open('build.py', 'w') as build_descriptor_file:
!
88
        build_descriptor_file.write(descriptor)
!
89

90
    scaffolding.set_up_project()
!
91
    _create_setup_file()
!
92
    return 0
!
93

94

95
def update_project():
5×
96
    _create_setup_file()
!
97
    return 0
!
98

99

100
def _create_setup_file():
5×
101
    setup_py_file_contents = '''#!/usr/bin/env python
!
102
#
103

104
#   -*- coding: utf-8 -*-
105
#
106
#   This file is part of PyBuilder
107
#
108
#   Copyright 2011-2015 PyBuilder Team
109
#
110
#   Licensed under the Apache License, Version 2.0 (the "License");
111
#   you may not use this file except in compliance with the License.
112
#   You may obtain a copy of the License at
113
#
114
#       http://www.apache.org/licenses/LICENSE-2.0
115
#
116
#   Unless required by applicable law or agreed to in writing, software
117
#   distributed under the License is distributed on an "AS IS" BASIS,
118
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119
#   See the License for the specific language governing permissions and
120
#   limitations under the License.
121

122
#
123
# This script allows to support installation via:
124
#   pip install git+git://<project>@<branch>
125
#
126
# This script is designed to be used in combination with `pip install` ONLY
127
#
128
# DO NOT RUN MANUALLY
129
#
130

131
import os
132
import subprocess
133
import sys
134
import glob
135
import shutil
136

137
from sys import version_info
138
py3 = version_info[0] == 3
139
py2 = not py3
140
if py2:
141
    FileNotFoundError = OSError
142

143
script_dir = os.path.dirname(os.path.realpath(__file__))
144
exit_code = 0
145
try:
146
    subprocess.check_call(["pyb", "--version"])
147
except FileNotFoundError as e:
148
    if py3 or py2 and e.errno == 2:
149
        try:
150
            subprocess.check_call([sys.executable, "-m", "pip.__main__", "install", "pybuilder"])
151
        except subprocess.CalledProcessError as e:
152
            sys.exit(e.returncode)
153
    else:
154
        raise
155
except subprocess.CalledProcessError as e:
156
        sys.exit(e.returncode)
157

158
try:
159
    subprocess.check_call(["pyb", "clean", "install_build_dependencies", "package", "-o"])
160
    dist_dir = glob.glob(os.path.join(script_dir, "target", "dist", "*"))[0]
161
    for src_file in glob.glob(os.path.join(dist_dir, "*")):
162
        file_name = os.path.basename(src_file)
163
        target_file_name = os.path.join(script_dir, file_name)
164
        if os.path.exists(target_file_name):
165
            if os.path.isdir(target_file_name):
166
                shutil.rmtree(target_file_name)
167
            else:
168
                os.remove(target_file_name)
169
        shutil.move(src_file, script_dir)
170
    setup_args = sys.argv[1:]
171
    subprocess.check_call([sys.executable, "setup.py"] + setup_args, cwd=script_dir)
172
except subprocess.CalledProcessError as e:
173
    exit_code = e.returncode
174
sys.exit(exit_code)
175
'''
176
    if os.path.exists("setup.py"):
Branches [[0, 177], [0, 182]] missed. !
177
        choice = prompt_user("Overwrite 'setup.py' (y/N)?", 'n')
!
178
        overwrite = not choice or choice.lower() == 'y'
!
179
        if not overwrite:
Branches [[0, 180], [0, 181]] missed. !
180
            return
!
181
        os.unlink("setup.py")
!
182
    with open('setup.py', 'w') as setup_descriptor_file:
!
183
        setup_descriptor_file.write(setup_py_file_contents)
!
184
    print_text_line("\nCreated 'setup.py'.")
!
185

186

187
class PythonProjectScaffolding(object):
5×
188
    DESCRIPTOR_TEMPLATE = string.Template("""\
5×
189
from pybuilder.core import $core_imports
190

191
$activated_plugins
192

193

194
name = "${project_name}"
195
default_task = "publish"
196

197

198
$initializer
199
""")
200

201
    INITIALIZER_HEAD = '''@init
5×
202
def set_properties(project):
203
'''
204

205
    def __init__(self, project_name):
5×
206
        self.project_name = project_name
5×
207
        self.dir_source_main_python = DEFAULT_SOURCE_DIRECTORY
5×
208
        self.dir_source_unittest_python = DEFAULT_UNITTEST_DIRECTORY
5×
209
        self.dir_source_main_scripts = DEFAULT_SCRIPTS_DIRECTORY
5×
210
        self.dir_docs = DEFAULT_DOCS_DIRECTORY
5×
211
        self.core_imports = ['use_plugin']
5×
212
        self.plugins = ['python.core', 'python.unittest', 'python.install_dependencies']
5×
213
        self.initializer = ''
5×
214

215
    def add_plugins(self, plugins):
5×
216
        self.plugins.extend(plugins)
5×
217

218
    def render_build_descriptor(self):
5×
219
        self.build_initializer()
5×
220
        self.build_imports()
5×
221
        self.core_imports = ', '.join(self.core_imports)
5×
222
        return self.DESCRIPTOR_TEMPLATE.substitute(self.__dict__)
5×
223

224
    def build_imports(self):
5×
225
        self.activated_plugins = '\n'.join(['use_plugin("%s")' % plugin for plugin in self.plugins])
5×
226

227
    def build_initializer(self):
5×
228
        self.core_imports.append('init')
5×
229

230
        properties_to_set = []
5×
231
        if not self.is_default_source_main_python:
5×
232
            properties_to_set.append(('dir_source_main_python', self.dir_source_main_python))
5×
233
        if not self.is_default_source_unittest_python:
5×
234
            properties_to_set.append(('dir_source_unittest_python', self.dir_source_unittest_python))
5×
235
        if not self.is_default_source_main_scripts:
Branches [[0, 236]] missed. 5×
236
            properties_to_set.append(('dir_source_main_scripts', self.dir_source_main_scripts))
!
237
        if not self.is_default_docs:
Branches [[0, 238]] missed. 5×
238
            properties_to_set.append(('dir_docs', self.dir_docs))
!
239

240
        initializer_body = self._build_initializer_body_with_properties(properties_to_set)
5×
241

242
        self.initializer = self.INITIALIZER_HEAD + initializer_body
5×
243

244
    @property
5×
245
    def is_default_source_main_python(self):
246
        return self.dir_source_main_python == DEFAULT_SOURCE_DIRECTORY
5×
247

248
    @property
5×
249
    def is_default_source_unittest_python(self):
250
        return self.dir_source_unittest_python == DEFAULT_UNITTEST_DIRECTORY
5×
251

252
    @property
5×
253
    def is_default_docs(self):
254
        return self.dir_docs == DEFAULT_DOCS_DIRECTORY
5×
255

256
    @property
5×
257
    def is_default_source_main_scripts(self):
258
        return self.dir_source_main_scripts == DEFAULT_SCRIPTS_DIRECTORY
5×
259

260
    def set_up_project(self):
5×
261
        for needed_directory in (self.dir_source_main_python,
5×
262
                                 self.dir_source_unittest_python,
263
                                 self.dir_docs,
264
                                 self.dir_source_main_scripts):
265
            if not os.path.exists(needed_directory):
5×
266
                os.makedirs(needed_directory)
5×
267

268
    @staticmethod
5×
269
    def _build_initializer_body_with_properties(properties_to_set):
270
        initializer_body = ''
5×
271
        initializer_body += '\n'.join(
5×
272
            ['    project.set_property("{0}", "{1}")'.format(k, v) for k, v in properties_to_set])
273

274
        if not initializer_body:
5×
275
            initializer_body += '    pass'
5×
276

277
        return initializer_body
5×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, LLC