Skip to content

Instantly share code, notes, and snippets.

@robstwd
Created June 22, 2017 20:36
Show Gist options
  • Save robstwd/2875de5d270f3b389c160f46518d9ef1 to your computer and use it in GitHub Desktop.
Save robstwd/2875de5d270f3b389c160f46518d9ef1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import requests
from bs4 import BeautifulSoup
import re
from requests import get
import subprocess as sp
# suppress 'InsecureRequestWarning' warning
# https://stackoverflow.com/a/28002687
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# ======================================================================
URL = "https://build.fhir.org/"
JAR_URL = "https://build.fhir.org/org.hl7.fhir.igpublisher.jar"
JAR_FILE_NAME = "org.hl7.fhir.igpublisher.jar"
GIT_CMD = ['/usr/bin/git', 'log', '-1', '--pretty=%B']
#=======================================================================
def get_content():
"""
"""
try:
print(":: Getting FHIR current build version:")
req = requests.get(URL, verify=False)
return req
except ConnectionError:
message = "Could not connect"
print(message)
def get_version_statement(data):
# get the following footer text
# ®© HL7.org 2011+. FHIR Release 4 Candidate (v3.1.0-12161) generated on Wed, Jun 21, 2017 19:17+0000.
# get the content of the webpage
content = get_content()
soup = BeautifulSoup(content,"lxml")
t = soup.find_all(string=re.compile("\d\.\d\.\d-\d{5}"))[0].lstrip().rstrip()
return t
def get_version_number():
# extract 'v3.1.0-12161' from
# ®© HL7.org 2011+. FHIR Release 4 Candidate (v3.1.0-12161) generated on Wed, Jun 21, 2017 19:17+0000.
data = get_version_statement()
m = re.search(r"v\d\.\d\.\d-\d{5}", data)
return m.group(0)
def get_ig_publisher_jar():
#
print("Downloading IG publisher jar file ...")
with open(JAR_FILE_NAME, "wb") as file:
# get request
response = get(JAR_URL)
# write to file
file.write(response.content)
print("Download complete.")
def get_output(cmd):
'''
Runs a subprocess command to check its output
Returns a byte, therefore needs to be converted to string
and removal of the trailing '\n'
'''
output = sp.check_output(cmd)
output = output.decode(encoding='UTF-8') # convert byte to string
return output.strip() # remove trailing '\n'
def get_last_commit_msg():
# git log -1 --pretty=%B
# need to split on line and retun first line only
msg = get_output(GIT_CMD).split('\n')[0]
return msg
# ======================================================================
if __name__ == '__main__':
# get data
version_statement = get_version_statement(content.text)
current_version_number = get_version_number()
last_commit_msg = get_last_commit_msg()
# if last commit message contains the current version number
if current_version_number in last_commit_msg:
# then exit as the jar file is the latest
print("The publisher jar is up-to-date.\nExiting.")
exit(0)
# the new jar file needs to be downloaded
else:
# download the publisher jar file
print("get new jar")
get_ig_publisher_jar()
# run the publisher to check if no errors
# when no errors
# git add this file
# git commit with the message of the full version statement
# git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment