File: circleci-download-artifacts.py

package info (click to toggle)
openscad 2021.01-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 35,972 kB
  • sloc: cpp: 53,199; sh: 4,384; ansic: 4,382; python: 1,813; yacc: 853; javascript: 762; lex: 417; lisp: 163; xml: 127; makefile: 118
file content (84 lines) | stat: -rwxr-xr-x 2,932 bytes parent folder | download | duplicates (4)
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
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
#
# Script for retrieving the artifact download URLs from CircleCI via the
# REST API. This fetches all artifacts from the last build for both the
# 32bit (mxe-i686-openscad) and 64bit (mxe-x86_64-openscad) docker builds
#
# The script does not actually download the binaries, that can be done
# using wget or similar tools, e.g.:
#
#   wget $(./circleci-download-artifacts.py)
#
# A cache file in the current folder is used to remember the build
# numbers from the last run. If there are no new builds on CircleCI,
# artifacts attached to those builds will not be reported again. To
# force full download, remove the file (.circleci-last-builds.json).
#
# Currently there is no check for successful build as the latest
# setup still reports docker push failures even though the actual
# build was successful.
#

import re
import json
import urllib3

cache_file = '.circleci-last-builds.json'
circleci_base_url = 'https://circleci.com/api/v1.1/project/github/openscad/openscad'
circleci_build_url = circleci_base_url + '/tree/master'
http = urllib3.PoolManager()

def filter(x, job):
	if x["status"] != 'success':
		return False
	if x["branch"] != 'master':
		return False
	return x["build_parameters"]["CIRCLE_JOB"] == job

def latest_builds():
	response = http.request('GET', circleci_build_url, headers={ 'Accept': 'application/json' })
	data = json.loads(response.data.decode('UTF-8'))
	#print(json.dumps(data, indent=4, sort_keys=True))
	builds32 = [ x["build_num"] for x in data if filter(x, 'openscad-mxe-32bit') ]
	builds64 = [ x["build_num"] for x in data if filter(x, 'openscad-mxe-64bit') ]
	appimages64 = [ x["build_num"] for x in data if filter(x, 'openscad-appimage-64bit') ]
	macos = [ x["build_num"] for x in data if filter(x, 'openscad-macos') ]
	list = zip(['32bit', '64bit', 'appimage-64bit', 'macos'], [builds32, builds64, appimages64, macos])
	builds = { key : max(val) for (key, val) in list if val }
	return builds

def latest_artifacts(builds):
	result = []
	pattern = re.compile('/OpenSCAD-')
	for build in builds:
		response = http.request('GET', circleci_base_url + '/{0}/artifacts'.format(build), headers={ 'Accept': 'application/json' })
		data = json.loads(response.data.decode('UTF-8'))
		urls = [ x["url"] for x in data if re.search(pattern, x["url"]) ]
		result.extend(urls)
	return result

def new_builds():
	try:
		with open(cache_file) as infile:
			last_builds = json.load(infile)
	except Exception:
		last_builds = {}

	builds = latest_builds()
	with open(cache_file, 'w') as outfile:
	    json.dump(builds, outfile)

	new_builds = []
	for key in ['32bit', '64bit', 'appimage-64bit', 'macos']:
		if key not in last_builds or (key in builds and last_builds[key] != builds[key]):
			if key in builds:
				new_builds.append(builds[key])
	return new_builds

def main():
	builds = new_builds()
	artifacts = latest_artifacts(builds)
	for url in artifacts:
		print(url)

main()