diff options
author | Rune Halvorsen <runeh@vorkosigan.(none)> | 2009-05-06 00:19:00 +0200 |
---|---|---|
committer | Rune Halvorsen <runeh@vorkosigan.(none)> | 2009-05-06 00:19:00 +0200 |
commit | 9e477158ac9e23d1b905b07cbe97f7da8cc53999 (patch) | |
tree | 4a6deb69989f859374dd293d27cb146b0939c38d | |
parent | 1804d0cfb72d766f16aaa1e11f48084f179fc16c (diff) | |
download | chishop-9e477158ac9e23d1b905b07cbe97f7da8cc53999.tar.bz2 chishop-9e477158ac9e23d1b905b07cbe97f7da8cc53999.tar.xz chishop-9e477158ac9e23d1b905b07cbe97f7da8cc53999.zip |
Added management command for loading classifiers from pypi
-rw-r--r-- | djangopypi/management/commands/loadclassifiers.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/djangopypi/management/commands/loadclassifiers.py b/djangopypi/management/commands/loadclassifiers.py new file mode 100644 index 0000000..49e2642 --- /dev/null +++ b/djangopypi/management/commands/loadclassifiers.py | |||
@@ -0,0 +1,53 @@ | |||
1 | """ | ||
2 | Management command for loading all the known classifiers from the official | ||
3 | pypi, or from a file/url. | ||
4 | |||
5 | Note, pypi docs says to not add classifiers that are not used in submitted | ||
6 | projects. On the other hand it can be usefull to have a list of classifiers | ||
7 | to choose if you have to modify package data. Use it if you need it. | ||
8 | """ | ||
9 | |||
10 | from __future__ import with_statement | ||
11 | import urllib | ||
12 | import os.path | ||
13 | |||
14 | from django.core.management.base import BaseCommand | ||
15 | from djangopypi.models import Classifier | ||
16 | |||
17 | CLASSIFIERS_URL = "http://pypi.python.org/pypi?%3Aaction=list_classifiers" | ||
18 | |||
19 | class Command(BaseCommand): | ||
20 | help = """Load all classifiers from pypi. If any arguments are given, | ||
21 | they will be used as paths or urls for classifiers instead of using the | ||
22 | official pypi list url""" | ||
23 | |||
24 | def handle(self, *args, **options): | ||
25 | args = args or [CLASSIFIERS_URL] | ||
26 | |||
27 | cnt = 0 | ||
28 | for location in args: | ||
29 | print "Loading %s" % location | ||
30 | lines = self._get_lines(location) | ||
31 | for name in lines: | ||
32 | c, created = Classifier.objects.get_or_create(name=name) | ||
33 | if created: | ||
34 | c.save() | ||
35 | cnt += 1 | ||
36 | |||
37 | print "Added %s new classifiers from %s source(s)" % (cnt, len(args)) | ||
38 | |||
39 | def _get_lines(self, location): | ||
40 | """Return a list of lines for a lication that can be a file or | ||
41 | a url. If path/url doesn't exist, returns an empty list""" | ||
42 | try: # This is dirty, but OK I think. both net and file ops raise IOE | ||
43 | if location.startswith(("http://", "https://")): | ||
44 | fp = urllib.urlopen(location) | ||
45 | return [e.strip() for e in fp.read().split('\n') | ||
46 | if e and not e.isspace()] | ||
47 | else: | ||
48 | fp = open(location) | ||
49 | return [e.strip() for e in fp.readlines() | ||
50 | if e and not e.isspace()] | ||
51 | except IOError: | ||
52 | print "Couldn't load %s" % location | ||
53 | return [] | ||