2016-01-04 16:33:20 +01:00
|
|
|
#!/usr/bin/env python3
|
2012-01-11 00:24:28 +01:00
|
|
|
#
|
|
|
|
# rewritemeta.py - part of the FDroid server tools
|
2020-07-02 00:48:03 +02:00
|
|
|
# This cleans up the original .yml metadata file format.
|
2012-01-11 00:24:28 +01:00
|
|
|
# Copyright (C) 2010-12, Ciaran Gultnieks, ciaran@ciarang.com
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-09-04 11:37:05 +02:00
|
|
|
from argparse import ArgumentParser
|
2014-01-27 16:56:55 +01:00
|
|
|
import logging
|
2016-01-04 17:00:21 +01:00
|
|
|
import io
|
2020-08-19 23:13:23 +02:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2021-06-08 15:31:55 +02:00
|
|
|
from pathlib import Path
|
2015-09-25 07:40:49 +02:00
|
|
|
|
2017-09-13 18:03:57 +02:00
|
|
|
from . import _
|
2016-01-04 17:37:35 +01:00
|
|
|
from . import common
|
|
|
|
from . import metadata
|
2012-01-11 00:24:28 +01:00
|
|
|
|
2013-11-01 12:10:57 +01:00
|
|
|
config = None
|
|
|
|
|
2014-05-02 05:39:33 +02:00
|
|
|
|
2015-12-07 20:12:26 +01:00
|
|
|
def proper_format(app):
|
2016-01-04 17:00:21 +01:00
|
|
|
s = io.StringIO()
|
2015-12-07 20:12:26 +01:00
|
|
|
# TODO: currently reading entire file again, should reuse first
|
|
|
|
# read in metadata.py
|
metadata: always open metadata files as UTF-8
Windows seems to require this, otherwise this happens:
Traceback (most recent call last):
File "tests/update.TestCase", line 737, in test_translate_per_build_anti_features
apps = fdroidserver.metadata.read_metadata(xref=True)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 813, in read_metadata
app = parse_metadata(metadatapath, appid in check_vcs, refresh)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1023, in parse_metadata
parse_yaml_metadata(mf, app)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1073, in parse_yaml_metadata
yamldata = yaml.safe_load(mf)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 162, in safe_load
return load(stream, SafeLoader)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 112, in load
loader = Loader(stream)
File "C:\python37\lib\site-packages\yaml\loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "C:\python37\lib\site-packages\yaml\reader.py", line 85, in __init__
self.determine_encoding()
File "C:\python37\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
self.update_raw()
File "C:\python37\lib\site-packages\yaml\reader.py", line 178, in update_raw
data = self.stream.read(size)
File "C:\python37\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 37: character maps to <undefined>
2019-09-25 13:34:11 +02:00
|
|
|
cur_content = Path(app.metadatapath).read_text(encoding='utf-8')
|
2021-06-08 15:31:55 +02:00
|
|
|
if Path(app.metadatapath).suffix == '.yml':
|
2018-02-23 22:42:46 +01:00
|
|
|
metadata.write_yaml(s, app)
|
2015-12-07 20:12:26 +01:00
|
|
|
content = s.getvalue()
|
|
|
|
s.close()
|
|
|
|
return content == cur_content
|
|
|
|
|
|
|
|
|
2023-05-04 14:51:04 +02:00
|
|
|
def remove_blank_flags_from_builds(builds):
|
|
|
|
"""Remove unset entries from Builds so they are not written out."""
|
|
|
|
if not builds:
|
|
|
|
return list()
|
|
|
|
newbuilds = list()
|
|
|
|
for build in builds:
|
|
|
|
new = dict()
|
|
|
|
for k in metadata.build_flags:
|
2023-04-21 10:00:40 +02:00
|
|
|
v = build.get(k)
|
|
|
|
# 0 is valid value, it should not be stripped
|
|
|
|
if v is None or v is False or v == '' or v == dict() or v == list():
|
2023-05-04 14:51:04 +02:00
|
|
|
continue
|
|
|
|
new[k] = v
|
|
|
|
newbuilds.append(new)
|
|
|
|
return newbuilds
|
|
|
|
|
|
|
|
|
2012-02-26 15:18:58 +01:00
|
|
|
def main():
|
2024-05-08 16:26:46 +02:00
|
|
|
global config
|
2012-01-11 00:24:28 +01:00
|
|
|
|
2020-10-01 12:17:02 +02:00
|
|
|
parser = ArgumentParser()
|
2015-09-12 08:42:50 +02:00
|
|
|
common.setup_global_opts(parser)
|
2023-04-24 12:47:32 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--list",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help=_("List files that would be reformatted (dry run)"),
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"appid", nargs='*', help=_("application ID of file to operate on")
|
|
|
|
)
|
2016-09-12 12:55:48 +02:00
|
|
|
metadata.add_metadata_arguments(parser)
|
2024-05-08 16:26:46 +02:00
|
|
|
options = common.parse_args(parser)
|
2016-09-12 12:55:48 +02:00
|
|
|
metadata.warnings_action = options.W
|
2012-01-11 00:24:28 +01:00
|
|
|
|
2024-05-08 16:26:46 +02:00
|
|
|
config = common.read_config()
|
2013-11-01 12:10:57 +01:00
|
|
|
|
2012-02-26 15:18:58 +01:00
|
|
|
# Get all apps...
|
2020-11-17 16:43:26 +01:00
|
|
|
allapps = metadata.read_metadata(options.appid)
|
2015-09-04 11:37:05 +02:00
|
|
|
apps = common.read_app_args(options.appid, allapps, False)
|
2012-01-11 00:24:28 +01:00
|
|
|
|
2016-01-04 17:02:28 +01:00
|
|
|
for appid, app in apps.items():
|
2021-06-08 15:31:55 +02:00
|
|
|
path = Path(app.metadatapath)
|
|
|
|
if path.suffix == '.yml':
|
2017-09-15 23:20:29 +02:00
|
|
|
logging.info(_("Rewriting '{appid}'").format(appid=appid))
|
2020-08-24 16:52:16 +02:00
|
|
|
else:
|
|
|
|
logging.warning(_('Cannot rewrite "{path}"').format(path=path))
|
|
|
|
continue
|
2015-10-04 09:29:51 +02:00
|
|
|
|
2015-09-25 07:40:49 +02:00
|
|
|
if options.list:
|
2015-12-07 20:12:26 +01:00
|
|
|
if not proper_format(app):
|
2016-11-25 15:20:19 +01:00
|
|
|
print(path)
|
2015-10-04 09:03:02 +02:00
|
|
|
continue
|
|
|
|
|
2023-05-08 22:03:25 +02:00
|
|
|
# TODO these should be moved to metadata.write_yaml()
|
2023-05-04 14:51:04 +02:00
|
|
|
builds = remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
if builds:
|
|
|
|
app['Builds'] = builds
|
2016-11-25 15:20:19 +01:00
|
|
|
|
2023-02-15 20:25:48 +01:00
|
|
|
# rewrite to temporary file before overwriting existing
|
2020-08-19 23:13:23 +02:00
|
|
|
# file in case there's a bug in write_metadata
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
2021-06-08 15:31:55 +02:00
|
|
|
tmp_path = Path(tmpdir) / path.name
|
2020-08-19 23:13:23 +02:00
|
|
|
metadata.write_metadata(tmp_path, app)
|
2023-02-15 20:25:48 +01:00
|
|
|
shutil.move(tmp_path, path)
|
2012-01-11 00:24:28 +01:00
|
|
|
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.debug(_("Finished"))
|
2012-02-26 15:18:58 +01:00
|
|
|
|
2016-11-15 21:55:06 +01:00
|
|
|
|
2012-02-26 15:18:58 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|