Skip to content

Commit

Permalink
Check classifiers against list of valid classifiers
Browse files Browse the repository at this point in the history
With caching

Closes gh-6
  • Loading branch information
takluyver committed Apr 1, 2015
1 parent 32a6fa3 commit c336b29
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- "3.4"
install: pip install testpath
install: pip install testpath requests
script: py.test
sudo: false
63 changes: 63 additions & 0 deletions flit/inifile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import configparser
import logging
import os
from pathlib import Path
import sys

import requests

from . import common

log = logging.getLogger(__name__)

class ConfigError(ValueError):
pass

Expand Down Expand Up @@ -29,6 +38,56 @@ class ConfigError(ValueError):
'home-page',
}

def get_cache_dir():
if os.name == 'posix' and sys.platform != 'darwin':
# Linux, Unix, AIX, etc.
# use ~/.cache if empty OR not set
xdg = os.environ.get("XDG_CACHE_HOME", None) or (os.path.expanduser('~/.cache'))
return Path(xdg, 'flit')

elif sys.platform == 'darwin':
return Path(os.path.expanduser('~'), 'Library/Caches/flit')

else:
# Windows (hopefully)
local = os.environ.get('LOCALAPPDATA', None) or (os.path.expanduser('~\\AppData\\Local'))
return Path(local, 'flit')

def _verify_classifiers_cached(classifiers):
with (get_cache_dir() / 'classifiers.lst').open() as f:
valid_classifiers = set(l.strip() for l in f)

invalid = classifiers - valid_classifiers
if invalid:
raise ConfigError("Invalid classifiers:\n" +
"\n".join(invalid))

def _download_classifiers():
log.info('Fetching list of valid trove classifiers')
resp = requests.get('https://pypi.python.org/pypi?%3Aaction=list_classifiers')
resp.raise_for_status()

cache_dir = get_cache_dir()
try:
cache_dir.mkdir(parents=True)
except FileExistsError:
pass
with (get_cache_dir() / 'classifiers.lst').open('wb') as f:
f.write(resp.content)

def verify_classifiers(classifiers):
classifiers = set(classifiers)
try:
_verify_classifiers_cached(classifiers)
except (FileNotFoundError, ConfigError):
# FileNotFoundError: We haven't yet got the classifiers cached
# ConfigError: At least one is invalid, but it may have been added since
# last time we fetched them.
_download_classifiers()

_verify_classifiers_cached(classifiers)


def read_pkg_ini(path):
"""Read and check the -pkg.ini file with data about the package.
"""
Expand Down Expand Up @@ -78,6 +137,10 @@ def read_pkg_ini(path):
if 'dist_name' in md_dict:
md_dict['name'] = md_dict.pop('dist_name')

if 'classifiers' in md_dict:
verify_classifiers(md_dict['classifiers'])

# Scripts ---------------
if cp.has_section('scripts'):
scripts_dict = {k: common.parse_entry_point(v) for k, v in cp['scripts'].items()}
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/samples/invalid_classifier.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[metadata]
module=module1
author=Sir Robin
author-email=robin@camelot.uk
home-page=http://github.com/sirrobin/module1
classifiers=License :: OSI Approved :: BSD License
Intended Audience :: Pacman
11 changes: 11 additions & 0 deletions tests/test_inifile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pathlib

import pytest

from flit.inifile import read_pkg_ini, ConfigError

samples_dir = pathlib.Path(__file__).parent / 'samples'

def test_invalid_classifier():
with pytest.raises(ConfigError):
read_pkg_ini(samples_dir / 'invalid_classifier.ini')

0 comments on commit c336b29

Please sign in to comment.