Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c554a48143
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 556 lines (435 sloc) 18.014 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
'''
Created on Aug 2, 2012

@author: Prashanth
'''

import urllib2
import ftplib
import re
import Util
import xml.dom.minidom as minidom
    
from urlparse import urlparse
from BeautifulSoup import BeautifulSoup


class Upstream(object):
    '''
classdocs
'''

    def getPageLinks(self, url):
        
        parsedUrl=urlparse(url)
        
        scheme=parsedUrl.scheme
        hostname=parsedUrl.hostname
        path=parsedUrl.path
        
        error=None
        links=[]
        
        if scheme=='http' or scheme=='https':
            try:
                data=urllib2.urlopen(url).read()
            except Exception, e:
                error='Unable to open URL'
                return None, error
            
            soup=BeautifulSoup(data)
            soup.prettify()
            
            try:
                for link in soup.findAll('a')+soup.findAll('file'):
                    links.append(scheme+'://'+hostname+path+link['href'])
            except:
                try:
                    for link in soup.findAll('a'):
                        links.append(scheme+'://'+hostname+path+str(link.contents).replace('\n','').strip())
                except:
                    pass
            
            if len(links)>0:
                return links, error
            else:
                error='No links found'
                return None, error
        
        if scheme=='ftp':
            
            files = []
            
            try:
                ftp = ftplib.FTP(hostname)
                ftp.login("anonymous", "")
                ftp.cwd(path)
            except Exception, e:
                error='Unable to open URL'
                return None, error
            
            try:
                files = ftp.nlst()
                for file in files:
                    links.append(scheme+'://'+hostname+path+'/'+file)
            except ftplib.error_perm, resp:
                if str(resp) == "550 No files found":
                    print "No files in this directory"
                else:
                    raise
            return links, error
    
    def getVersions(self, links, regex):
        
        newLinks=[]
        error=None
        
        for link in links:
            
            while link.find('..')>=0:
                link.replace('..','.')
            
            if link!='.':
                try:
                    match=re.match(regex, link, re.IGNORECASE)
                    if match.group(0).find('/')>=0:
                        newLinks.append(match.group(1).split('/')[-1])
                    else:
                        newLinks.append(match.group(1))
                except Exception, e:
                    pass
            
        if len(newLinks)==0:
            error='Unable to process links'
            return None, error
        else:
            return newLinks, error
        
    def getLatestVersion(self, versions, branch):
        
        latestVerList=[]
        
        util=Util.Util()
# latestVer=versions[0].replace('-','.').replace('_','.')
#
# for version in versions:
# if util.version_gt(version.replace('-','.').replace('_','.'), latestVer):
# latestVer=version
# latestVerList.append(latestVer)
#
# for x in range(0,int(latestVer.split('.')[0]+latestVer.split('.')[1])):
# latestVer='0.0'
# for version in versions:
# if int(version.replace('-','.').replace('_','.').split('.')[0])==x:
# if util.version_gt(version.replace('-','.').replace('_','.'), latestVer):
# latestVer=version
# if latestVer!='0.0':
# latestVerList.append(latestVer)

        if branch=='latest':
            latestVer=versions[0]
            for version in versions:
                cleanVer=util.cleanVerStr(version)
                if util.version_gt(cleanVer, util.cleanVerStr(latestVer)):
                    latestVer=version
            latestVerList.append(latestVer)
        else:
            for eachBranch in branch.split(','):
                latestVer=util.cleanVerStr(versions[0])
                for version in versions:
                    br=eachBranch.strip().replace('.','\.')
                    br=br+'.*'
                    x=re.match(br, version)
                    if x:
                        cleanVer=util.cleanVerStr(version)
                        if util.version_gt(cleanVer, latestVer):
                            latestVer=version
                latestVerList.append(latestVer)
                
        if len(latestVerList)>0:
            return sorted(latestVerList), None
        else:
            return None, 'Unable to find Latest Version'
    
    def getLocation(self, links, latestVer):
        for link in links:
            if link.find(latestVer)>=0:
                if link.find('.tar')>=0:
                    return link, None
                
        return None, 'Unable to find Location'
    
class HTTPLS(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
            
        return latestVer, locs, None

class SF(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url='http://sourceforge.net/api/file/index/project-name/'+url+'/rss'
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        doc = minidom.parseString(urllib2.urlopen(self.url).read())
        items = doc.getElementsByTagName('item')
        
        links=[]
        for item in items:
            linkObj=item.getElementsByTagName('link')[0]
            nodes = linkObj.childNodes
            for node in nodes:
                if node.nodeType == node.TEXT_NODE:
                    try:
                        link=node.data.replace('/download','')
                        links.append(link)
                    except Exception, e:
                        pass
        
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+).*\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        
        newVersions=[]
        for version in versions:
            m=re.sub('-','',re.sub('[a-zA-Z-_]', '', version))
            m=m.replace('..','.')
            newVersions.append(m)
            
        (latestVer, error) = self.getLatestVersion(newVersions, self.branch)
        if error:
            return None, None, error
        
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
        
        
        return latestVer, locs, None

    
class DualHTTPLS(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url1=url.split('|')[0]
        self.url2=url.split('|')[1]
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links1, error)=self.getPageLinks(self.url1)
        if error:
            return None, None, error
        (links2, error)=self.getPageLinks(self.url1)
        if error:
            return None, None, error
        links=links1+links2
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname.lower()+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
        
        return latestVer, locs, None
    
class FTPLS(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
            
        return latestVer, locs, None
    
class Google(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url='http://code.google.com/p/'+url.strip()+'/downloads/list'
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            location=urlparse(self.url).scheme+'://'+location.split('//')[2]
            locs.append(location)
        
        return latestVer, location, None
    
class Launchpad(Upstream):

    def __init__(self, url, pkgname, branch):
        
        self.url='http://launchpad.net/'+url.strip()+'/+download'
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            location=location.replace('http://launchpad.net/'+self.pkgname+'/+download','')
            locs.append(location)
        return latestVer, location, None
    
class SVNLS(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        (location, error) = self.getLocation(links, latestVer[0])
        return latestVer, location, None
    
class Trac(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        self.branch=branch
        
    def process(self):
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
        return latestVer, location, None
    
class SubdirHTTPLS(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        
        if branch=='latest':
            self.branch=''
        else:
            self.branch=branch
        
    def process(self):
        
        flag=0
        
        (links, error)=self.getPageLinks(self.url)
        if error:
            return None, None, error
        
        while True:
            (versions, error)=self.getVersions(links, '^.*/([\d\.]+)/')
            if error:
                return None, None, error
            (latestVer, error) = self.getLatestVersion(versions, self.branch)
            
            self.url=self.url+latestVer[0]+'/'
            
            (links, error)=self.getPageLinks(self.url)
            if error:
                return None, None, error
            
            for link in links:
                if link.find(self.pkgname)>=0 and link.find('.tar')>=0:
                    flag=1
                    break
                
            if flag==1:
                break
            
        (versions, error)=self.getVersions(links, '^.*'+self.pkgname+'[_-](([0-9]+[\.\-])*[0-9]+)\.(?:tar.*|t[bg]z2?).*$')
        if error:
            return None, None, error
        (latestVer, error) = self.getLatestVersion(versions, self.branch)
        if error:
            return None, None, error
        locs=[]
        for ver in latestVer:
            (location, error) = self.getLocation(links, ver)
            if error:
                return None, None, error
            locs.append(location)
        return latestVer, location, None
    
class Custom(Upstream):
    
    def __init__(self, url, pkgname, branch):
        
        self.url=url
        self.pkgname=pkgname
        self.branch=branch
        
    def parseRegex(self, path, regex):
        
        (links, error)=self.getPageLinks(path)
        if error:
            return None, None, error
        
        (versions, error)=self.getVersions(links, '^.*/'+regex)
        if error:
            return None, None, error
        
        newVersions=[]
        for version in versions:
            m=re.sub('-','',re.sub('[a-zA-Z-_]', '', version))
            m=m.replace('..','.')
            newVersions.append(m)
        
        (latestVer, error) = self.getLatestVersion(newVersions,self.branch)
        if error:
            return None, None, error
        
        (location, error) = self.getLocation(links, latestVer[0])
        
        return latestVer, location, None
    
    def processSf(self):
        regex=self.url.split('/')[-1]
        doc = minidom.parseString(urllib2.urlopen('/'.join(self.url.split('/')[:-1])).read())
        items = doc.getElementsByTagName('item')
        
        links=[]
        for item in items:
            linkObj=item.getElementsByTagName('link')[0]
            nodes = linkObj.childNodes
            for node in nodes:
                if node.nodeType == node.TEXT_NODE:
                    try:
                        link=node.data.replace('/download','')
                        match=re.match('^.*/'+regex, link)
                        links.append(match.group(0))
                    except Exception, e:
                        pass
                    
        (versions, error)=self.getVersions(links, '^.*/'+regex)
        if error:
            return None, None, error
        
        newVersions=[]
        for version in versions:
            m=re.sub('-','',re.sub('[a-zA-Z-_]', '', version))
            m=m.replace('..','.')
            newVersions.append(m)
            
        (latestVer, error) = self.getLatestVersion(newVersions, self.branch)
        if error:
            return None, None, error
        
        (location, error) = self.getLocation(links, latestVer[0])
        if error:
            return None, None, error
        
        return latestVer, location, None
        
    def process(self):
        
        parsedUrl=urlparse(self.url)
        downloadDir=parsedUrl.scheme+'://'+parsedUrl.hostname
        
        if parsedUrl.hostname.find('sf.net')>=0 or parsedUrl.hostname.find('sourceforge.net')>=0:
            return self.processSf()
            
        for dir in parsedUrl.path.split('/'):
            if dir.find('(')>=0 or dir.find(')')>=0:
                
                if parsedUrl.hostname=='code.google.com':
                    if downloadDir[-1]=='/':
                        downloadDir=downloadDir[:-1]
                        
                (latestVer, loc, error)=self.parseRegex(downloadDir, dir)
                if loc!=None and loc.find('/')>=0:
                    downloadDir+=loc.split('/')[-1]
                else:
                    downloadDir+=latestVer[0]+'/'
            else:
                downloadDir+=dir+'/'
        
        return latestVer, downloadDir, error
Something went wrong with that request. Please try again.