diff --git a/fdroidserver/update.py b/fdroidserver/update.py index 463a1c7d..9225c241 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -1823,12 +1823,20 @@ def archive_old_apks(apps, apks, archapks, repodir, archivedir, defaultkeepversi def filter_apk_list_sorted(apk_list): res = [] + currentVersionApk = None for apk in apk_list: if apk['packageName'] == appid: + if apk['versionCode'] == common.version_code_string_to_int(app.CurrentVersionCode): + currentVersionApk = apk + continue res.append(apk) # Sort the apk list by version code. First is highest/newest. - return sorted(res, key=lambda apk: apk['versionCode'], reverse=True) + sorted_list = sorted(res, key=lambda apk: apk['versionCode'], reverse=True) + if currentVersionApk: + # Insert apk which corresponds to currentVersion at the front + sorted_list.insert(0, currentVersionApk) + return sorted_list for appid, app in apps.items(): @@ -1840,26 +1848,28 @@ def archive_old_apks(apps, apks, archapks, repodir, archivedir, defaultkeepversi logging.debug(_("Checking archiving for {appid} - apks:{integer}, keepversions:{keep}, archapks:{arch}") .format(appid=appid, integer=len(apks), keep=keepversions, arch=len(archapks))) - current_app_apks = filter_apk_list_sorted(apks) - if len(current_app_apks) > keepversions: - # Move back the ones we don't want. - for apk in current_app_apks[keepversions:]: - move_apk_between_sections(repodir, archivedir, apk) - archapks.append(apk) - apks.remove(apk) + all_app_apks = filter_apk_list_sorted(apks + archapks) - current_app_archapks = filter_apk_list_sorted(archapks) - if len(current_app_apks) < keepversions and len(current_app_archapks) > 0: - kept = 0 - # Move forward the ones we want again, except DisableAlgorithm - for apk in current_app_archapks: - if 'DisabledAlgorithm' not in apk['antiFeatures']: - move_apk_between_sections(archivedir, repodir, apk) - archapks.remove(apk) - apks.append(apk) - kept += 1 - if kept == keepversions: - break + # determine which apks to keep in repo + keep = [] + for apk in all_app_apks: + if len(keep) == keepversions: + break + if 'antiFeatures' not in apk: + keep.append(apk) + elif 'DisabledAlgorithm' not in apk['antiFeatures']: + keep.append(apk) + + # actually move apks to the target section + for apk in all_app_apks: + if apk in apks and apk not in keep: + apks.remove(apk) + archapks.append(apk) + move_apk_between_sections(repodir, archivedir, apk) + elif apk in archapks and apk in keep: + archapks.remove(apk) + apks.append(apk) + move_apk_between_sections(archivedir, repodir, apk) def move_apk_between_sections(from_dir, to_dir, apk):