xref: /petsc/lib/petsc/bin/petscnagupgrade.py (revision f14a7c29b82d1117d8e3de344377442be395a55f)
1#!/usr/bin/env python3
2#
3#    Nags the user to update to the latest version
4#
5import os
6import os.path, time,sys
7import re
8try:
9  from packaging.version import Version
10except ImportError:
11  try:
12    from distutils.version import LooseVersion as Version
13  except ImportError:
14    sys.exit()
15
16def naggedtoday(file):
17  if not os.path.exists(file): return 0
18  if time.time() - os.path.getmtime(file) > 60*60*24: return 0
19  return 1
20
21def parse_version_h(pv):
22  release  = int(re.compile(' PETSC_VERSION_RELEASE[ ]*([0-9]*)').search(pv).group(1))
23  major    = int(re.compile(' PETSC_VERSION_MAJOR[ ]*([0-9]*)').search(pv).group(1))
24  minor    = int(re.compile(' PETSC_VERSION_MINOR[ ]*([0-9]*)').search(pv).group(1))
25  subminor = int(re.compile(' PETSC_VERSION_SUBMINOR[ ]*([0-9]*)').search(pv).group(1))
26  if release:
27    return Version('%d.%d.%d' % (major, minor, subminor))
28  else:
29    return Version('%d.%d.0rc0' % (major,minor+1))
30
31def currentversion(petscdir):
32  try:
33    with open(os.path.join(petscdir, 'include', 'petscversion.h')) as fd:
34      pv = fd.read()
35    version = parse_version_h(pv)
36  except:
37    return
38  try:
39    try:
40      from urllib.request import urlopen
41    except ImportError:
42      from urllib2 import urlopen
43    # with context manager not support in Python-2; would be preferred in Python-3
44    fd = urlopen("https://gitlab.com/petsc/petsc/raw/release/include/petscversion.h",timeout = 2)
45    pv = fd.read().decode('utf-8')
46    fd.close()
47    aversion = parse_version_h(pv)
48  except:
49    return
50  if aversion > version:
51    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
52    print("The version of PETSc you are using is out-of-date, we recommend updating to the new release")
53    print(" Available Version: "+str(aversion)+"   Installed Version: "+str(version))
54    print("https://petsc.org/release/download/")
55    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
56  try:
57    fd = open(os.path.join(petscdir,'.nagged'),"w")
58    fd.close()
59  except:
60    return
61
62  return 0
63#
64#
65if __name__ ==  '__main__':
66  if 'PETSC_DIR' in os.environ:
67    petscdir = os.environ['PETSC_DIR']
68  elif os.path.exists(os.path.join('.', 'include', 'petscversion.h')):
69    petscdir  = '.'
70  else:
71    sys.exit(0)
72  file     = os.path.join(petscdir,'.nagged')
73  if not naggedtoday(file):
74    currentversion(petscdir)
75