#!/usr/bin/python

import os, sys

from osc.core import *
from osc.conf import *

conf.get_config()
apiurl=conf.config['apiurl']

PROJECT='openSUSE:Factory'
CACHE='packages.todo'

def is_source_link(apiurl, project, package):
    return '_link' in meta_get_filelist(apiurl, project, package)

if not os.path.isfile(CACHE):
    with open(CACHE, 'w') as cf:
        lst = [p for p in meta_get_packagelist(apiurl, PROJECT) if '_product' not in p]
        cf.write("\n".join(lst))

if os.path.isfile(CACHE):
    with open(CACHE, 'r') as cf:
        packages = [p.strip() for p in cf.readlines()]
else:
    print >>sys.stderr, "Cache file not found!"
    sys.exit(1)

for i, package in enumerate(packages):
    devel_project = show_develproject(apiurl, PROJECT, package)
    if not devel_project:
        continue
    try:
        if not is_source_link(apiurl, devel_project, package):
            print "%s/%s" % (devel_project, package)
    except urllib2.HTTPError, http_error:
        print "NOT FOUND: %s/%s" % (devel_project, package)
    except SyntaxError:
        print "SyntaxError: %s/%s" % (devel_project, package)
    except Exception, exc:
        with open(CACHE, 'w') as cf:
            cf.write("\n".join(packages[i:]))
        print >>sys.stderr, "An exception has been occured, the rest of the work has been save in %s, please rerun the program" % CACHE
        print >>sys.stderr, exc
        sys.exit(2)

if os.path.isfile(CACHE):
    os.unlink(CACHE)
