update: add --rename-apks to force APK filenames to fdroid standard

uses the standard package.name_123.apk.  If that exists, it appends the
shasum.  If that exists, then its a duplicate, so its deleted. This should
help @SergeWinters with his 12,000 APKs.
This commit is contained in:
Hans-Christoph Steiner 2017-05-31 21:20:35 +02:00
parent 0f4cbc7224
commit 4053f03d77
4 changed files with 73 additions and 8 deletions

View file

@ -1089,8 +1089,14 @@ def scan_apk(apkcache, apkfilename, repodir, knownapks, use_date_from_apk):
"""
if ' ' in apkfilename:
logging.critical("Spaces in filenames are not allowed.")
return True, None, False
if options.rename_apks:
newfilename = apkfilename.replace(' ', '_')
os.rename(os.path.join(repodir, apkfilename),
os.path.join(repodir, newfilename))
apkfilename = newfilename
else:
logging.critical("Spaces in filenames are not allowed.")
return True, None, False
apkfile = os.path.join(repodir, apkfilename)
shasum = sha256sum(apkfile)
@ -1108,13 +1114,8 @@ def scan_apk(apkcache, apkfilename, repodir, knownapks, use_date_from_apk):
if not usecache:
logging.debug("Processing " + apkfilename)
apk = {}
apk['apkName'] = apkfilename
apk['hash'] = shasum
apk['hashType'] = 'sha256'
srcfilename = apkfilename[:-4] + "_src.tar.gz"
if os.path.exists(os.path.join(repodir, srcfilename)):
apk['srcname'] = srcfilename
apk['size'] = os.path.getsize(apkfile)
apk['uses-permission'] = []
apk['uses-permission-sdk-23'] = []
apk['features'] = []
@ -1147,6 +1148,35 @@ def scan_apk(apkcache, apkfilename, repodir, knownapks, use_date_from_apk):
logging.critical("Failed to get apk signature")
return True, None, False
if options.rename_apks:
n = apk['packageName'] + '_' + str(apk['versionCode']) + '.apk'
std_short_name = os.path.join(repodir, n)
if apkfile != std_short_name:
if os.path.exists(std_short_name):
std_long_name = std_short_name.replace('.apk', '_' + apk['sig'][:7] + '.apk')
if apkfile != std_long_name:
if os.path.exists(std_long_name):
dupdir = os.path.join('duplicates', repodir)
if not os.path.isdir(dupdir):
os.makedirs(dupdir, exist_ok=True)
dupfile = os.path.join('duplicates', std_long_name)
logging.warning('Moving duplicate ' + std_long_name + ' to ' + dupfile)
os.rename(apkfile, dupfile)
return True, None, False
else:
os.rename(apkfile, std_long_name)
apkfile = std_long_name
else:
os.rename(apkfile, std_short_name)
apkfile = std_short_name
apkfilename = apkfile[len(repodir) + 1:]
apk['apkName'] = apkfilename
srcfilename = apkfilename[:-4] + "_src.tar.gz"
if os.path.exists(os.path.join(repodir, srcfilename)):
apk['srcname'] = srcfilename
apk['size'] = os.path.getsize(apkfile)
apkzip = zipfile.ZipFile(apkfile, 'r')
# if an APK has files newer than the system time, suggest updating
@ -1498,6 +1528,8 @@ def main():
help="When configured for signed indexes, create only unsigned indexes at this stage")
parser.add_argument("--use-date-from-apk", action="store_true", default=False,
help="Use date from apk instead of current time for newly added apks")
parser.add_argument("--rename-apks", action="store_true", default=False,
help="Rename APK files that do not match package.name_123.apk")
metadata.add_metadata_arguments(parser)
options = parser.parse_args()
metadata.warnings_action = options.W
@ -1517,6 +1549,9 @@ def main():
resize_all_icons(repodirs)
sys.exit(0)
if options.rename_apks:
options.clean = True
# check that icons exist now, rather than fail at the end of `fdroid update`
for k in ['repo_icon', 'archive_icon']:
if k in config: