copy-release.py 1.16 KB
Newer Older
1 2 3
from django.core.management.base import BaseCommand, CommandError
from stats.models import Release, Category, Module

4

5 6
class Command(BaseCommand):
    help = "Copy an existing release and use master branches"
7

8 9 10
    def add_arguments(self, parser):
        parser.add_argument('release_to_copy')
        parser.add_argument('new_release')
11

12
    def handle(self, **options):
13
        try:
14
            rel_to_copy = Release.objects.get(name=options['release_to_copy'])
15
        except Release.DoesNotExist:
16
            raise CommandError("No release named '%s'" % options['release_to_copy'])
17

18 19 20 21
        new_rel = Release.objects.create(
            name=options['new_release'], description=options['new_release'],
            string_frozen=False, status=rel_to_copy.status
        )
22

23 24 25 26 27 28
        for cat in rel_to_copy.category_set.all():
            if not cat.branch.is_head():
                mod = Module.objects.get(pk=cat.branch.module.id)
                branch = mod.get_head_branch()
            else:
                branch = cat.branch
29
            new_rel.category_set.add(Category(release=new_rel, branch=branch, name=cat.name))
30

31
        return "New release '%s' created" % new_rel.name