Compare commits

..

No commits in common. "master" and "4b319e3f406488f0f5419d64b8cc7b8b57e61a28" have entirely different histories.

3 changed files with 22 additions and 27 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
.vscode/

44
main.py
View File

@ -1,8 +1,6 @@
import requests
import json
import pprint
from datetime import datetime
from pytz import timezone
import psycopg
from psycopg.rows import dict_row
import semver
@ -23,6 +21,8 @@ class jru:
def _db_connect(self,db_config):
try:
#pprint.pprint(db_config)
#print(db_config['db']['server'])
conn = psycopg.connect(
host=db_config['db']['server'],
dbname=db_config['db']['name'],
@ -39,6 +39,7 @@ class jru:
def _get_version_from_db(self,db_conn):
cur = db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select * from jru.java_versions").fetchall()
#print(qres)
if qres:
return qres
else:
@ -48,25 +49,15 @@ class jru:
def _get_version_semver_from_db(self,java_version):
cur = self.db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select latest_release_packaged from jru.java_versions where java_version='{}'".format(java_version)).fetchall()
#print(qres)
if qres:
return qres
else:
return []
def _update_version_in_db(self,java_version,java_semver,update_timestampz):
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}',latest_release_packaged_timestamp = '{}' where java_version = '{}'".format(java_semver,update_timestampz,java_version))
def _update_version_in_db(self,java_version,java_semver):
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}' where java_version = '{}'".format(java_semver,java_version))
self.db_conn.commit()
def _get_version_from_api(self):
url = "https://api.adoptium.net/v3/info/available_releases"
try:
r = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36','Accept': 'application/json'})
available_lts = r.json()["available_lts_releases"]
available_lts_str = [str(i) for i in available_lts]
except Exception as err_json_request:
print(err_json_request)
r = {}
return available_lts_str
def _get_version_from_json(self):
return self.db_config["java_settings"]["enabled_version"]
@ -77,6 +68,7 @@ class jru:
try:
print(" - requesting adoptium : {}".format(url))
r = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36','Accept': 'application/json'})
#pprint.pprint(r.json())
except Exception as err_json_request:
print(err_json_request)
r = {}
@ -86,26 +78,30 @@ class jru:
print("Retrieve Java versions from DB : ")
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
print("Retrieve Java versions from API : ")
# if java_db_version_list:
# for db_vers in java_db_version_list:
# print(' - {}'.format(db_vers))
# else:
# print(" - no version in database yet!")
print("Retrieve Java versions from JSON : ")
#java_config_version = self._get_version_from_json()
java_config_version = self._get_version_from_api()
java_config_version = self._get_version_from_json()
# Inserting in database first time
for json_vers in java_config_version:
print(' - {}'.format(json_vers))
if json_vers not in java_db_version_list:
print('- Java {} is not in database yet!'.format(json_vers))
print('Java {} is not in database yet!'.format(json_vers))
self.db_conn.execute("insert into jru.java_versions (java_version,latest_release_packaged) values ({},'{}.0.0')".format(json_vers,json_vers))
else:
print('- Java {} already found in database'.format(json_vers))
print('Java {} already found in database'.format(json_vers))
self.db_conn.commit()
#updating java_version from database
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
update_timestampz = datetime.now(timezone("Europe/Paris"))
for db_version in java_db_version_list:
print("\nProcessing Java {}".format(db_version))
api_version_info = self._get_latest_release_info(db_version)
@ -113,9 +109,11 @@ class jru:
db_version_semver = self._get_version_semver_from_db(db_version)[0]["latest_release_packaged"]
if semver.VersionInfo.parse(api_version_info_semver) > semver.VersionInfo.parse(db_version_semver):
print(" - New version available! - {} > {}".format(api_version_info_semver,db_version_semver))
self._update_version_in_db(db_version,api_version_info_semver,update_timestampz)
self._update_version_in_db(db_version,api_version_info_semver)
else:
print(' - No upgrade needed! API {} == DB {}'.format(api_version_info_semver,db_version_semver))
if __name__ == "__main__":
jru_main = jru()

View File

@ -1,3 +1 @@
semver
psycopg
pytz
semver