Skip to content

Commit 1778cdb

Browse files
committed
Add build image script [skip ci]
1 parent 9ff6b6d commit 1778cdb

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

scripts/build-image.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'''
2+
Builds a docker image for the given dockerfile and idris version for local testing
3+
'''
4+
5+
import argparse
6+
import requests
7+
import subprocess
8+
9+
10+
def get_latest_sha():
11+
idris_latest_sha = requests.get('https://api.github.com/repos/idris-lang/Idris2/commits').json()[0]['sha']
12+
lsp_latest_sha = requests.get('https://api.github.com/repos/idris-community/idris2-lsp/commits').json()[0]['sha']
13+
return {
14+
'idris': idris_latest_sha,
15+
'lsp': lsp_latest_sha
16+
}
17+
18+
19+
def get_lsp_version(version: str):
20+
'''
21+
Given an idris version, return the corresponding lsp version
22+
If the version is not supported by the LSP, exit with an error
23+
'''
24+
version_map = {
25+
'latest': 'latest',
26+
'v0.7.0': 'idris2-0.7.0',
27+
'v0.6.0': 'idris2-0.6.0',
28+
'v0.5.1': 'idris2-0.5.1',
29+
}
30+
if version not in version_map:
31+
print(f'Idris2 version {version} not supported in LSP')
32+
exit(1)
33+
return version_map[version]
34+
35+
36+
def build_image_sha(image: str, sha_info: dict, idris_base_version: str, tag: str):
37+
dockerfile = f'{image}-sha.Dockerfile'
38+
print(f'Building {dockerfile} with tag {tag}')
39+
if image == 'devcontainer':
40+
# for sha-specific devcontainer images, we also need to pass in the sha from the idris2 github repo
41+
# by default, this is the latest idris2 sha.
42+
idris_sha = sha_info['idris'] if idris_base_version == 'latest' else idris_base_version
43+
subprocess.run([ 'docker', 'build', '-t', tag, '-f', dockerfile,
44+
'--build-arg', f'IDRIS_SHA={idris_sha}',
45+
'--build-arg', f'IDRIS_LSP_SHA={sha_info["lsp"]}',
46+
'--build-arg', f'IDRIS_VERSION={idris_base_version}',
47+
'.' ])
48+
else:
49+
subprocess.run([ 'docker', 'build', '-t', tag, '-f', dockerfile,
50+
'--build-arg', f'IDRIS_SHA={sha_info["idris"]}', '.' ])
51+
print(f'Image built with tag {tag}')
52+
53+
54+
if __name__ == '__main__':
55+
parser = argparse.ArgumentParser(description='Builds a docker image')
56+
parser.add_argument(
57+
'--image',
58+
help='The image to build. One of (base | debian | ubuntu | devcontainer). Defaults to base.',
59+
default='base')
60+
group = parser.add_mutually_exclusive_group()
61+
group.add_argument(
62+
'--version',
63+
help='Idris version to use. Defaults to `latest`, and of the form `v0.6.0`',
64+
default='latest')
65+
group.add_argument(
66+
'--sha',
67+
help='Idris/Idris LSP SHA to use. Should not be used with `--version`. SHAs also cannot be used with base or debian images.',
68+
default=None)
69+
parser.add_argument(
70+
'--tag',
71+
help='Tag to use for the image. Defaults to `{image}-{version}` or `{image}-{tag}.',
72+
default=None)
73+
parser.add_argument(
74+
'--idris_base_version',
75+
help='Only used for devcontainer sha images. Version of idris to build the LSP against. Defaults to `latest`, and in the form `v0.6.0`',
76+
default='latest'
77+
)
78+
args = parser.parse_args()
79+
80+
if args.image not in ['base', 'debian', 'ubuntu', 'devcontainer']:
81+
print('Invalid image. Must be one of (base | debian | ubuntu | devcontainer).')
82+
exit(1)
83+
84+
if args.version and args.version != 'latest':
85+
# Build versioned image.
86+
dockerfile = f'{parser.image}.Dockerfile'
87+
tag = f'{parser.image}-{parser.version}' if not args.tag else args.tag
88+
print(f'Building {dockerfile} with tag {tag}')
89+
90+
# build image
91+
if args.image == 'devcontainer':
92+
lsp_version = get_lsp_version(args.version)
93+
subprocess.run([ 'docker', 'build', '-t', tag, '-f', dockerfile,
94+
'--build-arg', f'IDRIS_VERSION={args.version}',
95+
'--build-arg', f'IDRIS_LSP_VERSION={lsp_version}',
96+
'.' ])
97+
else:
98+
subprocess.run([ 'docker', 'build', '-t', tag, '-f', dockerfile, '--build-arg', f'IDRIS_VERSION={args.version}', '.' ])
99+
print(f'Image built with tag {tag}')
100+
101+
elif args.version == 'latest':
102+
sha_info = get_latest_sha()
103+
tag = f'{args.image}-latest' if not args.tag else args.tag
104+
build_image_sha(args.image, sha_info, args.idris_base_version, tag)
105+
106+
elif args.sha:
107+
if args.image in ['base', 'debian']:
108+
print('Cannot build base or debian images with a sha.')
109+
exit(1)
110+
111+
sha_info = {
112+
'idris': args.sha,
113+
'lsp': args.sha
114+
}
115+
tag = f'{args.image}-{args.sha}' if not args.tag else args.tag
116+
build_image_sha(args.image, sha_info, args.idris_base_version, tag)
117+
118+
else:
119+
print('This should never happen.')
120+
exit(1)

0 commit comments

Comments
 (0)