aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2014-07-27 12:28:22 -0500
committerAndy Doan <andy.doan@linaro.org>2014-07-27 12:28:22 -0500
commita6831cff67ca263fc6eea7b2c295465a5b08feea (patch)
tree971a6fef5ae2af82a0fa6061c0f632a298457109
parent49772171420cc9221f889bbd957c1b23b31b4a80 (diff)
bug247: fix directory sorting logic
This ensures we sort our directory listing properly using the logic: * if all numerical then you'd list like: - latest - 1 - 10 - 100 * if non-numerical we just ensure "latest" will alway go first https://bugs.linaro.org/show_bug.cgi?id=247 Change-Id: I61c32cda08ae94dfc94e4379f8b48deb7df9911a
-rw-r--r--license_protected_downloads/tests/test_views.py14
-rw-r--r--license_protected_downloads/views.py25
l---------sampleroot/~linaro-android/staging-snowball/101
l---------sampleroot/~linaro-android/staging-snowball/latest1
4 files changed, 39 insertions, 2 deletions
diff --git a/license_protected_downloads/tests/test_views.py b/license_protected_downloads/tests/test_views.py
index 20976c8..1ca48a1 100644
--- a/license_protected_downloads/tests/test_views.py
+++ b/license_protected_downloads/tests/test_views.py
@@ -630,6 +630,20 @@ class ViewTests(BaseServeViewTest):
self.assertEqual(_sizeof_fmt(1234567899), '1.1G')
self.assertEqual(_sizeof_fmt(1234567899999), '1.1T')
+ def test_listdir(self):
+ patterns = [
+ (['b', 'a', 'latest', 'c'], ['latest', 'a', 'b', 'c']),
+ (['10', '1', '100', 'latest'], ['latest', '1', '10', '100']),
+ (['10', 'foo', '100', 'latest'], ['latest', '10', '100', 'foo']),
+ ]
+ for files, expected in patterns:
+ path = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, path)
+ for file in files:
+ with open(os.path.join(path, file), 'w') as f:
+ f.write(file)
+ self.assertEqual(expected, views._listdir(path))
+
def test_whitelisted_dirs(self):
target_file = "precise/restricted/whitelisted.txt"
url = urlparse.urljoin("http://testserver/", target_file)
diff --git a/license_protected_downloads/views.py b/license_protected_downloads/views.py
index 0813916..561b8aa 100644
--- a/license_protected_downloads/views.py
+++ b/license_protected_downloads/views.py
@@ -73,9 +73,30 @@ def _sizeof_fmt(num):
return "%3.1f%s" % (num, 'T')
-def dir_list(url, path, human_readable=True):
+def _listdir(path):
+ '''Lists the contents of a directory sorted to our requirements.
+
+ If the directory is all numbers it sorts them numerically. The "latest"
+ entry will always be the first entry. Else use standard sorting.
+ '''
+ def _sort(a, b):
+ try:
+ return cmp(int(a), int(b))
+ except:
+ pass
+ if a == 'latest':
+ return -1
+ elif b == 'latest':
+ return 1
+
+ return cmp(a, b)
files = os.listdir(path)
- files.sort()
+ files.sort(_sort)
+ return files
+
+
+def dir_list(url, path, human_readable=True):
+ files = _listdir(path)
listing = []
for file_name in files:
diff --git a/sampleroot/~linaro-android/staging-snowball/10 b/sampleroot/~linaro-android/staging-snowball/10
new file mode 120000
index 0000000..7b27b25
--- /dev/null
+++ b/sampleroot/~linaro-android/staging-snowball/10
@@ -0,0 +1 @@
+173 \ No newline at end of file
diff --git a/sampleroot/~linaro-android/staging-snowball/latest b/sampleroot/~linaro-android/staging-snowball/latest
new file mode 120000
index 0000000..7b27b25
--- /dev/null
+++ b/sampleroot/~linaro-android/staging-snowball/latest
@@ -0,0 +1 @@
+173 \ No newline at end of file