#!/usr/bin/python

import os
import shutil
import urllib2
import urllib
import traceback
from BeautifulSoup import BeautifulSoup

#BASE_SRC_URL = 'http://cbs.centos.org/repos/virt7-docker-el-candidate/x86_64/os/Packages/'
BASE_SRC_URL = 'http://cbs.centos.org/repos/virt7-docker-el-candidate/source/SRPMS/'
BASE_DEST_DIR = '/home/disk1/repo/arrayepel.72/docker/srpms/'

def _prepare(proj):
    projdir = BASE_DEST_DIR + '/'
    if not os.path.exists(projdir):
        os.mkdir(projdir)
    projurl = BASE_SRC_URL + proj + '/c7/'
    return projurl, projdir

def _parse(resp):
    filelist = []
    try:
        soup = BeautifulSoup(resp)
        res = soup.findAll('a')
        for idx in xrange(len(res)):
            fi = soup('a')[idx]['href']
            if fi[0] != '?' and fi[0] != '/':
                filelist.append(fi)
    except Exception, e:
        print "There is something to resolve ."
        traceback.print_exc(e)
    return filelist

f = urllib2.urlopen(BASE_SRC_URL)
resp = f.read()
fl = _parse(resp)
for fi in fl:
    print fi
    #fullurl = BASE_SRC_URL + fi
    #fulldir = BASE_DEST_DIR + fi
    #print "It will download %s into %s" % (fi, fulldir)
    #urllib.urlretrieve(fullurl, fulldir)
