1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

# Copyright 2013 The Distro Tracker Developers 

# See the COPYRIGHT file at the top-level directory of this distribution and 

# at https://deb.li/DTAuthors 

# 

# This file is part of Distro Tracker. It is subject to the license terms 

# in the LICENSE file found in the top-level directory of this 

# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

# including this file, may be copied, modified, propagated, or distributed 

# except according to the terms contained in the LICENSE file. 

"""Command to import tags data from an old PTS dump.""" 

 

import sys 

 

from django.core.exceptions import ValidationError 

from django.core.management.base import BaseCommand 

from django.db import transaction 

 

from distro_tracker.core.models import ( 

EmailSettings, 

Keyword, 

Subscription, 

UserEmail 

) 

 

 

class Command(BaseCommand): 

""" 

Import the old PTS user default keywords and package-specific keywords. 

The expected input is the output of the ``bin/dump-tags.pl`` file on stdin. 

""" 

stdin = sys.stdin 

 

def write(self, message): 

if self.verbose: 

self.stdout.write(message) 

 

@transaction.atomic 

def handle(self, *args, **kwargs): 

self.verbose = int(kwargs.get('verbosity', 1)) > 1 

 

# Each line can have one of the two possible formats: 

# <email>: <tag1>,<tag2>,... 

# or 

# <email>#<package-name>: <tag1>,<tag2> 

 

for line in self.stdin: 

email, tags = line.rsplit(':', 1) 

tags = tags.strip().split(',') 

tags = [tag.strip() for tag in tags] 

# Map the keyword names that have been changed to their new values 

legacy_mapping = { 

'katie-other': 'archive', 

'buildd': 'build', 

'ddtp': 'translation', 

'cvs': 'vcs', 

} 

keywords = [ 

legacy_mapping.get(tag, tag) 

for tag in tags 

] 

keywords = Keyword.objects.filter(name__in=keywords) 

email = email.strip() 

if '#' in email: 

# A subscription specific set of keywords 

email, package = email.split('#', 1) 

try: 

subscription = Subscription.objects.get( 

package__name=package, 

email_settings__user_email__email=email) 

except Subscription.DoesNotExist: 

self.write('Skip non-existing subscription ' 

'({email} to {package}).\n'.format( 

email=email, package=package)) 

continue 

subscription.keywords.clear() 

for keyword in keywords: 

subscription.keywords.add(keyword) 

else: 

# User default keywords 

try: 

user_email, _ = UserEmail.objects.get_or_create(email=email) 

except ValidationError: 

self.write('Skip invalid email {email}.\n'.format( 

email=email)) 

continue 

email_settings, _ = \ 

EmailSettings.objects.get_or_create(user_email=user_email) 

email_settings.default_keywords.set(keywords)