diff options
author | Danilo Segan <danilo@canonical.com> | 2012-07-11 13:56:10 +0200 |
---|---|---|
committer | Danilo Segan <danilo@canonical.com> | 2012-07-11 13:56:10 +0200 |
commit | 16b167b6418a8b086bc89f210aef7c73c7c16b39 (patch) | |
tree | da86e6d98a1555aee71ba0420931ea7737bc5c43 /license_protected_downloads | |
parent | d9f4eff4a60e24a2071bf72f4f9b523a438578d5 (diff) |
Lint fixes.
Diffstat (limited to 'license_protected_downloads')
-rw-r--r-- | license_protected_downloads/buildinfo.py | 34 | ||||
-rw-r--r-- | license_protected_downloads/models.py | 1 | ||||
-rw-r--r-- | license_protected_downloads/tests/__init__.py | 12 | ||||
-rw-r--r-- | license_protected_downloads/tests/test_buildinfo.py | 51 | ||||
-rw-r--r-- | license_protected_downloads/tests/test_pep8.py | 40 | ||||
-rw-r--r-- | license_protected_downloads/tests/test_pyflakes.py | 32 | ||||
-rw-r--r-- | license_protected_downloads/tests/test_views.py | 14 | ||||
-rw-r--r-- | license_protected_downloads/views.py | 21 |
8 files changed, 159 insertions, 46 deletions
diff --git a/license_protected_downloads/buildinfo.py b/license_protected_downloads/buildinfo.py index e961949..128c109 100644 --- a/license_protected_downloads/buildinfo.py +++ b/license_protected_downloads/buildinfo.py @@ -1,19 +1,21 @@ import os -import glob +import fnmatch class IncorrectDataFormatException(Exception): ''' Build-info data is in incorrect format. ''' + class BuildInfo: def __init__(self, fn): self.index = 0 self.lines = [] self.build_info_array = [{}] self.file_info_array = [{}] - self.fields_defined = ["format-version", "files-pattern", - "build-name", "theme", "license-type", "openid-launchpad-teams", - "collect-user-data", "license-text"] + self.fields_defined = [ + "format-version", "files-pattern", "build-name", "theme", + "license-type", "openid-launchpad-teams", "collect-user-data", + "license-text"] self.full_file_name = fn self.search_path = self.get_search_path(fn) self.fname = os.path.basename(fn) @@ -24,7 +26,6 @@ class BuildInfo: self.remove_false_positives() self.max_index = len(self.file_info_array) - @classmethod def get_search_path(cls, path): "Return BUILD-INFO.txt search path for a given filesystem path." @@ -32,12 +33,11 @@ class BuildInfo: path = os.path.dirname(path) return path - @classmethod def build_info_exists(cls, path): "Check if BUILD-INFO.txt exists for a given filesystem path." - return os.path.exists(os.path.join(cls.get_search_path(path), "BUILD-INFO.txt")) - + return os.path.exists( + os.path.join(cls.get_search_path(path), "BUILD-INFO.txt")) def _set(self, key, value): key = key.lower() @@ -63,10 +63,9 @@ class BuildInfo: # Special handling of entire-directory access specifier if key == "*" and os.path.isdir(self.full_file_name): return block[key] - files = glob.glob(os.path.join(self.search_path, key)) - for filename in files: - if filename == self.full_file_name: - return block[key] + if fnmatch.fnmatch(self.full_file_name, + os.path.join(self.search_path, key)): + return block[key] return [{}] def getFormatVersion(self): @@ -86,16 +85,18 @@ class BuildInfo: if field == key: return block[field] return False - + def parseLine(self, line): values = line.split(":", 1) if len(values) != 2: - raise IncorrectDataFormatException("'%s': Line is not in the correct format." % line) + raise IncorrectDataFormatException( + "'%s': Line is not in the correct format." % line) else: field = values[0].strip().lower() value = values[1].strip() if not self.isValidField(field): - raise IncorrectDataFormatException("Field '%s' not allowed." % field) + raise IncorrectDataFormatException( + "Field '%s' not allowed." % field) else: return {field: value} @@ -111,7 +112,8 @@ class BuildInfo: format_line = lines.pop(0) values = self.parseLine(format_line) if not "format-version" in values: - raise IncorrectDataFormatException("Format-Version field not found.") + raise IncorrectDataFormatException( + "Format-Version field not found.") self._set("format-version", values["format-version"]) self.line_no = 0 diff --git a/license_protected_downloads/models.py b/license_protected_downloads/models.py index bfb4961..d95a964 100644 --- a/license_protected_downloads/models.py +++ b/license_protected_downloads/models.py @@ -1,5 +1,6 @@ from django.db import models + class License(models.Model): digest = models.CharField(max_length=40) text = models.TextField() diff --git a/license_protected_downloads/tests/__init__.py b/license_protected_downloads/tests/__init__.py index 04bf1eb..3c2e68d 100644 --- a/license_protected_downloads/tests/__init__.py +++ b/license_protected_downloads/tests/__init__.py @@ -1,10 +1,14 @@ -from license_protected_downloads.tests.test_models import * -from license_protected_downloads.tests.test_views import * -from license_protected_downloads.tests.test_buildinfo import * +from license_protected_downloads.tests.test_buildinfo import BuildInfoTests +from license_protected_downloads.tests.test_models import LicenseTestCase +from license_protected_downloads.tests.test_pep8 import TestPep8 +from license_protected_downloads.tests.test_pyflakes import TestPyflakes +from license_protected_downloads.tests.test_views import ViewTests #starts the test suite -__test__= { +__test__ = { 'LicenseTestCase': LicenseTestCase, 'ViewTests': ViewTests, 'BuildInfoTests': BuildInfoTests, + 'TestPep8': TestPep8, + 'TestPyflakes': TestPyflakes, } diff --git a/license_protected_downloads/tests/test_buildinfo.py b/license_protected_downloads/tests/test_buildinfo.py index f315529..80228e7 100644 --- a/license_protected_downloads/tests/test_buildinfo.py +++ b/license_protected_downloads/tests/test_buildinfo.py @@ -12,14 +12,14 @@ class BuildInfoTests(unittest.TestCase): def setUp(self): self.buildinfo_file_path = os.path.join(THIS_DIRECTORY, "BUILD-INFO.txt") - + def test_readFile_nonFile(self): with self.assertRaises(IOError): - build_info = BuildInfo("license_protected_downloads") + BuildInfo("license_protected_downloads") def test_readFile_nonexistentFile(self): with self.assertRaises(IOError): - build_info = BuildInfo("nonexistent.file") + BuildInfo("nonexistent.file") def test_readFile_File(self): build_info = BuildInfo(self.buildinfo_file_path) @@ -58,13 +58,15 @@ class BuildInfoTests(unittest.TestCase): line = "Build-Name:value" build_info = BuildInfo(self.buildinfo_file_path) - self.assertDictEqual({"build-name":"value"}, build_info.parseLine(line)) + self.assertDictEqual({"build-name": "value"}, + build_info.parseLine(line)) def test_parseLine_trims(self): line = "Build-Name: value" build_info = BuildInfo(self.buildinfo_file_path) - self.assertDictEqual({"build-name":"value"}, build_info.parseLine(line)) + self.assertDictEqual({"build-name": "value"}, + build_info.parseLine(line)) def test_parseLine_invalid_field(self): line = "field: value" @@ -82,8 +84,11 @@ class BuildInfoTests(unittest.TestCase): def test_parseData_blocks(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "Build-Name: weehee", - "Files-Pattern: *.tgz", "Build-Name: woohoo"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "Build-Name: weehee", + "Files-Pattern: *.tgz", + "Build-Name: woohoo"] build_info.parseData(data) self.assertEquals(build_info.build_info_array, @@ -94,7 +99,8 @@ class BuildInfoTests(unittest.TestCase): def test_parseData_block_multiple_patterns(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt,*.tgz", + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt,*.tgz", "Build-Name: weehee"] build_info.parseData(data) @@ -113,7 +119,8 @@ class BuildInfoTests(unittest.TestCase): build_info = BuildInfo(self.buildinfo_file_path) build_info.line_no = 0 - self.assertEquals("", build_info.parseContinuation(["no-space", " space"])) + self.assertEquals("", + build_info.parseContinuation(["no-space", " space"])) def test_parseContinuation(self): build_info = BuildInfo(self.buildinfo_file_path) @@ -135,7 +142,9 @@ class BuildInfoTests(unittest.TestCase): def test_parseData_extra_fields(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "Build-Name: woohoo"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "Build-Name: woohoo"] build_info.parseData(data) self.assertEqual(build_info.build_info_array, @@ -182,8 +191,11 @@ class BuildInfoTests(unittest.TestCase): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] build_info.file_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected", - "Files-Pattern: *.txt", "License-Type: open"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "License-Type: protected", + "Files-Pattern: *.txt", + "License-Type: open"] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() build_info.remove_false_positives() @@ -195,8 +207,11 @@ class BuildInfoTests(unittest.TestCase): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] build_info.file_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected", - "Files-Pattern: *.txt", "License-Type: protected"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "License-Type: protected", + "Files-Pattern: *.txt", + "License-Type: protected"] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() build_info.remove_false_positives() @@ -219,7 +234,9 @@ class BuildInfoTests(unittest.TestCase): build_info.full_file_name = file_path build_info.build_info_array = [{}] build_info.file_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "License-Type: protected"] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() @@ -246,7 +263,9 @@ class BuildInfoTests(unittest.TestCase): build_info.full_file_name = file_path build_info.build_info_array = [{}] build_info.file_info_array = [{}] - data = ["Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected"] + data = ["Format-Version: 2.0", + "Files-Pattern: *.txt", + "License-Type: protected"] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() build_info.remove_false_positives() diff --git a/license_protected_downloads/tests/test_pep8.py b/license_protected_downloads/tests/test_pep8.py new file mode 100644 index 0000000..3877d85 --- /dev/null +++ b/license_protected_downloads/tests/test_pep8.py @@ -0,0 +1,40 @@ +# Copyright (C) 2012 Linaro Ltd. +# +# Author: Loic Minier <loic.minier@linaro.org> +# +# This file is part of Linaro Image Tools. +# +# Linaro Image Tools is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Linaro Image Tools is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import subprocess +from testtools import TestCase + + +class TestPep8(TestCase): + def test_pep8(self): + # Errors we have to ignore for now: + # * E202 whitespace before ')' or ']' + # E202 is actually only reported with the natty version of pep8 and + # can be re-enabled once we drop support for natty. + ignore = ['E202'] + # Ignore return code. + proc = subprocess.Popen(['pep8', + '--repeat', + '--ignore=%s' % ','.join(ignore), + '.'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + (stdout, stderr) = proc.communicate() + self.assertEquals('', stdout) + self.assertEquals('', stderr) diff --git a/license_protected_downloads/tests/test_pyflakes.py b/license_protected_downloads/tests/test_pyflakes.py new file mode 100644 index 0000000..b7eadbe --- /dev/null +++ b/license_protected_downloads/tests/test_pyflakes.py @@ -0,0 +1,32 @@ +# Copyright (C) 2011 Linaro +# +# Author: Loic Minier <loic.minier@linaro.org> +# +# This file is part of Linaro Image Tools. +# +# Linaro Image Tools is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Linaro Image Tools is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import subprocess +from testtools import TestCase + + +class TestPyflakes(TestCase): + def test_pyflakes(self): + # ignore return code + proc = subprocess.Popen(['pyflakes', '.'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + (stdout, stderr) = proc.communicate() + self.assertEquals('', stdout) + self.assertEquals('', stderr) diff --git a/license_protected_downloads/tests/test_views.py b/license_protected_downloads/tests/test_views.py index 68d3f28..d945a1d 100644 --- a/license_protected_downloads/tests/test_views.py +++ b/license_protected_downloads/tests/test_views.py @@ -1,19 +1,19 @@ -import re -import urlparse -from license_protected_downloads.views import _insert_license_into_db - __author__ = 'dooferlad' +from django.conf import settings +from django.test import Client, TestCase +import hashlib import os import unittest -import hashlib -from django.test import Client, TestCase +import urlparse + from license_protected_downloads.buildinfo import BuildInfo -from django.conf import settings +from license_protected_downloads.views import _insert_license_into_db THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root") + class ViewTests(TestCase): def setUp(self): self.client = Client() diff --git a/license_protected_downloads/views.py b/license_protected_downloads/views.py index 37c1ef4..9499c7b 100644 --- a/license_protected_downloads/views.py +++ b/license_protected_downloads/views.py @@ -19,6 +19,7 @@ from django.template import RequestContext import mimetypes import glob + def _hidden_file(file_name): hidden_files = ["BUILD-INFO.txt", "EULA.txt", ".htaccess", "HEADER.html"] for pattern in hidden_files: @@ -26,6 +27,7 @@ def _hidden_file(file_name): return True return False + def _hidden_dir(file_name): hidden_files = [".*openid.*", ".*restricted.*", ".*private.*"] for pattern in hidden_files: @@ -33,6 +35,7 @@ def _hidden_dir(file_name): return True return False + def dir_list(url, path): files = os.listdir(path) files.sort() @@ -57,7 +60,7 @@ def dir_list(url, path): size = os.path.getsize(file) if not re.search(r'^/', url) and url != '': - url = '/' + url + url = '/' + url listing.append({'name': name, 'size': size, 'type': type, @@ -65,6 +68,7 @@ def dir_list(url, path): 'url': url + '/' + name}) return listing + def test_path(path): for basepath in settings.SERVED_PATHS: @@ -76,20 +80,24 @@ def test_path(path): return None + def _insert_license_into_db(digest, text, theme): if not License.objects.filter(digest=digest): l = License(digest=digest, text=text, theme=theme) l.save() + def _check_special_eula(path): if glob.glob(path + ".EULA.txt.*"): return True + def _get_theme(path): eula = glob.glob(path + ".EULA.txt.*") vendor = os.path.splitext(eula[0])[1] return vendor[1:] + def is_protected(path): build_info = None max_index = 1 @@ -151,9 +159,11 @@ def is_protected(path): return digests + def license_accepted(request, digest): return 'license_accepted_' + digest in request.COOKIES + def accept_license(request): if "accept" in request.POST: lic = License.objects.filter(digest=request.GET['lic']).get() @@ -163,14 +173,16 @@ def accept_license(request): "?dl=/" + file_url) d = lic.digest cookie_name = "license_accepted_" + d.encode("ascii") + # Set a cookie with 1 day of expiry. response.set_cookie(cookie_name, - max_age=60*60*24, # 1 day expiry + max_age=60 * 60 * 24, path="/" + os.path.dirname(file_url)) else: response = render_to_response('licenses/nolicense.html') return response + def show_license(request): if 'lic' not in request.GET or 'url' not in request.GET: raise Http404 @@ -182,9 +194,11 @@ def show_license(request): 'url': request.GET['url']}, context_instance=RequestContext(request)) + def redirect_to_root(request): return redirect('/') + def file_server(request, path): url = path result = test_path(path) @@ -226,7 +240,8 @@ def file_server(request, path): else: for digest in digests: if not license_accepted(request, digest): - response = redirect('/license?lic=' + digest + "&url=" + url) + response = redirect( + '/license?lic=' + digest + "&url=" + url) if not response: mimetypes.init() |