platform-impending-eol.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. '''Check if a given distro is going to be EOL soon.
  3. This queries the public API of https://endoflife.date to fetch EOL dates.
  4. ‘soon’ is defined by LEAD_DAYS, currently 30 days.'''
  5. import datetime
  6. import json
  7. import sys
  8. import urllib.request
  9. URL_BASE = 'https://endoflife.date/api'
  10. NOW = datetime.date.today()
  11. LEAD_DAYS = datetime.timedelta(days=30)
  12. DISTRO = sys.argv[1]
  13. RELEASE = sys.argv[2]
  14. LTS = sys.argv[3]
  15. EXIT_NOT_IMPENDING = 0
  16. EXIT_IMPENDING = 1
  17. EXIT_NO_DATA = 2
  18. EXIT_FAILURE = 3
  19. try:
  20. with urllib.request.urlopen(f'{ URL_BASE }/{ DISTRO }/{ RELEASE }.json') as response:
  21. match response.status:
  22. case 200:
  23. data = json.load(response)
  24. case _:
  25. print(
  26. f'Failed to retrieve data for { DISTRO } { RELEASE } ' +
  27. f'(status: { response.status }).',
  28. file=sys.stderr
  29. )
  30. sys.exit(EXIT_FAILURE)
  31. except urllib.error.HTTPError as e:
  32. match e.code:
  33. case 404:
  34. print(f'No data available for { DISTRO } { RELEASE }.', file=sys.stderr)
  35. sys.exit(EXIT_NO_DATA)
  36. case _:
  37. print(
  38. f'Failed to retrieve data for { DISTRO } { RELEASE } ' +
  39. f'(status: { e.code }).',
  40. file=sys.stderr
  41. )
  42. sys.exit(EXIT_FAILURE)
  43. if LTS == '1' and 'extendedSupport' in data:
  44. ref = 'extendedSupport'
  45. else:
  46. ref = 'eol'
  47. LTS = False
  48. eol = datetime.date.fromisoformat(data[ref])
  49. offset = abs(eol - NOW)
  50. if offset <= LEAD_DAYS:
  51. if LTS:
  52. print(data['extendedSupport'])
  53. else:
  54. print(data['eol'])
  55. sys.exit(EXIT_IMPENDING)
  56. else:
  57. sys.exit(EXIT_NOT_IMPENDING)