This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
75 lines (63 loc) · 2.78 KB
/
build.py
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
import os
import re
import click
import warnings
from shub import exceptions as shub_exceptions
from shub.deploy import list_targets
from shub.deploy import _create_default_setup_py
from shub.utils import closest_file, get_config
from shub_image import utils
SHORT_HELP = 'Build release image.'
HELP = """
Build command uses your Dockerfile to build an image and tag it properly.
Internally, this command is a simple wrapper to `docker build` and uses
docker daemon on your system to build an image. Also it can generate
project version for you, and locate root project directory by itself.
Image should be set via scrapinghub.yml, section "images". If version is not
provided, the tool uses VCS-based stamp over project directory (the same as
shub utils itself).
"""
@click.command(help=HELP, short_help=SHORT_HELP)
@click.argument("target", required=False, default="default")
@click.option("-l", "--list-targets", help="list available targets",
is_flag=True, is_eager=True, expose_value=False,
callback=list_targets)
@click.option("-d", "--debug", help="debug mode", is_flag=True)
@click.option("--version", help="release version")
def cli(target, debug, version):
build_cmd(target, version)
def build_cmd(target, version):
client = utils.get_docker_client()
project_dir = utils.get_project_dir()
config = utils.load_release_config()
image = config.get_image(target)
_create_setup_py_if_not_exists()
image_name = utils.format_image_name(image, version)
if not os.path.exists(os.path.join(project_dir, 'Dockerfile')):
raise shub_exceptions.BadParameterException(
'Dockerfile is not found, please use shub-image init cmd')
is_built = False
for data in client.build(path=project_dir, tag=image_name, decode=True):
if 'stream' in data:
utils.debug_log("{}".format(data['stream'][:-1]))
is_built = re.search(
r'Successfully built ([0-9a-f]+)', data['stream'])
elif 'error' in data:
click.echo("Error {}:\n{}".format(
data['error'], data['errorDetail']))
if not is_built:
raise shub_exceptions.RemoteErrorException(
"Build image operation failed")
click.echo("The image {} build is completed.".format(image_name))
def _create_setup_py_if_not_exists():
closest = closest_file('scrapy.cfg')
# create default setup.py only if scrapy.cfg is found, otherwise
# consider it as a non-scrapy/non-python project
if not closest:
warnings.warn("scrapy.cfg is not found")
return
with utils.remember_cwd():
os.chdir(os.path.dirname(closest))
if not os.path.exists('setup.py'):
settings = get_config().get('settings', 'default')
_create_default_setup_py(settings=settings)