# Path: folivora/utils/pypi.py
# DEFAULT_SERVER = 'http://pypi.python.org/pypi/'
# 
# class CheeseShop(object):
# 
#     def __init__(self, server=DEFAULT_SERVER):
#         self.xmlrpc = xmlrpclib.Server(server)
# 
#     def get_package_versions(self, package_name):
#         """Fetch list of available versions for a package.
# 
#         :param package_name: Name of the package to query.
#         """
#         return self.xmlrpc.package_releases(package_name)
# 
#     def get_package_list(self):
#         """Fetch the master list of package names."""
#         return self.xmlrpc.list_packages()
# 
#     def search(self, spec, operator):
#         """Query using search spec."""
#         return self.xmlrpc.search(spec, operator.lower())
# 
#     def get_changelog(self, hours, force_seconds=False):
#         """Query the changelog.
# 
#         :param hours: Hours from now to specify the changelog size.
#         :param force_seconds: Force that `hours` are the seconds to use.
#         """
#         seconds = get_seconds(hours) if not force_seconds else hours
#         return self.xmlrpc.changelog(seconds)
# 
#     def get_updated_releases(self, hours, force_seconds):
#         """Query all updated releases within `hours`.
# 
#         :param hours: Specify the number of hours to find updated releases.
#         :param force_seconds: Force that `hours` are the seconds to use.
#         """
#         seconds = get_seconds(hours) if not force_seconds else hours
#         return self.xmlrpc.updated_releases(seconds)
# 
#     def get_release_urls(self, package_name, version):
#         """Query for all available release urls of `package_name`.
# 
#         :param package_name: Name of the package.
#         :param version: Version of the package.
#         """
#         return self.xmlrpc.release_urls(package_name, version)
# 
#     def get_release_data(self, package_name, version=None):
#         """Query for specific release data.
# 
#         :param package_name: Name of the package.
#         :param version: Version to query the data. If `None`, it's latest
#                         version will be used.
#         """
#         if version is None:
#             versions = self.get_package_versions(package_name)
#             if not versions:
#                 return {}
#             version = versions[-1]
#         return self.xmlrpc.release_data(package_name, version)
# 
# def normalize_name(name):
#     return pkg_resources.safe_name(name).lower()


import time
import urlparse
import datetime
import pytz
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import make_aware, now
from django.contrib.auth.models import User
from django_orm.postgresql import hstore
from .utils.pypi import DEFAULT_SERVER, CheeseShop, normalize_name
        from .tasks import sync_project
    @classmethod
    def create_with_provider_url(cls, name, provider='pypi', url=None):
        if url is None:
            url = urlparse.urljoin(DEFAULT_SERVER, name)
        pkg = cls(name=name, url=url, provider=provider)
        pkg.save()
        return pkg

    def sync_versions(self):
        if self.initial_sync_done:
            return
        client = CheeseShop()
        versions = client.get_package_versions(self.name)
        for version in versions:
            urls = client.get_release_urls(self.name, version)
            if urls:
                url = urls[0]
                utime = time.mktime(url['upload_time'].timetuple())
                release_date = make_aware(
                    datetime.datetime.fromtimestamp(utime),
                    pytz.UTC)
                PackageVersion.objects.get_or_create(
                    package=self,
                    version=version,
                    release_date=release_date)
        self.initial_sync_done = True
        self.save()

    def save(self, *args, **kwargs):
        if not self.normalized_name:

# Path: folivora/utils/pypi.py
# DEFAULT_SERVER = 'http://pypi.python.org/pypi/'
# 
# class CheeseShop(object):
# 
#     def __init__(self, server=DEFAULT_SERVER):
#         self.xmlrpc = xmlrpclib.Server(server)
# 
#     def get_package_versions(self, package_name):
#         """Fetch list of available versions for a package.
# 
#         :param package_name: Name of the package to query.
#         """
#         return self.xmlrpc.package_releases(package_name)
# 
#     def get_package_list(self):
#         """Fetch the master list of package names."""
#         return self.xmlrpc.list_packages()
# 
#     def search(self, spec, operator):
#         """Query using search spec."""
#         return self.xmlrpc.search(spec, operator.lower())
# 
#     def get_changelog(self, hours, force_seconds=False):
#         """Query the changelog.
# 
#         :param hours: Hours from now to specify the changelog size.
#         :param force_seconds: Force that `hours` are the seconds to use.
#         """
#         seconds = get_seconds(hours) if not force_seconds else hours
#         return self.xmlrpc.changelog(seconds)
# 
#     def get_updated_releases(self, hours, force_seconds):
#         """Query all updated releases within `hours`.
# 
#         :param hours: Specify the number of hours to find updated releases.
#         :param force_seconds: Force that `hours` are the seconds to use.
#         """
#         seconds = get_seconds(hours) if not force_seconds else hours
#         return self.xmlrpc.updated_releases(seconds)
# 
#     def get_release_urls(self, package_name, version):
#         """Query for all available release urls of `package_name`.
# 
#         :param package_name: Name of the package.
#         :param version: Version of the package.
#         """
#         return self.xmlrpc.release_urls(package_name, version)
# 
#     def get_release_data(self, package_name, version=None):
#         """Query for specific release data.
# 
#         :param package_name: Name of the package.
#         :param version: Version to query the data. If `None`, it's latest
#                         version will be used.
#         """
#         if version is None:
#             versions = self.get_package_versions(package_name)
#             if not versions:
#                 return {}
#             version = versions[-1]
#         return self.xmlrpc.release_data(package_name, version)
# 
# def normalize_name(name):
#     return pkg_resources.safe_name(name).lower()


# Path: folivora/models.py
import time
import urlparse
import datetime
import pytz
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import make_aware, now
from django.contrib.auth.models import User
from django_orm.postgresql import hstore
from .utils.pypi import DEFAULT_SERVER, CheeseShop, normalize_name
        from .tasks import sync_project

    @classmethod
    def create_with_provider_url(cls, name, provider='pypi', url=None):
        if url is None:
            url = urlparse.urljoin(DEFAULT_SERVER, name)
        pkg = cls(name=name, url=url, provider=provider)
        pkg.save()
        return pkg

    def sync_versions(self):
        if self.initial_sync_done:
            return
        client = CheeseShop()
        versions = client.get_package_versions(self.name)
        for version in versions:
            urls = client.get_release_urls(self.name, version)
            if urls:
                url = urls[0]
                utime = time.mktime(url['upload_time'].timetuple())
                release_date = make_aware(
                    datetime.datetime.fromtimestamp(utime),
                    pytz.UTC)
                PackageVersion.objects.get_or_create(
                    package=self,
                    version=version,
                    release_date=release_date)
        self.initial_sync_done = True
        self.save()

    def save(self, *args, **kwargs):
        if not self.normalized_name:

            self.normalized_name = normalize_name(self.name)