import os

import docker

import git

DEFAULT_REPOSITORY = 'git://github.com/dotcloud/docker'
DEFAULT_BRANCH = 'library'


def fetch_buildlist(repository=None, branch=None):
    if repository is None:
        repository = DEFAULT_REPOSITORY
    if branch is None:
        branch = DEFAULT_BRANCH
    #FIXME: set destination folder and only pull latest changes instead of
    # cloning the whole repo everytime
    dst_folder = git.clone_branch(repository, branch)
    for buildfile in os.listdir(os.path.join(dst_folder, 'library')):
        f = open(os.path.join(dst_folder, 'library', buildfile))
        for line in f:
            print buildfile, '--->', line
            args = line.split()
            try:
                #FIXME: delegate to workers instead?
                if len(args) == 1:  # Just a URL, simple mode
                    start_build(args[0], 'refs/heads/master', buildfile)
                elif len(args) == 3:  # docker-tag  url     B:branch or T:tag
                    ref = None
                    if args[2].startswith('B:'):
                        ref = 'refs/heads/' + args[2][2:]
                    elif args[2].startswith('T:'):
                        ref = 'refs/tags/' + args[2][2:]
                    else:
                        raise RuntimeError('Incorrect line format, '
                            'please refer to the docs')
                    start_build(args[1], ref, buildfile, args[0])
            except Exception as e:
                print '!!! ', str(e).strip()
        f.close()


def start_build(repository, ref, docker_repo, docker_tag=None):
    print '---> Cloning {0} (ref: {1})'.format(repository, ref)
    dst_folder = git.clone(repository, ref)
    if not 'Dockerfile' in os.listdir(dst_folder):
        raise RuntimeError('Dockerfile not found in cloned repository')
    f = open(os.path.join(dst_folder, 'Dockerfile'))
    print '---> Building using dockerfile...'
    #img_id, logs = docker.build_context(dst_folder)
    print '--> Committing to library/{0}:{1}'.format(docker_repo,
        docker_tag or 'latest')
    #docker.commit(img_id, 'library/' + docker_repo, docker_tag)
    print '---> Pushing result to the main registry'
    #docker.push('library/' + docker_repo)
    f.close()

@@ -2,16 +2,31 @@
 mport os
+%0Aimport logging
 %0A%0Aimport
@@ -130,16 +130,157 @@
 brary'%0A%0A
+logger = logging.getLogger(__name__)%0Alogging.basicConfig(format='%25(asctime)s %25(levelname)s %25(message)s',%0A                    level='DEBUG')%0A%0A
 %0Adef fet
@@ -444,16 +444,114 @@
 T_BRANCH
+%0A%0A    logger.info('Cloning docker repo from %7B0%7D, branch: %7B1%7D'.format(%0A        repository, branch))
 %0A    #FI
@@ -881,22 +881,51 @@
         
-print 
+logger.debug('%7B0%7D ---%3E %7B1%7D'.format(
 buildfil
@@ -930,21 +930,15 @@
 ile,
- '---%3E',
  line
+))
 %0A   
@@ -1767,35 +1767,26 @@
     
-print '!!! ', str(e).strip(
+logger.exception(e
 )%0A  
@@ -1871,28 +1871,29 @@
 e):%0A    
-print '---%3E 
+logger.info('
 Cloning 
@@ -1927,24 +1927,25 @@
 sitory, ref)
+)
 %0A    dst_fol
@@ -2157,28 +2157,29 @@
 '))%0A    
-print '---%3E 
+logger.info('
 Building
@@ -2199,16 +2199,17 @@
 file...'
+)
 %0A    #im
@@ -2262,19 +2262,21 @@
     
-print '--%3E 
+logger.info('
 Comm
@@ -2353,16 +2353,17 @@
 latest')
+)
 %0A    #do
@@ -2428,20 +2428,21 @@
     
-print '---%3E 
+logger.info('
 Push
@@ -2473,16 +2473,17 @@
 egistry'
+)
 %0A    #do

Use logger instead of prints