xref: /petsc/config/BuildSystem/retrieval.py (revision 728600e61fada40578dd62fa309da7c89a669376)
1179860b2SJed Brownimport logger
2179860b2SJed Brown
3179860b2SJed Brownimport os
4179860b2SJed Brownimport urllib
5179860b2SJed Brownimport urlparse
6179860b2SJed Brownimport config.base
7*728600e6SSatish Balayimport socket
8*728600e6SSatish Balay
9179860b2SJed Brown# Fix parsing for nonstandard schemes
10179860b2SJed Brownurlparse.uses_netloc.extend(['bk', 'ssh', 'svn'])
11179860b2SJed Brown
12179860b2SJed Brownclass Retriever(logger.Logger):
13179860b2SJed Brown  def __init__(self, sourceControl, clArgs = None, argDB = None):
14179860b2SJed Brown    logger.Logger.__init__(self, clArgs, argDB)
15179860b2SJed Brown    self.sourceControl = sourceControl
16179860b2SJed Brown    self.stamp = None
17179860b2SJed Brown    return
18179860b2SJed Brown
19179860b2SJed Brown  def getAuthorizedUrl(self, url):
20179860b2SJed Brown    '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input'''
21179860b2SJed Brown    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
22179860b2SJed Brown    if not location:
23179860b2SJed Brown      url     = urlparse.urlunparse(('', '', path, parameters, query, fragment))
24179860b2SJed Brown      authUrl = None
25179860b2SJed Brown      wasAuth = 0
26179860b2SJed Brown    else:
27179860b2SJed Brown      index = location.find('@')
28179860b2SJed Brown      if index >= 0:
29179860b2SJed Brown        login   = location[0:index]
30179860b2SJed Brown        authUrl = url
31179860b2SJed Brown        url     = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment))
32179860b2SJed Brown        wasAuth = 1
33179860b2SJed Brown      else:
34179860b2SJed Brown        login   = location.split('.')[0]
35179860b2SJed Brown        authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment))
36179860b2SJed Brown        wasAuth = 0
37179860b2SJed Brown    return (url, authUrl, wasAuth)
38179860b2SJed Brown
39179860b2SJed Brown  def testAuthorizedUrl(self, authUrl):
40179860b2SJed Brown    '''Raise an exception if the URL cannot receive an SSH login without a password'''
41179860b2SJed Brown    if not authUrl:
42179860b2SJed Brown      raise RuntimeError('Url is empty')
43179860b2SJed Brown    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl)
44179860b2SJed Brown    return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location)
45179860b2SJed Brown
46179860b2SJed Brown  def genericRetrieve(self, url, root, name):
47179860b2SJed Brown    '''Fetch the gzipped tarfile indicated by url and expand it into root
48179860b2SJed Brown       - All the logic for removing old versions, updating etc. must move'''
49179860b2SJed Brown
5015ac2963SJed Brown    # get the tarball file name from the URL
5115ac2963SJed Brown    filename = os.path.basename(urlparse.urlparse(url)[2])
5215ac2963SJed Brown    localFile = os.path.join(root,'_d_'+filename)
5315ac2963SJed Brown    ext =  os.path.splitext(localFile)[1]
5415ac2963SJed Brown    if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']:
55179860b2SJed Brown      raise RuntimeError('Unknown compression type in URL: '+ url)
56179860b2SJed Brown    self.logPrint('Downloading '+url+' to '+localFile)
57179860b2SJed Brown    if os.path.exists(localFile):
5815ac2963SJed Brown      os.unlink(localFile)
5915ac2963SJed Brown
60179860b2SJed Brown    try:
61*728600e6SSatish Balay      sav_timeout = socket.getdefaulttimeout()
62*728600e6SSatish Balay      socket.setdefaulttimeout(30)
63179860b2SJed Brown      urllib.urlretrieve(url, localFile)
64*728600e6SSatish Balay      socket.setdefaulttimeout(sav_timeout)
65179860b2SJed Brown    except Exception, e:
66*728600e6SSatish Balay      socket.setdefaulttimeout(sav_timeout)
67179860b2SJed Brown      failureMessage = '''\
68179860b2SJed BrownUnable to download package %s from: %s
69179860b2SJed Brown* If URL specified manually - perhaps there is a typo?
70179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure
7115ac2963SJed Brown* Or perhaps you have a firewall blocking the download
72179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s
73179860b2SJed Brown  and use the configure option:
74179860b2SJed Brown  --download-%s=/yourselectedlocation/%s
75179860b2SJed Brown''' % (name, url, filename, name.lower(), filename)
76179860b2SJed Brown      raise RuntimeError(failureMessage)
7715ac2963SJed Brown
7815ac2963SJed Brown    self.logPrint('Extracting '+localFile)
7915ac2963SJed Brown    if ext in ['.zip','.ZIP']:
8015ac2963SJed Brown      config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log)
8115ac2963SJed Brown      output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log)
82179860b2SJed Brown      dirname = os.path.normpath(output[0].strip())
8315ac2963SJed Brown    else:
8415ac2963SJed Brown      failureMessage = '''\
8515ac2963SJed BrownDownloaded package %s from: %s is not a tarball.
8615ac2963SJed Brown[or installed python cannot process compressed files]
8715ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure
8815ac2963SJed Brown  For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to  http://proxyout.lanl.gov
8915ac2963SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s
9015ac2963SJed Brown  and use the configure option:
9115ac2963SJed Brown  --download-%s=/yourselectedlocation/%s
9215ac2963SJed Brown''' % (name, url, filename, name.lower(), filename)
9315ac2963SJed Brown      import tarfile
9415ac2963SJed Brown      try:
9515ac2963SJed Brown        tf  = tarfile.open(os.path.join(root, localFile))
96b95f98c7SJed Brown      except tarfile.ReadError, e:
97b95f98c7SJed Brown        raise RuntimeError(str(e)+'\n'+failureMessage)
9815ac2963SJed Brown      if not tf: raise RuntimeError(failureMessage)
992501eaf6SSatish Balay      #git puts 'pax_global_header' as the first entry and some tar utils process this as a file
1002501eaf6SSatish Balay      firstname = tf.getnames()[0]
1012501eaf6SSatish Balay      if firstname == 'pax_global_header':
1022501eaf6SSatish Balay        firstmember = tf.getmembers()[1]
10315ac2963SJed Brown      else:
1042501eaf6SSatish Balay        firstmember = tf.getmembers()[0]
1052501eaf6SSatish Balay      # some tarfiles list packagename/ but some list packagename/filename in the first entry
1062501eaf6SSatish Balay      if firstmember.isdir():
1072501eaf6SSatish Balay        dirname = firstmember.name
1082501eaf6SSatish Balay      else:
1092501eaf6SSatish Balay        dirname = os.path.dirname(firstmember.name)
11015ac2963SJed Brown      if hasattr(tf,'extractall'): #python 2.5+
11115ac2963SJed Brown        tf.extractall(root)
11215ac2963SJed Brown      else:
11315ac2963SJed Brown        for tfile in tf.getmembers():
11415ac2963SJed Brown          tf.extract(tfile,root)
11515ac2963SJed Brown      tf.close()
11615ac2963SJed Brown
11715ac2963SJed Brown    # fix file permissions for the untared tarballs.
11815ac2963SJed Brown    try:
1192501eaf6SSatish Balay      # check if 'dirname' is set'
1202501eaf6SSatish Balay      if dirname:
121179860b2SJed Brown        config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find  '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log)
1222501eaf6SSatish Balay      else:
1232501eaf6SSatish Balay        self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions')
124179860b2SJed Brown    except RuntimeError, e:
12515ac2963SJed Brown      raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e))
126179860b2SJed Brown    os.unlink(localFile)
127179860b2SJed Brown    return
128179860b2SJed Brown
129179860b2SJed Brown  def ftpRetrieve(self, url, root, name,force):
130179860b2SJed Brown    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install')
131179860b2SJed Brown    return self.genericRetrieve(url, root, name)
132179860b2SJed Brown
133179860b2SJed Brown  def httpRetrieve(self, url, root, name,force):
134179860b2SJed Brown    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install')
135179860b2SJed Brown    return self.genericRetrieve(url, root, name)
136179860b2SJed Brown
137179860b2SJed Brown  def fileRetrieve(self, url, root, name,force):
138179860b2SJed Brown    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install')
139179860b2SJed Brown    return self.genericRetrieve(url, root, name)
140179860b2SJed Brown
141179860b2SJed Brown  def svnRetrieve(self, url, root, name,force):
142179860b2SJed Brown    if not hasattr(self.sourceControl, 'svn'):
143179860b2SJed Brown      raise RuntimeError('Cannot retrieve a SVN repository since svn was not found')
144179860b2SJed Brown    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install')
145179860b2SJed Brown    try:
146179860b2SJed Brown      config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name))
147179860b2SJed Brown    except RuntimeError:
148179860b2SJed Brown      pass
149179860b2SJed Brown
150179860b2SJed Brown
151179860b2SJed Brown  # This is the old code for updating a BK repository
152179860b2SJed Brown  # Stamp used to be stored with a url
153179860b2SJed Brown  def bkUpdate(self):
154179860b2SJed Brown    if not self.stamp is None and url in self.stamp:
155179860b2SJed Brown      if not self.stamp[url] == self.bkHeadRevision(root):
156179860b2SJed Brown        raise RuntimeError('Existing stamp for '+url+' does not match revision of repository in '+root)
157179860b2SJed Brown    (url, authUrl, wasAuth) = self.getAuthorizedUrl(self.getBKParentURL(root))
158179860b2SJed Brown    if not wasAuth:
159179860b2SJed Brown      self.debugPrint('Changing parent from '+url+' --> '+authUrl, 1, 'install')
160179860b2SJed Brown      output = self.executeShellCommand('cd '+root+'; bk parent '+authUrl)
161179860b2SJed Brown    try:
162179860b2SJed Brown      self.testAuthorizedUrl(authUrl)
163179860b2SJed Brown      output = self.executeShellCommand('cd '+root+'; bk pull')
164179860b2SJed Brown    except RuntimeError, e:
165179860b2SJed Brown      (url, authUrl, wasAuth) = self.getAuthorizedUrl(self.getBKParentURL(root))
166179860b2SJed Brown      if wasAuth:
167179860b2SJed Brown        self.debugPrint('Changing parent from '+authUrl+' --> '+url, 1, 'install')
168179860b2SJed Brown        output = self.executeShellCommand('cd '+root+'; bk parent '+url)
169179860b2SJed Brown        output = self.executeShellCommand('cd '+root+'; bk pull')
170179860b2SJed Brown      else:
171179860b2SJed Brown        raise e
172179860b2SJed Brown    return
173179860b2SJed Brown
174179860b2SJed Brown  def bkClone(self, url, root, name):
175179860b2SJed Brown    '''Clone a Bitkeeper repository located at url into root/name
176179860b2SJed Brown       - If self.stamp exists, clone only up to that revision'''
177179860b2SJed Brown    failureMessage = '''\
178179860b2SJed BrownUnable to bk clone %s
179179860b2SJed BrownYou may be off the network. Connect to the internet and run ./configure again
180179860b2SJed Brownor from the directory %s try:
181179860b2SJed Brown  bk clone %s
182179860b2SJed Brownand if that succeeds then rerun ./configure
183179860b2SJed Brown''' % (name, root, url, name)
184179860b2SJed Brown    try:
185179860b2SJed Brown      if not self.stamp is None and url in self.stamp:
186179860b2SJed Brown        (output, error, status) = self.executeShellCommand('bk clone -r'+self.stamp[url]+' '+url+' '+os.path.join(root, name))
187179860b2SJed Brown      else:
188179860b2SJed Brown        (output, error, status) = self.executeShellCommand('bk clone '+url+' '+os.path.join(root, name))
189179860b2SJed Brown    except RuntimeError, e:
190179860b2SJed Brown      status = 1
191179860b2SJed Brown      output = str(e)
192179860b2SJed Brown      error  = ''
193179860b2SJed Brown    if status:
194179860b2SJed Brown      if output.find('ommand not found') >= 0:
195179860b2SJed Brown        failureMessage = 'Unable to locate bk (Bitkeeper) to download repository; make sure bk is in your path'
196179860b2SJed Brown      elif output.find('Cannot resolve host') >= 0:
197179860b2SJed Brown        failureMessage = output+'\n'+error+'\n'+failureMessage
198179860b2SJed Brown      else:
199179860b2SJed Brown        (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
200179860b2SJed Brown        try:
201179860b2SJed Brown          self.bkClone(urlparse.urlunparse(('http', location, path, parameters, query, fragment)), root, name)
202179860b2SJed Brown        except RuntimeError, e:
203179860b2SJed Brown          failureMessage += '\n'+str(e)
204179860b2SJed Brown        else:
205179860b2SJed Brown          return
206179860b2SJed Brown      raise RuntimeError(failureMessage)
207179860b2SJed Brown    return
208179860b2SJed Brown
209179860b2SJed Brown  def bkRetrieve(self, url, root, name):
210179860b2SJed Brown    if not hasattr(self.sourceControl, 'bk'):
211179860b2SJed Brown      raise RuntimeError('Cannot retrieve a BitKeeper repository since BK was not found')
212179860b2SJed Brown    self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via bk', 3, 'install')
213179860b2SJed Brown    (url, authUrl, wasAuth) = self.getAuthorizedUrl(url)
214179860b2SJed Brown    try:
215179860b2SJed Brown      self.testAuthorizedUrl(authUrl)
216179860b2SJed Brown      self.bkClone(authUrl, root, name)
217179860b2SJed Brown    except RuntimeError:
218179860b2SJed Brown      pass
219179860b2SJed Brown    else:
220179860b2SJed Brown      return
221179860b2SJed Brown    return self.bkClone(url, root, name)
222179860b2SJed Brown
223179860b2SJed Brown  def retrieve(self, url, root = None, canExist = 0, force = 0):
224179860b2SJed Brown    '''Retrieve the project corresponding to url
225179860b2SJed Brown    - If root is None, the local root directory is automatically determined. If the project
226179860b2SJed Brown      was already installed, this root is used. Otherwise a guess is made based upon the url.
227179860b2SJed Brown    - If canExist is True and the root exists, an update is done instead of a full download.
228179860b2SJed Brown      The canExist is automatically true if the project has been installed. The retrievalCanExist
229179860b2SJed Brown      flag can also be used to set this.
230179860b2SJed Brown    - If force is True, a full download is mandated.
231179860b2SJed Brown    Providing the root is an easy way to make a copy, for instance when making tarballs.
232179860b2SJed Brown    '''
233179860b2SJed Brown    if root is None:
234179860b2SJed Brown      root = self.getInstallRoot(url)
235179860b2SJed Brown    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
236179860b2SJed Brown    if hasattr(self,scheme+'Retrieve'):
237179860b2SJed Brown      getattr(self, scheme+'Retrieve')(url, os.path.abspath(root), canExist, force)
238179860b2SJed Brown    else:
239179860b2SJed Brown      raise RuntimeError('Invalid transport for retrieval: '+scheme)
240179860b2SJed Brown    return
241179860b2SJed Brown
242179860b2SJed Brown  ##############################################
243179860b2SJed Brown  # This is the old shit
244179860b2SJed Brown  ##############################################
245179860b2SJed Brown  def removeRoot(self, root, canExist, force = 0):
246179860b2SJed Brown    '''Returns 1 if removes root'''
247179860b2SJed Brown    if os.path.exists(root):
248179860b2SJed Brown      if canExist:
249179860b2SJed Brown        if force:
250179860b2SJed Brown          import shutil
251179860b2SJed Brown          shutil.rmtree(root)
252179860b2SJed Brown          return 1
253179860b2SJed Brown        else:
254179860b2SJed Brown          return 0
255179860b2SJed Brown      else:
256179860b2SJed Brown        raise RuntimeError('Root directory '+root+' already exists')
257179860b2SJed Brown    return 1
258179860b2SJed Brown
259179860b2SJed Brown  def getBKParentURL(self, root):
260179860b2SJed Brown    '''Return the parent URL for the BK repository at "root"'''
261179860b2SJed Brown    return self.executeShellCommand('cd '+root+'; bk parent')[21:]
262179860b2SJed Brown
263179860b2SJed Brown  def bkHeadRevision(self, root):
264179860b2SJed Brown    '''Return the last change set revision in the repository'''
265179860b2SJed Brown    return self.executeShellCommand('cd '+root+'; bk changes -and:REV: | head -1')
266179860b2SJed Brown
267179860b2SJed Brown  def bkfileRetrieve(self, url, root, canExist = 0, force = 0):
268179860b2SJed Brown    self.debugPrint('Retrieving '+url+' --> '+root+' via local bk', 3, 'install')
269179860b2SJed Brown    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
270179860b2SJed Brown    return self.bkRetrieve(urlparse.urlunparse(('file', location, path, parameters, query, fragment)), root, canExist, force)
271179860b2SJed Brown
272179860b2SJed Brown  def sshRetrieve(self, url, root, canExist = 0, force = 0):
273179860b2SJed Brown    command = 'hg clone '+url+' '+os.path.join(root,os.path.basename(url))
274179860b2SJed Brown    output  = config.base.Configure.executeShellCommand(command)
275179860b2SJed Brown    return root
276179860b2SJed Brown
277179860b2SJed Brown  def oldRetrieve(self, url, root = None, canExist = 0, force = 0):
278179860b2SJed Brown    '''Retrieve the project corresponding to url
279179860b2SJed Brown    - If root is None, the local root directory is automatically determined. If the project
280179860b2SJed Brown      was already installed, this root is used. Otherwise a guess is made based upon the url.
281179860b2SJed Brown    - If canExist is True and the root exists, an update is done instead of a full download.
282179860b2SJed Brown      The canExist is automatically true if the project has been installed. The retrievalCanExist
283179860b2SJed Brown      flag can also be used to set this.
284179860b2SJed Brown    - If force is True, a full download is mandated.
285179860b2SJed Brown    Providing the root is an easy way to make a copy, for instance when making tarballs.
286179860b2SJed Brown    '''
287179860b2SJed Brown    origUrl = url
288179860b2SJed Brown    url     = self.getMappedUrl(origUrl)
289179860b2SJed Brown    project = self.getInstalledProject(url)
290179860b2SJed Brown    if not project is None and root is None:
291179860b2SJed Brown      root     = project.getRoot()
292179860b2SJed Brown      canExist = 1
293179860b2SJed Brown    if root is None:
294179860b2SJed Brown      root = self.getInstallRoot(origUrl)
295179860b2SJed Brown    (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url)
296179860b2SJed Brown    try:
297179860b2SJed Brown      if self.argDB['retrievalCanExist']:
298179860b2SJed Brown        canExist = 1
299179860b2SJed Brown      return getattr(self, scheme+'Retrieve')(url, os.path.abspath(root), canExist, force)
300179860b2SJed Brown    except AttributeError:
301179860b2SJed Brown      raise RuntimeError('Invalid transport for retrieval: '+scheme)
302