From ba33c7f6cd21ad12b0cb8ee95c3b29a2f71d692f Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Sun, 26 Jan 2020 18:39:37 +0100 Subject: [PATCH] daca2: Improve package sorting using natsort (#2505) * daca2: Improve package sorting using natsort This switches the external dependency from semver to natsort, and improves comparison of packages where one or more of the packages do not use semantic versioning (major.minor.patch). This also makes daca2-download and daca2-getpackages work with python 3. In theory, they should work with python 2 as well, but I have not tested it. * Make daca2 scripts executable * Update hashbangs to python3 * Update usage description To avoid specifying python version in the usage description, just show how to execute the scripts and leave the rest to the shebangs. * No need to specify python version in start_donate_cpu_server_test_local.sh Leave it to the hashbang instead. --- .travis.yml | 3 +-- tools/daca2-download.py | 13 +++++-------- tools/daca2-getpackages.py | 17 +++++------------ .../test/start_donate_cpu_server_test_local.sh | 2 +- 4 files changed, 12 insertions(+), 23 deletions(-) mode change 100644 => 100755 tools/daca2-download.py mode change 100644 => 100755 tools/daca2-getpackages.py diff --git a/.travis.yml b/.travis.yml index 4443c8763..7933b8b2e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,6 @@ before_install: - travis_retry python2 -m pip install --user unittest2 - travis_retry python2 -m pip install --user pexpect # imported by tools/ci.py - travis_retry python2 -m pip install --user pygments - - travis_retry python2 -m pip install --user semver # Python 3 modules - travis_retry python3 -m pip install --user setuptools --upgrade - travis_retry python3 -m pip install --user pytest @@ -37,7 +36,7 @@ before_install: - travis_retry python3 -m pip install --user requests # imported by tools/pr.py - travis_retry python3 -m pip install --user pygments - travis_retry sudo python3 -m pip install demjson # installs jsonlint => sudo required - - travis_retry python3 -m pip install --user semver + - travis_retry python3 -m pip install --user natsort matrix: # do notify immediately about it when a job of a build fails. diff --git a/tools/daca2-download.py b/tools/daca2-download.py old mode 100644 new mode 100755 index 336e1555e..8a05332ab --- a/tools/daca2-download.py +++ b/tools/daca2-download.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Downloads all daca2 source code packages. # # Usage: -# $ mkdir ~/daca2-packages && python daca2-download.py +# $ ./daca2-download.py import subprocess @@ -12,7 +12,7 @@ import shutil import glob import os import time -import semver +import natsort DEBIAN = ('ftp://ftp.se.debian.org/debian/', 'ftp://ftp.debian.org/debian/') @@ -33,11 +33,8 @@ def wget(filepath): def latestvername(names): - if len(names) == 1: - return names[0] - names.sort(cmp=semver.cmp, key=lambda x: x[x.index( - '_')+1:x.index('.orig.tar')-x.index('_')+1]) - return names[-1] + s = natsort.natsorted(names, key=lambda x: x[x.index('_')+1:x.index('.orig.tar')]) + return s[-1] def getpackages(): diff --git a/tools/daca2-getpackages.py b/tools/daca2-getpackages.py old mode 100644 new mode 100755 index 9e0288ac1..6cfc5b197 --- a/tools/daca2-getpackages.py +++ b/tools/daca2-getpackages.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Get list of debian packages # # Usage: # # cd cppcheck/tools -# python daca2-getpackages.py +# ./daca2-getpackages.py # @@ -19,7 +19,7 @@ import os import re import datetime import time -import semver +import natsort DEBIAN = ('ftp://ftp.de.debian.org/debian/', 'ftp://ftp.debian.org/debian/') @@ -39,15 +39,8 @@ def wget(filepath): def latestvername(names): - if len(names) == 1: - return names[0] - try: - names.sort(cmp=semver.compare, key=lambda x: x[x.index('_')+1:x.index('.orig.tar')]) - except ValueError: - # semver.compare() throws an exception if the version is not formatted - # like it is required by semver. For example 0.8 is not valid. - pass - return names[-1] + s = natsort.natsorted(names, key=lambda x: x[x.index('_')+1:x.index('.orig.tar')]) + return s[-1] def getpackages(): diff --git a/tools/test/start_donate_cpu_server_test_local.sh b/tools/test/start_donate_cpu_server_test_local.sh index b53124707..4bd19555f 100644 --- a/tools/test/start_donate_cpu_server_test_local.sh +++ b/tools/test/start_donate_cpu_server_test_local.sh @@ -22,7 +22,7 @@ fi if [ ! -f "${dacaathome_path}/packages.txt" ] then - python "${cppcheck_tools_path}/daca2-getpackages.py" > "${dacaathome_path}/packages.txt" + "${cppcheck_tools_path}/daca2-getpackages.py" > "${dacaathome_path}/packages.txt" fi while :