metadata.py/rewritemeta.py: use pathlib and support Windows

This commit is contained in:
linsui 2021-06-08 21:31:55 +08:00
parent 5635815898
commit 8f21f1e510
8 changed files with 768 additions and 511 deletions

View file

@ -18,11 +18,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from argparse import ArgumentParser
import os
import logging
import io
import tempfile
import shutil
from pathlib import Path
from . import _
from . import common
@ -36,9 +36,8 @@ def proper_format(app):
s = io.StringIO()
# TODO: currently reading entire file again, should reuse first
# read in metadata.py
with open(app.metadatapath, 'r') as f:
cur_content = f.read()
if app.metadatapath.endswith('.yml'):
cur_content = Path(app.metadatapath).read_text()
if Path(app.metadatapath).suffix == '.yml':
metadata.write_yaml(s, app)
content = s.getvalue()
s.close()
@ -65,8 +64,8 @@ def main():
apps = common.read_app_args(options.appid, allapps, False)
for appid, app in apps.items():
path = app.metadatapath
if path.endswith('.yml'):
path = Path(app.metadatapath)
if path.suffix == '.yml':
logging.info(_("Rewriting '{appid}'").format(appid=appid))
else:
logging.warning(_('Cannot rewrite "{path}"').format(path=path))
@ -91,9 +90,10 @@ def main():
# rewrite to temporary file before overwriting existsing
# file in case there's a bug in write_metadata
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = os.path.join(tmpdir, os.path.basename(path))
tmp_path = Path(tmpdir) / path.name
metadata.write_metadata(tmp_path, app)
shutil.move(tmp_path, path)
# TODO: Python3.6: Accept path-lik
shutil.move(str(tmp_path), str(path))
logging.debug(_("Finished"))