

@task(max_retries=4, iterations=0)
def sync_with_changelog():
    'Syncronize with pypi changelog.\n\n    Right now we only listen for `new-release`, `remove`, `rename`,\n    and `create` as we do not store any metadata information.\n\n    Following actions can be issued according to pypi source code:\n        new release\t\t\t- Creates a new Release\n        remove\t\t\t\t- Removes a Package from the Shop\n        rename from %(old)s\t\t- Rename a package\n        add %(pyversion)s %(filename)s  - Add a new file to a version\n        remove file %(filename)s        - Remove a file\n        docupdate                       - Notify for documentation update\n        create\t\t\t\t- Create a new package\n        update %(type)s                 - Update some detailed classifiers\n    '
    next_last_sync = timezone.now()
    (state, created) = SyncState.objects.get_or_create(type=SyncState.CHANGELOG)
    epoch = int(time.mktime(state.last_sync.timetuple()))
    client = CheeseShop()
    try:
        log = client.get_changelog(epoch, True)
    except socket.error as exc:
        if (current.iterations == current.max_retries):
            SyncState.objects.filter(type=SyncState.CHANGELOG).update(state=SyncState.STATE_DOWN)
            logger.warning("No sync with PyPi, it's not reachable.")
            return
        else:
            current.iterations += 1
            current.retry(countdown=0, exc=exc)
    else:
        projects = set()
        for (package, version, stamp, action) in log:
            if (action == 'new release'):
                try:
                    pkg = Package.objects.get(name=package)
                except Package.DoesNotExist:
                    pkg = Package.create_with_provider_url(package)
                dt = datetime.datetime.fromtimestamp(stamp)
                release_date = timezone.make_aware(dt, pytz.UTC)
                exists = PackageVersion.objects.filter(package=pkg, version=version).exists()
                if (not exists):
                    update = PackageVersion(version=version, release_date=release_date)
                    pkg.versions.add(update)
                    ProjectDependency.objects.filter(package=pkg).update(update=update)
                projects.update(Project.objects.filter(dependencies__package=pkg).values_list('id', flat=True))
            elif (action == 'remove'):
                try:
                    pkg = Package.objects.get(name=package)
                    ProjectDependency.objects.filter(package=pkg).update(update=None)
                    if (version is None):
                        pkg.versions.all().delete()
                    log_affected_projects(pkg, action='remove_package', type='package', package=pkg)
                except Package.DoesNotExist:
                    pass
            elif (action == 'create'):
                if (not Package.objects.filter(name=stamp).exists()):
                    Package.create_with_provider_url(package)
        for project in projects:
            sync_project.apply(args=(project,))
        SyncState.objects.filter(type=SyncState.CHANGELOG).update(last_sync=next_last_sync, state=SyncState.STATE_RUNNING)

dataset/ETHPy150Open rocketDuck/folivora/folivora/tasks.py sync_with_changelog/VarMisuse@473/856 `package`->`stamp`