Convert finalize_sdk.py to use pathlib

The legacy os path stuff is a bit cumbersome to work with.

Test: together with the rest of the CLs in this set
Change-Id: I94f7c6f04e9f3e1644b1cc6585c2c8752f6b33ef
This commit is contained in:
Anton Hansson
2022-04-06 11:47:12 +01:00
parent 27af85827a
commit 0e8dbf88fb

View File

@@ -9,6 +9,7 @@ import tempfile
import zipfile import zipfile
from collections import defaultdict from collections import defaultdict
from pathlib import Path
# See go/fetch_artifact for details on this script. # See go/fetch_artifact for details on this script.
FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact' FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
@@ -29,14 +30,14 @@ def fail(*args, **kwargs):
sys.exit(1) sys.exit(1)
def fetch_artifacts(target, build_id, artifact_path): def fetch_artifacts(target, build_id, artifact_path):
tmpdir = tempfile.TemporaryDirectory().name tmpdir = Path(tempfile.TemporaryDirectory().name)
os.mkdir(tmpdir) tmpdir.mkdir()
print('Fetching %s from %s ...' % (artifact_path, target)) print('Fetching %s from %s ...' % (artifact_path, target))
fetch_cmd = [FETCH_ARTIFACT] fetch_cmd = [FETCH_ARTIFACT]
fetch_cmd.extend(['--bid', str(build_id)]) fetch_cmd.extend(['--bid', str(build_id)])
fetch_cmd.extend(['--target', target]) fetch_cmd.extend(['--target', target])
fetch_cmd.append(artifact_path) fetch_cmd.append(artifact_path)
fetch_cmd.append(tmpdir) fetch_cmd.append(str(tmpdir))
print("Running: " + ' '.join(fetch_cmd)) print("Running: " + ' '.join(fetch_cmd))
try: try:
subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT) subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
@@ -47,8 +48,8 @@ def fetch_artifacts(target, build_id, artifact_path):
def repo_for_sdk(filename): def repo_for_sdk(filename):
module = filename.split('-')[0] module = filename.split('-')[0]
target_dir = '' target_dir = ''
if module == 'media': return 'prebuilts/module_sdk/Media' if module == 'media': return Path('prebuilts/module_sdk/Media')
if module == 'tethering': return 'prebuilts/module_sdk/Connectivity' if module == 'tethering': return Path('prebuilts/module_sdk/Connectivity')
for dir in os.listdir('prebuilts/module_sdk/'): for dir in os.listdir('prebuilts/module_sdk/'):
if module.lower() in dir.lower(): if module.lower() in dir.lower():
if target_dir: if target_dir:
@@ -57,7 +58,7 @@ def repo_for_sdk(filename):
if not target_dir: if not target_dir:
fail('Could not find a target dir for %s' % filename) fail('Could not find a target dir for %s' % filename)
return 'prebuilts/module_sdk/%s' % target_dir return Path('prebuilts/module_sdk/%s' % target_dir)
def dir_for_sdk(filename, version): def dir_for_sdk(filename, version):
base = str(version) base = str(version)
@@ -85,14 +86,14 @@ tmpdir = fetch_artifacts(BUILD_TARGET, args.bid, ARTIFACT_PATTERN % args.finaliz
created_dirs = defaultdict(list) created_dirs = defaultdict(list)
for f in os.listdir(tmpdir): for f in tmpdir.iterdir():
repo = repo_for_sdk(f) repo = repo_for_sdk(f.name)
dir = dir_for_sdk(f, args.finalize_sdk) dir = dir_for_sdk(f.name, args.finalize_sdk)
target_dir = os.path.join(repo, dir) target_dir = repo.joinpath(dir)
if os.path.isfile(target_dir): if target_dir.is_dir():
print('Removing existing dir %s' % target_dir) print('Removing existing dir %s' % target_dir)
shutil.rmtree(target_dir) shutil.rmtree(target_dir)
with zipfile.ZipFile(os.path.join(tmpdir, f)) as zipFile: with zipfile.ZipFile(tmpdir.joinpath(f)) as zipFile:
zipFile.extractall(target_dir) zipFile.extractall(target_dir)
print('Created %s' % target_dir) print('Created %s' % target_dir)
@@ -101,7 +102,7 @@ for f in os.listdir(tmpdir):
subprocess.check_output(['repo', 'start', branch_name] + list(created_dirs.keys())) subprocess.check_output(['repo', 'start', branch_name] + list(created_dirs.keys()))
print('Running git commit') print('Running git commit')
for repo in created_dirs: for repo in created_dirs:
git = ['git', '-C', repo] git = ['git', '-C', str(repo)]
subprocess.check_output(git + ['add'] + created_dirs[repo]) subprocess.check_output(git + ['add'] + created_dirs[repo])
if args.amend_last_commit: if args.amend_last_commit:
change_id = '\n' + re.search(r'Change-Id: [^\\n]+', str(subprocess.check_output(git + ['log', '-1']))).group(0) change_id = '\n' + re.search(r'Change-Id: [^\\n]+', str(subprocess.check_output(git + ['log', '-1']))).group(0)