xref: /petsc/config/BuildSystem/retrieval.py (revision e0877f539457ad1ce8178bc0750eac5ffa490a67)
1import logger
2
3import os
4import urllib
5import urlparse
6import config.base
7import socket
8
9# Fix parsing for nonstandard schemes
10urlparse.uses_netloc.extend(['bk', 'ssh', 'svn'])
11
12class Retriever(logger.Logger):
13  def __init__(self, sourceControl, clArgs = None, argDB = None):
14    logger.Logger.__init__(self, clArgs, argDB)
15    self.sourceControl = sourceControl
16    self.stamp = None
17    return
18
19  def getAuthorizedUrl(self, url):
20    '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input'''
21    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
22    if not location:
23      url     = urlparse.urlunparse(('', '', path, parameters, query, fragment))
24      authUrl = None
25      wasAuth = 0
26    else:
27      index = location.find('@')
28      if index >= 0:
29        login   = location[0:index]
30        authUrl = url
31        url     = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment))
32        wasAuth = 1
33      else:
34        login   = location.split('.')[0]
35        authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment))
36        wasAuth = 0
37    return (url, authUrl, wasAuth)
38
39  def testAuthorizedUrl(self, authUrl):
40    '''Raise an exception if the URL cannot receive an SSH login without a password'''
41    if not authUrl:
42      raise RuntimeError('Url is empty')
43    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl)
44    return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location, log = self.log)
45
46  def genericRetrieve(self, url, root, package):
47    '''Fetch the gzipped tarfile indicated by url and expand it into root
48       - All the logic for removing old versions, updating etc. must move'''
49
50    # copy a directory
51    if url.startswith('dir://'):
52      import shutil
53      dir = url[6:]
54      if not os.path.isdir(dir): raise RuntimeError('Url begins with dir:// but is not a directory')
55
56      if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir)))
57      if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir)))
58
59      shutil.copytree(dir,os.path.join(root,os.path.basename(dir)))
60      return
61
62    if url.startswith('git://'):
63      if not hasattr(self.sourceControl, 'git'): return
64      import shutil
65      dir = url[6:]
66      if os.path.isdir(dir):
67        if not os.path.isdir(os.path.join(dir,'.git')): raise RuntimeError('Url begins with git:// and is a directory but but does not have a .git subdirectory')
68
69      newgitrepo = os.path.join(root,'git.'+package)
70      if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo)
71      if os.path.isfile(newgitrepo): os.unlink(newgitrepo)
72
73      config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo, log = self.log)
74      return
75
76    if url.startswith('hg://'):
77      if not hasattr(self.sourceControl, 'hg'): return
78
79      newgitrepo = os.path.join(root,'hg.'+package)
80      if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo)
81      if os.path.isfile(newgitrepo): os.unlink(newgitrepo)
82      config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url[5:]+' '+newgitrepo)
83      return
84
85    if url.startswith('ssh://hg@'):
86      if not hasattr(self.sourceControl, 'hg'): return
87
88      newgitrepo = os.path.join(root,'hg.'+package)
89      if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo)
90      if os.path.isfile(newgitrepo): os.unlink(newgitrepo)
91      config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo)
92      return
93
94    # get the tarball file name from the URL
95    filename = os.path.basename(urlparse.urlparse(url)[2])
96    localFile = os.path.join(root,'_d_'+filename)
97    ext =  os.path.splitext(localFile)[1]
98    if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']:
99      raise RuntimeError('Unknown compression type in URL: '+ url)
100    self.logPrint('Downloading '+url+' to '+localFile)
101    if os.path.exists(localFile):
102      os.unlink(localFile)
103
104    try:
105      sav_timeout = socket.getdefaulttimeout()
106      socket.setdefaulttimeout(30)
107      urllib.urlretrieve(url, localFile)
108      socket.setdefaulttimeout(sav_timeout)
109    except Exception, e:
110      socket.setdefaulttimeout(sav_timeout)
111      failureMessage = '''\
112Unable to download package %s from: %s
113* If URL specified manually - perhaps there is a typo?
114* If your network is disconnected - please reconnect and rerun ./configure
115* Or perhaps you have a firewall blocking the download
116* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s
117  and use the configure option:
118  --download-%s=/yourselectedlocation/%s
119''' % (package.upper(), url, filename, package, filename)
120      raise RuntimeError(failureMessage)
121
122    self.logPrint('Extracting '+localFile)
123    if ext in ['.zip','.ZIP']:
124      config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log)
125      output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log)
126      dirname = os.path.normpath(output[0].strip())
127    else:
128      failureMessage = '''\
129Downloaded package %s from: %s is not a tarball.
130[or installed python cannot process compressed files]
131* If you are behind a firewall - please fix your proxy and rerun ./configure
132  For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to  http://proxyout.lanl.gov
133* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s
134  and use the configure option:
135  --download-%s=/yourselectedlocation/%s
136''' % (package.upper(), url, filename, package, filename)
137      import tarfile
138      try:
139        tf  = tarfile.open(os.path.join(root, localFile))
140      except tarfile.ReadError, e:
141        raise RuntimeError(str(e)+'\n'+failureMessage)
142      if not tf: raise RuntimeError(failureMessage)
143      #git puts 'pax_global_header' as the first entry and some tar utils process this as a file
144      firstname = tf.getnames()[0]
145      if firstname == 'pax_global_header':
146        firstmember = tf.getmembers()[1]
147      else:
148        firstmember = tf.getmembers()[0]
149      # some tarfiles list packagename/ but some list packagename/filename in the first entry
150      if firstmember.isdir():
151        dirname = firstmember.name
152      else:
153        dirname = os.path.dirname(firstmember.name)
154      if hasattr(tf,'extractall'): #python 2.5+
155        tf.extractall(root)
156      else:
157        for tfile in tf.getmembers():
158          tf.extract(tfile,root)
159      tf.close()
160
161    # fix file permissions for the untared tarballs.
162    try:
163      # check if 'dirname' is set'
164      if dirname:
165        config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find  '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log)
166      else:
167        self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions')
168    except RuntimeError, e:
169      raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e))
170    os.unlink(localFile)
171    return
172
173  def ftpRetrieve(self, url, root, name,force):
174    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install')
175    return self.genericRetrieve(url, root, name)
176
177  def httpRetrieve(self, url, root, name,force):
178    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install')
179    return self.genericRetrieve(url, root, name)
180
181  def fileRetrieve(self, url, root, name,force):
182    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install')
183    return self.genericRetrieve(url, root, name)
184
185  def svnRetrieve(self, url, root, name,force):
186    if not hasattr(self.sourceControl, 'svn'):
187      raise RuntimeError('Cannot retrieve a SVN repository since svn was not found')
188    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install')
189    try:
190      config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name), log = self.log)
191    except RuntimeError:
192      pass
193
194
195