1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-06-03 06:10:10 +02:00
fdroidserver/fdroidserver/rewritemeta.py

118 lines
3.5 KiB
Python
Raw Permalink Normal View History

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/>.
from argparse import ArgumentParser
import logging
2016-01-04 17:00:21 +01:00
import io
import tempfile
import shutil
from pathlib import Path
from . import _
2016-01-04 17:37:35 +01:00
from . import common
from . import metadata
2012-01-11 00:24:28 +01:00
config = None
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')
if Path(app.metadatapath).suffix == '.yml':
metadata.write_yaml(s, app)
2015-12-07 20:12:26 +01:00
content = s.getvalue()
s.close()
return content == cur_content
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:
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():
continue
new[k] = v
newbuilds.append(new)
return newbuilds
def main():
global config
2012-01-11 00:24:28 +01:00
parser = ArgumentParser()
2015-09-12 08:42:50 +02:00
common.setup_global_opts(parser)
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")
)
metadata.add_metadata_arguments(parser)
options = common.parse_args(parser)
metadata.warnings_action = options.W
2012-01-11 00:24:28 +01:00
config = common.read_config()
# Get all apps...
allapps = metadata.read_metadata(options.appid)
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():
path = Path(app.metadatapath)
if path.suffix == '.yml':
logging.info(_("Rewriting '{appid}'").format(appid=appid))
else:
logging.warning(_('Cannot rewrite "{path}"').format(path=path))
continue
2015-10-04 09:29:51 +02:00
if options.list:
2015-12-07 20:12:26 +01:00
if not proper_format(app):
print(path)
2015-10-04 09:03:02 +02:00
continue
# TODO these should be moved to metadata.write_yaml()
builds = remove_blank_flags_from_builds(app.get('Builds'))
if builds:
app['Builds'] = builds
# rewrite to temporary file before overwriting existing
# file in case there's a bug in write_metadata
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir) / path.name
metadata.write_metadata(tmp_path, app)
shutil.move(tmp_path, path)
2012-01-11 00:24:28 +01:00
logging.debug(_("Finished"))
if __name__ == "__main__":
main()