xref: /petsc/config/BuildSystem/retrieval.py (revision 0a7c9ef60ad58ad54976aa4b924112ee20061f60)
15b6bfdb9SJed Brownfrom __future__ import absolute_import
2179860b2SJed Brownimport logger
3179860b2SJed Brown
4179860b2SJed Brownimport os
5e7c47bf1SJed Browntry:
6e7c47bf1SJed Brown  from urllib import urlretrieve
7e7c47bf1SJed Brownexcept ImportError:
8e7c47bf1SJed Brown  from urllib.request import urlretrieve
9e7c47bf1SJed Browntry:
108f450857SSatish Balay  import urlparse as urlparse_local # novermin
119ad79eecSSatish Balayexcept ImportError:
128f450857SSatish Balay  from urllib import parse as urlparse_local
13179860b2SJed Brownimport config.base
14728600e6SSatish Balayimport socket
15fbfe4939SVaclav Haplaimport shutil
16728600e6SSatish Balay
17179860b2SJed Brown# Fix parsing for nonstandard schemes
188f450857SSatish Balayurlparse_local.uses_netloc.extend(['bk', 'ssh', 'svn'])
19179860b2SJed Brown
20179860b2SJed Brownclass Retriever(logger.Logger):
21179860b2SJed Brown  def __init__(self, sourceControl, clArgs = None, argDB = None):
22179860b2SJed Brown    logger.Logger.__init__(self, clArgs, argDB)
23179860b2SJed Brown    self.sourceControl = sourceControl
24179860b2SJed Brown    self.stamp = None
25179860b2SJed Brown    return
26179860b2SJed Brown
27fbfe4939SVaclav Hapla  def isDirectoryGitRepo(self, directory):
28fbfe4939SVaclav Hapla    from config.base import Configure
29fbfe4939SVaclav Hapla    for loc in ['.git','']:
30fbfe4939SVaclav Hapla      cmd = '%s rev-parse --resolve-git-dir  %s'  % (self.sourceControl.git, os.path.join(directory,loc))
31fbfe4939SVaclav Hapla      (output, error, ret) = Configure.executeShellCommand(cmd, checkCommand = Configure.passCheckCommand, log = self.log)
32fbfe4939SVaclav Hapla      if not ret:
33fbfe4939SVaclav Hapla        return True
34fbfe4939SVaclav Hapla    return False
35fbfe4939SVaclav Hapla
36fbfe4939SVaclav Hapla  @staticmethod
37fbfe4939SVaclav Hapla  def removeTarget(t):
38fbfe4939SVaclav Hapla    if os.path.islink(t) or os.path.isfile(t):
39fbfe4939SVaclav Hapla      os.unlink(t) # same as os.remove(t)
40fbfe4939SVaclav Hapla    elif os.path.isdir(t):
41fbfe4939SVaclav Hapla      shutil.rmtree(t)
42fbfe4939SVaclav Hapla
43fbfe4939SVaclav Hapla  @staticmethod
44fbfe4939SVaclav Hapla  def getDownloadFailureMessage(package, url, filename=None):
45fbfe4939SVaclav Hapla    slashFilename = '/'+filename if filename else ''
46fbfe4939SVaclav Hapla    return '''\
47fbfe4939SVaclav HaplaUnable to download package %s from: %s
48fbfe4939SVaclav Hapla* If URL specified manually - perhaps there is a typo?
49fbfe4939SVaclav Hapla* If your network is disconnected - please reconnect and rerun ./configure
50fbfe4939SVaclav Hapla* Or perhaps you have a firewall blocking the download
51fbfe4939SVaclav Hapla* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually
52fbfe4939SVaclav Hapla* or you can download the above URL manually, to /yourselectedlocation%s
53fbfe4939SVaclav Hapla  and use the configure option:
54fbfe4939SVaclav Hapla  --download-%s=/yourselectedlocation%s
55fbfe4939SVaclav Hapla    ''' % (package.upper(), url, slashFilename, package, slashFilename)
56fbfe4939SVaclav Hapla
57fbfe4939SVaclav Hapla  @staticmethod
58fbfe4939SVaclav Hapla  def removePrefix(url,prefix):
59fbfe4939SVaclav Hapla    '''Replacement for str.removeprefix() supported only since Python 3.9'''
60fbfe4939SVaclav Hapla    if url.startswith(prefix):
61fbfe4939SVaclav Hapla      return url[len(prefix):]
62fbfe4939SVaclav Hapla    return url
63fbfe4939SVaclav Hapla
64*0a7c9ef6SSatish Balay  def genericRetrieve(self, url, root, package, submodules):
65fbfe4939SVaclav Hapla    '''Fetch package from version control repository or tarfile indicated by URL and extract it into root'''
66179860b2SJed Brown
67fbfe4939SVaclav Hapla    parsed = urlparse_local.urlparse(url)
68fbfe4939SVaclav Hapla    if parsed[0] == 'dir':
69fbfe4939SVaclav Hapla      f = self.dirRetrieve
70fbfe4939SVaclav Hapla    elif parsed[0] == 'link':
71fbfe4939SVaclav Hapla      f = self.linkRetrieve
72fbfe4939SVaclav Hapla    elif parsed[0] == 'git':
73fbfe4939SVaclav Hapla      f = self.gitRetrieve
74fbfe4939SVaclav Hapla    elif parsed[0] == 'ssh'   and parsed[2].endswith('.git'):
75fbfe4939SVaclav Hapla      f = self.gitRetrieve
76fbfe4939SVaclav Hapla    elif parsed[0] == 'https' and parsed[2].endswith('.git'):
77fbfe4939SVaclav Hapla      f = self.gitRetrieve
78fbfe4939SVaclav Hapla    elif parsed[0] == 'hg':
79fbfe4939SVaclav Hapla      f = self.hgRetrieve
80fbfe4939SVaclav Hapla    elif parsed[0] == 'ssh' and parsed[1].startswith('hg@'):
81fbfe4939SVaclav Hapla      f = self.hgRetrieve
82fbfe4939SVaclav Hapla    elif os.path.isdir(url):
83fbfe4939SVaclav Hapla      if self.isDirectoryGitRepo(url):
84fbfe4939SVaclav Hapla        f = self.gitRetrieve
85fbfe4939SVaclav Hapla      else:
86fbfe4939SVaclav Hapla        f = self.dirRetrieve
87fbfe4939SVaclav Hapla    else:
88fbfe4939SVaclav Hapla      f = self.tarballRetrieve
89*0a7c9ef6SSatish Balay    return f(url, root, package, submodules)
9052df3566SBarry Smith
91*0a7c9ef6SSatish Balay  def dirRetrieve(self, url, root, package, submodules):
92fbfe4939SVaclav Hapla    self.logPrint('Retrieving %s as directory' % url, 3, 'install')
93fbfe4939SVaclav Hapla    d = self.removePrefix(url, 'dir://')
94fbfe4939SVaclav Hapla    if not os.path.isdir(d): raise RuntimeError('URL %s is not a directory' % url)
9552df3566SBarry Smith
96fbfe4939SVaclav Hapla    t = os.path.join(root,os.path.basename(d))
97fbfe4939SVaclav Hapla    self.removeTarget(t)
98fbfe4939SVaclav Hapla    shutil.copytree(d,t)
9952df3566SBarry Smith
100*0a7c9ef6SSatish Balay  def linkRetrieve(self, url, root, package, submodules):
101fbfe4939SVaclav Hapla    self.logPrint('Retrieving %s as link' % url, 3, 'install')
102fbfe4939SVaclav Hapla    d = self.removePrefix(url, 'link://')
103fbfe4939SVaclav Hapla    if not os.path.isdir(d): raise RuntimeError('URL %s is not pointing to a directory' % url)
1043a911845SSatish Balay
105fbfe4939SVaclav Hapla    t = os.path.join(root,os.path.basename(d))
106fbfe4939SVaclav Hapla    self.removeTarget(t)
107fbfe4939SVaclav Hapla    os.symlink(os.path.abspath(d),t)
1083a911845SSatish Balay
109*0a7c9ef6SSatish Balay  def gitRetrieve(self, url, root, package, submodules):
110fbfe4939SVaclav Hapla    self.logPrint('Retrieving %s as git repo' % url, 3, 'install')
111fbfe4939SVaclav Hapla    if not hasattr(self.sourceControl, 'git'):
112fbfe4939SVaclav Hapla      raise RuntimeError('self.sourceControl.git not set')
113fbfe4939SVaclav Hapla    d = self.removePrefix(url, 'git://')
114fbfe4939SVaclav Hapla    if os.path.isdir(d) and not self.isDirectoryGitRepo(d):
115fbfe4939SVaclav Hapla      raise RuntimeError('URL %s is a directory but not a git repository' % url)
11652df3566SBarry Smith
1171aefc9f4SSatish Balay    newgitrepo = os.path.join(root,'git.'+package)
118fbfe4939SVaclav Hapla    self.removeTarget(newgitrepo)
11952df3566SBarry Smith
120b93f8388SBarry Smith    try:
121*0a7c9ef6SSatish Balay      submodopt =''
122*0a7c9ef6SSatish Balay      for itm in submodules:
123*0a7c9ef6SSatish Balay        submodopt += ' --recurse-submodules='+itm
124*0a7c9ef6SSatish Balay      config.base.Configure.executeShellCommand('%s clone %s %s %s' % (self.sourceControl.git, submodopt, d, newgitrepo), log = self.log)
1255b6bfdb9SJed Brown    except  RuntimeError as e:
126b93f8388SBarry Smith      self.logPrint('ERROR: '+str(e))
127b93f8388SBarry Smith      err = str(e)
128fbfe4939SVaclav Hapla      failureMessage = self.getDownloadFailureMessage(package, url)
129fbfe4939SVaclav Hapla      raise RuntimeError('Unable to clone '+package+'\n'+err+failureMessage)
1305e208ef3SBarry Smith
131*0a7c9ef6SSatish Balay  def hgRetrieve(self, url, root, package, submodules):
132fbfe4939SVaclav Hapla    self.logPrint('Retrieving %s as hg repo' % url, 3, 'install')
133fbfe4939SVaclav Hapla    if not hasattr(self.sourceControl, 'hg'):
134fbfe4939SVaclav Hapla      raise RuntimeError('self.sourceControl.hg not set')
135fbfe4939SVaclav Hapla    d = self.removePrefix(url, 'hg://')
1360c3d3c20SBarry Smith
1370c3d3c20SBarry Smith    newgitrepo = os.path.join(root,'hg.'+package)
138fbfe4939SVaclav Hapla    self.removeTarget(newgitrepo)
139b93f8388SBarry Smith    try:
140fbfe4939SVaclav Hapla      config.base.Configure.executeShellCommand('%s clone %s %s' % (self.sourceControl.hg, d, newgitrepo), log = self.log)
1415b6bfdb9SJed Brown    except  RuntimeError as e:
142b93f8388SBarry Smith      self.logPrint('ERROR: '+str(e))
143b93f8388SBarry Smith      err = str(e)
144fbfe4939SVaclav Hapla      failureMessage = self.getDownloadFailureMessage(package, url)
145fbfe4939SVaclav Hapla      raise RuntimeError('Unable to clone '+package+'\n'+err+failureMessage)
1460c3d3c20SBarry Smith
147*0a7c9ef6SSatish Balay  def tarballRetrieve(self, url, root, package, submodules):
148fbfe4939SVaclav Hapla    parsed = urlparse_local.urlparse(url)
149fbfe4939SVaclav Hapla    filename = os.path.basename(parsed[2])
15015ac2963SJed Brown    localFile = os.path.join(root,'_d_'+filename)
151fbfe4939SVaclav Hapla    self.logPrint('Retrieving %s as tarball to %s' % (url,localFile) , 3, 'install')
15215ac2963SJed Brown    ext =  os.path.splitext(localFile)[1]
15315ac2963SJed Brown    if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']:
154179860b2SJed Brown      raise RuntimeError('Unknown compression type in URL: '+ url)
15515ac2963SJed Brown
156fbfe4939SVaclav Hapla    self.removeTarget(localFile)
157fbfe4939SVaclav Hapla
158fbfe4939SVaclav Hapla    if parsed[0] == 'file' and not parsed[1]:
159fbfe4939SVaclav Hapla      url = parsed[2]
160fbfe4939SVaclav Hapla    if os.path.exists(url):
161fbfe4939SVaclav Hapla      if not os.path.isfile(url):
162fbfe4939SVaclav Hapla        raise RuntimeError('Local path exists but is not a regular file: '+ url)
163fbfe4939SVaclav Hapla      # copy local file
164fbfe4939SVaclav Hapla      shutil.copyfile(url, localFile)
165fbfe4939SVaclav Hapla    else:
166fbfe4939SVaclav Hapla      # fetch remote file
167179860b2SJed Brown      try:
168728600e6SSatish Balay        sav_timeout = socket.getdefaulttimeout()
169728600e6SSatish Balay        socket.setdefaulttimeout(30)
170e7c47bf1SJed Brown        urlretrieve(url, localFile)
171728600e6SSatish Balay        socket.setdefaulttimeout(sav_timeout)
1725b6bfdb9SJed Brown      except Exception as e:
173728600e6SSatish Balay        socket.setdefaulttimeout(sav_timeout)
174fbfe4939SVaclav Hapla        failureMessage = self.getDownloadFailureMessage(package, url, filename)
175179860b2SJed Brown        raise RuntimeError(failureMessage)
17615ac2963SJed Brown
17715ac2963SJed Brown    self.logPrint('Extracting '+localFile)
17815ac2963SJed Brown    if ext in ['.zip','.ZIP']:
17915ac2963SJed Brown      config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log)
18015ac2963SJed Brown      output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log)
181179860b2SJed Brown      dirname = os.path.normpath(output[0].strip())
18215ac2963SJed Brown    else:
18315ac2963SJed Brown      failureMessage = '''\
18415ac2963SJed BrownDownloaded package %s from: %s is not a tarball.
18515ac2963SJed Brown[or installed python cannot process compressed files]
18615ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure
18715ac2963SJed Brown  For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to  http://proxyout.lanl.gov
1880aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually
189b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s
19015ac2963SJed Brown  and use the configure option:
19115ac2963SJed Brown  --download-%s=/yourselectedlocation/%s
1921aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename)
19315ac2963SJed Brown      import tarfile
19415ac2963SJed Brown      try:
19515ac2963SJed Brown        tf  = tarfile.open(os.path.join(root, localFile))
1965b6bfdb9SJed Brown      except tarfile.ReadError as e:
197b95f98c7SJed Brown        raise RuntimeError(str(e)+'\n'+failureMessage)
19815ac2963SJed Brown      if not tf: raise RuntimeError(failureMessage)
1992501eaf6SSatish Balay      #git puts 'pax_global_header' as the first entry and some tar utils process this as a file
2002501eaf6SSatish Balay      firstname = tf.getnames()[0]
2012501eaf6SSatish Balay      if firstname == 'pax_global_header':
2022501eaf6SSatish Balay        firstmember = tf.getmembers()[1]
20315ac2963SJed Brown      else:
2042501eaf6SSatish Balay        firstmember = tf.getmembers()[0]
2052501eaf6SSatish Balay      # some tarfiles list packagename/ but some list packagename/filename in the first entry
2062501eaf6SSatish Balay      if firstmember.isdir():
2072501eaf6SSatish Balay        dirname = firstmember.name
2082501eaf6SSatish Balay      else:
2092501eaf6SSatish Balay        dirname = os.path.dirname(firstmember.name)
21015ac2963SJed Brown      tf.extractall(root)
21115ac2963SJed Brown      tf.close()
21215ac2963SJed Brown
21315ac2963SJed Brown    # fix file permissions for the untared tarballs.
21415ac2963SJed Brown    try:
2152501eaf6SSatish Balay      # check if 'dirname' is set'
2162501eaf6SSatish Balay      if dirname:
217179860b2SJed Brown        config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find  '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log)
2182501eaf6SSatish Balay      else:
2192501eaf6SSatish Balay        self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions')
2205b6bfdb9SJed Brown    except RuntimeError as e:
22115ac2963SJed Brown      raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e))
222179860b2SJed Brown    os.unlink(localFile)
223