|
| 1 | +import argparse |
| 2 | +import getpass |
| 3 | +import jinja2 |
| 4 | +import os |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | +def get_proxies(): |
| 10 | + '''Pass through proxies to docker container''' |
| 11 | + proxies = '' |
| 12 | + for var in ['http_proxy','https_proxy','no_proxy']: |
| 13 | + if var in os.environ: |
| 14 | + proxies += ' --build-arg %s=%s' % (var,os.environ[var]) |
| 15 | + return proxies |
| 16 | + |
| 17 | +class Templates(): |
| 18 | + '''singleton to render the templates''' |
| 19 | + def __init__(self): |
| 20 | + loader = jinja2.FileSystemLoader(searchpath = '.') |
| 21 | + env = jinja2.Environment(loader=loader) |
| 22 | + self._readme = env.get_template('tpl.README.md') |
| 23 | + self._dockerfile = env.get_template('tpl.Dockerfile') |
| 24 | + |
| 25 | + def render(self, conf): |
| 26 | + name = conf.name() |
| 27 | + with open('%s/README.md' % name,'wb') as fh: |
| 28 | + fh.write(self._readme.render(conf).encode('utf-8')) |
| 29 | + with open('%s/Dockerfile' % name,'wb') as fh: |
| 30 | + fh.write(self._dockerfile.render(conf).encode('utf-8')) |
| 31 | + |
| 32 | +# singleton |
| 33 | +templates = Templates() |
| 34 | + |
| 35 | +def parse_name(name): |
| 36 | + pattern = re.compile(r'intelpython(?P<pyver>[23])_(?P<package>.*)') |
| 37 | + match = pattern.match(name) |
| 38 | + return (int(match.group('pyver')),match.group('package')) |
| 39 | + |
| 40 | +class Conf(dict): |
| 41 | + '''Docker image configuration''' |
| 42 | + def __init__(self,pyver=None,package=None,name=None): |
| 43 | + if name: |
| 44 | + (pyver,package) = parse_name(name) |
| 45 | + self['pyver'] = pyver |
| 46 | + self['package'] = package |
| 47 | + self['release'] = '2017.0.0' |
| 48 | + |
| 49 | + def name(self): |
| 50 | + return 'intelpython%d_%s' % (self['pyver'],self['package']) |
| 51 | + |
| 52 | + def tag(self): |
| 53 | + return '%s/%s' % (getpass.getuser(),self.name()) |
| 54 | + |
| 55 | + def gen(self): |
| 56 | + templates.render(self) |
| 57 | + |
| 58 | + def build(self): |
| 59 | + cmd = 'docker build %s -t %s --file %s/Dockerfile .' % (get_proxies(),self.tag(),self.name()) |
| 60 | + print(' ',cmd) |
| 61 | + subprocess.check_call(cmd, shell=True) |
| 62 | + |
| 63 | + def test(self): |
| 64 | + cmd = 'docker run -t %s python -c 1' % self.tag() |
| 65 | + print(' ',cmd) |
| 66 | + subprocess.check_call(cmd, shell=True) |
| 67 | + |
| 68 | +all_confs = [Conf(2,'core'), |
| 69 | + Conf(2,'full'), |
| 70 | + Conf(3,'core'), |
| 71 | + Conf(3,'full') |
| 72 | +] |
| 73 | + |
| 74 | +def main(): |
| 75 | + conf_names = [conf.name() for conf in all_confs] |
| 76 | + parser = argparse.ArgumentParser(description='generate the configurations for docker images') |
| 77 | + parser.add_argument('--gen', |
| 78 | + action='store_true', |
| 79 | + help='Generate Dockerfile and README.md') |
| 80 | + parser.add_argument('--build', |
| 81 | + action='store_true', |
| 82 | + help='Build docker image') |
| 83 | + parser.add_argument('--test', |
| 84 | + action='store_true', |
| 85 | + help='Test docker image') |
| 86 | + parser.add_argument('conf', |
| 87 | + choices=['all'] + conf_names, |
| 88 | + nargs='*', |
| 89 | + help='list of confs to generate') |
| 90 | + args = parser.parse_args() |
| 91 | + if args.conf[0] == 'all': |
| 92 | + args.conf = conf_names |
| 93 | + |
| 94 | + for n in args.conf: |
| 95 | + print('Processing:',n) |
| 96 | + c = Conf(name=n) |
| 97 | + if args.gen | args.build | args.test: |
| 98 | + print(' gen') |
| 99 | + c.gen() |
| 100 | + if args.build | args.test: |
| 101 | + print(' build') |
| 102 | + c.build() |
| 103 | + if args.test: |
| 104 | + print(' test') |
| 105 | + c.test() |
| 106 | + |
| 107 | + |
| 108 | +main() |
0 commit comments