11#!/usr/bin/python
2+ """
3+ Usage:
4+ .travis-run-local.py [--arch=<arch>] [--image=<image> --image-tag=<tag>]
5+
6+ Options:
7+ -h --help Print this help message.
8+ --arch=<arch> Only build in containers with this CPU architecture. [default: any]
9+ --image=<image> Only build for this image name. [default: any]
10+ --image-tag=<tag> Only build for this image tag. [default: any]
11+ """
212
313import logging
414import os
515import subprocess
616import sys
717import time
818import yaml
19+ from docopt import docopt
920
1021def read_travis_yml ():
1122 return yaml .load (open ('.travis.yml' , 'r' ), Loader = yaml .SafeLoader )
@@ -15,7 +26,7 @@ def docker_arch(travis_arch):
1526 return 'arm64v8'
1627 return travis_arch
1728
18- def env_parse (env , arch = None ):
29+ def env_parse (env , arch ):
1930 kv_str = env .split ()
2031 kv = { k : v for k , v in [ s .split ('=' ) for s in kv_str ] }
2132
@@ -29,10 +40,19 @@ def env_parse(env, arch=None):
2940 if 'IMAGE' not in kv or 'IMAGE_TAG' not in kv :
3041 return None
3142
43+ if options ['--arch' ] != 'any' and arch != options ['--arch' ]:
44+ return None
45+
46+ if options ['--image' ] != 'any' and kv ['IMAGE' ] != options ['--image' ]:
47+ return None
48+
49+ if options ['--image-tag' ] != 'any' and kv ['IMAGE_TAG' ] != options ['--image-tag' ]:
50+ return None
51+
3252 # e.g. ubuntu:latest
3353 image = '%s:%s' % (kv ['IMAGE' ], kv ['IMAGE_TAG' ])
3454
35- if arch is not None :
55+ if arch != 'amd64' :
3656 image_prefix = docker_arch (arch ) + '/'
3757 os .environ ['IMAGE_PREFIX' ] = image_prefix
3858 return image_prefix + image
@@ -41,11 +61,17 @@ def env_parse(env, arch=None):
4161
4262def get_images (travis ):
4363 for env in travis ['env' ]['global' ]:
44- env_parse (env )
64+ env_parse (env , travis ['arch' ])
65+
4566 for env in travis ['env' ]['jobs' ]:
46- yield env_parse (env )
67+ image = env_parse (env , travis ['arch' ])
68+ if image is not None :
69+ yield image
70+
4771 for job in travis ['jobs' ]['include' ]:
48- yield env_parse (job ['env' ], job ['arch' ])
72+ image = env_parse (job ['env' ], job ['arch' ])
73+ if image is not None :
74+ yield image
4975
5076def docker_pull (image ):
5177 subprocess .run (['docker' , 'pull' , image ], check = True )
@@ -91,9 +117,12 @@ def kill_and_wait():
91117 log .info ("Container has exited." )
92118
93119def main ():
120+ global options
94121 global log
95122 log = init_logging ()
96123
124+ options = docopt (__doc__ )
125+
97126 log .info ("Parsing Travis configuration file" )
98127 travis = read_travis_yml ()
99128
0 commit comments