import.py: use pathlib and support Windows

This commit is contained in:
linsui 2021-06-08 18:33:22 +08:00
parent 7ddcbb3e80
commit d6eece6395
6 changed files with 175 additions and 153 deletions

View file

@ -20,12 +20,12 @@
import configparser
import git
import json
import os
import shutil
import sys
import yaml
from argparse import ArgumentParser
import logging
from pathlib import Path, PurePosixPath
try:
from yaml import CSafeLoader as SafeLoader
@ -44,15 +44,15 @@ options = None
# WARNING! This cannot be imported as a Python module, so reuseable functions need to go into common.py!
def clone_to_tmp_dir(app):
tmp_dir = 'tmp'
if not os.path.isdir(tmp_dir):
logging.info(_("Creating temporary directory"))
os.makedirs(tmp_dir)
tmp_dir = os.path.join(tmp_dir, 'importer')
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
def clone_to_tmp_dir(app):
tmp_dir = Path('tmp')
tmp_dir.mkdir(exist_ok=True)
tmp_dir = tmp_dir / 'importer'
if tmp_dir.exists():
shutil.rmtree(str(tmp_dir), onerror=common.handle_retree_error_on_windows)
vcs = common.getvcs(app.RepoType, app.Repo, tmp_dir)
vcs.gotorevision(options.rev)
@ -61,8 +61,8 @@ def clone_to_tmp_dir(app):
def check_for_kivy_buildozer(tmp_importer_dir, app, build):
versionCode = None
buildozer_spec = os.path.join(tmp_importer_dir, 'buildozer.spec')
if os.path.exists(buildozer_spec):
buildozer_spec = tmp_importer_dir / 'buildozer.spec'
if buildozer_spec.exists():
config = configparser.ConfigParser()
config.read(buildozer_spec)
import pprint
@ -132,15 +132,16 @@ def main():
raise FDroidException(_("This repo already has local metadata: %s") % local_metadata_files[0])
build = metadata.Build()
if options.url is None and os.path.isdir('.git'):
if options.url is None and Path('.git').is_dir():
app = metadata.App()
app.AutoName = os.path.basename(os.getcwd())
app.AutoName = Path.cwd().name
app.RepoType = 'git'
if os.path.exists('build.gradle') or os.path.exists('build.gradle.kts'):
if Path('build.gradle').exists() or Path('build.gradle.kts').exists():
build.gradle = ['yes']
git_repo = git.repo.Repo(os.getcwd())
# TODO: Python3.6: Should accept path-like
git_repo = git.Repo(str(Path.cwd()))
for remote in git.Remote.iter_items(git_repo):
if remote.name == 'origin':
url = git_repo.remotes.origin.url
@ -152,7 +153,9 @@ def main():
elif options.url:
app = common.get_app_from_url(options.url)
tmp_importer_dir = clone_to_tmp_dir(app)
git_repo = git.repo.Repo(tmp_importer_dir)
# TODO: Python3.6: Should accept path-like
git_repo = git.Repo(str(tmp_importer_dir))
if not options.omit_disable:
build.disable = 'Generated by import.py - check/set version fields and commit id'
write_local_file = False
@ -189,37 +192,36 @@ def main():
build.subdir = options.subdir
build.gradle = ['yes']
elif subdir:
build.subdir = subdir
build.subdir = str(PurePosixPath(subdir))
build.gradle = ['yes']
if options.license:
app.License = options.license
if options.categories:
app.Categories = options.categories.split(',')
if os.path.exists(os.path.join(subdir, 'jni')):
if (subdir / 'jni').exists():
build.buildjni = ['yes']
if os.path.exists(os.path.join(subdir, 'build.gradle')) \
or os.path.exists(os.path.join(subdir, 'build.gradle')):
if (subdir / 'build.gradle').exists() or (subdir / 'build.gradle').exists():
build.gradle = ['yes']
package_json = os.path.join(tmp_importer_dir, 'package.json') # react-native
pubspec_yaml = os.path.join(tmp_importer_dir, 'pubspec.yaml') # flutter
if os.path.exists(package_json):
package_json = tmp_importer_dir / 'package.json' # react-native
pubspec_yaml = tmp_importer_dir / 'pubspec.yaml' # flutter
if package_json.exists():
build.sudo = ['apt-get update || apt-get update', 'apt-get install -t stretch-backports npm', 'npm install -g react-native-cli']
build.init = ['npm install']
with open(package_json) as fp:
with package_json.open() as fp:
data = json.load(fp)
app.AutoName = data.get('name', app.AutoName)
app.License = data.get('license', app.License)
app.Description = data.get('description', app.Description)
app.WebSite = data.get('homepage', app.WebSite)
app_json = os.path.join(tmp_importer_dir, 'app.json')
if os.path.exists(app_json):
with open(app_json) as fp:
app_json = tmp_importer_dir / 'app.json'
if app_json.exists():
with app_json.open() as fp:
data = json.load(fp)
app.AutoName = data.get('name', app.AutoName)
if os.path.exists(pubspec_yaml):
with open(pubspec_yaml) as fp:
if pubspec_yaml.exists():
with pubspec_yaml.open() as fp:
data = yaml.load(fp, Loader=SafeLoader)
app.AutoName = data.get('name', app.AutoName)
app.License = data.get('license', app.License)
@ -232,8 +234,8 @@ def main():
'$$flutter$$/bin/flutter build apk',
]
git_modules = os.path.join(tmp_importer_dir, '.gitmodules')
if os.path.exists(git_modules):
git_modules = tmp_importer_dir / '.gitmodules'
if git_modules.exists():
build.submodules = True
metadata.post_metadata_parse(app)
@ -241,24 +243,25 @@ def main():
app['Builds'].append(build)
if write_local_file:
metadata.write_metadata('.fdroid.yml', app)
metadata.write_metadata(Path('.fdroid.yml'), app)
else:
# Keep the repo directory to save bandwidth...
if not os.path.exists('build'):
os.mkdir('build')
build_dir = os.path.join('build', appid)
if os.path.exists(build_dir):
Path('build').mkdir(exist_ok=True)
build_dir = Path('build') / appid
if build_dir.exists():
logging.warning(_('{path} already exists, ignoring import results!')
.format(path=build_dir))
sys.exit(1)
elif tmp_importer_dir is not None:
shutil.move(tmp_importer_dir, build_dir)
with open('build/.fdroidvcs-' + appid, 'w') as f:
f.write(app.RepoType + ' ' + app.Repo)
elif tmp_importer_dir:
# For Windows: Close the repo or a git.exe instance holds handles to repo
git_repo.close()
# TODO: Python3.9: Accepts a path-like object for both src and dst.
shutil.move(str(tmp_importer_dir), str(build_dir))
Path('build/.fdroidvcs-' + appid).write_text(app.RepoType + ' ' + app.Repo)
metadatapath = os.path.join('metadata', appid + '.yml')
metadatapath = Path('metadata') / (appid + '.yml')
metadata.write_metadata(metadatapath, app)
logging.info("Wrote " + metadatapath)
logging.info("Wrote " + str(metadatapath))
if __name__ == "__main__":