aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRune Halvorsen <runeh@vorkosigan.(none)>2009-05-06 00:19:00 +0200
committerRune Halvorsen <runeh@vorkosigan.(none)>2009-05-06 00:19:00 +0200
commit9e477158ac9e23d1b905b07cbe97f7da8cc53999 (patch)
tree4a6deb69989f859374dd293d27cb146b0939c38d
parent1804d0cfb72d766f16aaa1e11f48084f179fc16c (diff)
downloadchishop-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.py53
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"""
2Management command for loading all the known classifiers from the official
3pypi, or from a file/url.
4
5Note, pypi docs says to not add classifiers that are not used in submitted
6projects. On the other hand it can be usefull to have a list of classifiers
7to choose if you have to modify package data. Use it if you need it.
8"""
9
10from __future__ import with_statement
11import urllib
12import os.path
13
14from django.core.management.base import BaseCommand
15from djangopypi.models import Classifier
16
17CLASSIFIERS_URL = "http://pypi.python.org/pypi?%3Aaction=list_classifiers"
18
19class Command(BaseCommand):
20 help = """Load all classifiers from pypi. If any arguments are given,
21they will be used as paths or urls for classifiers instead of using the
22official 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 []