Skip to content

Commit

Permalink
Migrate from optparse to argparse
Browse files Browse the repository at this point in the history
#codehealth

PiperOrigin-RevId: 597580829
  • Loading branch information
yuryu authored and hiroyuki-komatsu committed Jan 11, 2024
1 parent 825915c commit bb7c45d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
44 changes: 23 additions & 21 deletions src/build_tools/copy_dll_and_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,34 @@

"""Utilitis for copying dependent files for Windows build."""

import argparse
import datetime
import logging
import optparse
import os
import shutil

from util import PrintErrorAndExit

def ParseOption():
"""Parse command line options."""
parser = optparse.OptionParser()
MSG = ' you can use %s as path separator' % os.pathsep
parser.add_option('--dll_paths', dest='dll_paths', default='',
help='Search paths for DLLs.' + MSG)
parser.add_option('--pdb_paths', dest='pdb_paths', default='',
help='Search paths for PDB files.' + MSG)
parser.add_option('--target_dir', dest='target_dir', default='',
help='Deploy target directory.')
parser.add_option('--basenames', dest='basenames', default='',
help='The basenames of DLL and/or PDB.' + MSG)
parser = argparse.ArgumentParser()
additional_desc = ' you can use %s as path separator' % os.pathsep
parser.add_argument(
'--dll_paths',
required=True,
help='Search paths for DLLs.' + additional_desc,
)
parser.add_argument(
'--pdb_paths',
required=True,
help='Search paths for PDB files.' + additional_desc,
)
parser.add_argument(
'--target_dir', required=True, help='Deploy target directory.'
)
parser.add_argument(
'--basenames',
required=True,
help='The basenames of DLL and/or PDB.' + additional_desc,
)

(opts, _) = parser.parse_args()

Expand All @@ -61,8 +69,8 @@ def DeployMain(full_filename, src_paths, target_absdir):
Args:
full_filename: A string which represents the filename to be copied.
src_paths: A string which represents file search path with delimited by
a OS-dependent path separator.
src_paths: A string which represents file search path with delimited by a
OS-dependent path separator.
target_absdir: A string which represents target directory name.
"""
if not src_paths:
Expand Down Expand Up @@ -105,12 +113,6 @@ def _GetLastModifiedTime(filepath):
def main():
opts = ParseOption()

if not opts.basenames:
PrintErrorAndExit('--basenames option is mandatory.')

if not opts.target_dir:
PrintErrorAndExit('--target_dir option is mandatory.')

target_absdir = os.path.abspath(opts.target_dir)
for basename in opts.basenames.split(os.pathsep):
DeployMain(basename + '.dll', opts.dll_paths, target_absdir)
Expand Down
81 changes: 40 additions & 41 deletions src/build_tools/copy_qt_frameworks_mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@

__author__ = "horo"

import argparse
import dataclasses
import optparse
import os
from copy_file import CopyFiles
from util import PrintErrorAndExit
from util import RunOrDie


Expand All @@ -54,20 +53,18 @@ class QtModule:
name: the name of the component (e.g. 'QtCore')
version: the version string in framework (e.g. '5' and 'A')
"""

name: str
version: str


def ParseOption():
"""Parse command line options."""
parser = optparse.OptionParser()
parser.add_option('--qtdir', dest='qtdir')
parser.add_option('--qtver', dest='qtver', type=int, default=None)
parser.add_option('--target', dest='target')

(opts, _) = parser.parse_args()

return opts
def ParseArgs():
"""Parses command line options."""
parser = argparse.ArgumentParser()
parser.add_argument('--qtdir', required=True)
parser.add_argument('--qtver', required=True, type=int)
parser.add_argument('--target', required=True)
return parser.parse_args()


def GetFrameworkPath(module: QtModule):
Expand Down Expand Up @@ -96,15 +93,18 @@ def CopyQt(qtdir, module: QtModule, target):

# cp {qtdir}/lib/QtCore.framework/Versions/{ver}/QtCore
# {target}/QtCore.framework/Versions/{ver}/QtCore
CopyFiles([srcdir + module.name],
f'{dstdir}Versions/{module.version}/{module.name}')
CopyFiles(
[srcdir + module.name], f'{dstdir}Versions/{module.version}/{module.name}'
)

# Copies Resources of QtGui
# cp -r {qtdir}/lib/QtCore.framework/Resources
# {target}/QtCore.framework/Versions/{ver}/Resources
CopyFiles([srcdir + 'Resources'],
f'{dstdir}Versions/{module.version}/Resources',
recursive=True)
CopyFiles(
[srcdir + 'Resources'],
f'{dstdir}Versions/{module.version}/Resources',
recursive=True,
)

# ln -s {ver} {target}/QtCore.framework/Versions/Current
Symlink(f'{module.version}', dstdir + 'Versions/Current')
Expand All @@ -119,46 +119,43 @@ def CopyQt(qtdir, module: QtModule, target):
def ChangeReferences(path, target, ref_to, ref_framework_paths=None):
"""Change the references of frameworks, by using install_name_tool."""
# Change id
cmd = ['install_name_tool',
'-id', '%s/%s' % (ref_to, path),
'%s/%s' % (target, path)]
cmd = [
'install_name_tool',
'-id',
'%s/%s' % (ref_to, path),
'%s/%s' % (target, path),
]
RunOrDie(cmd)

if not ref_framework_paths:
return

# Change references
for ref_framework_path in ref_framework_paths:
change_cmd = ['install_name_tool', '-change',
'@rpath/%s' % ref_framework_path,
'%s/%s' % (ref_to, ref_framework_path),
'%s/%s' % (target, path)]
change_cmd = [
'install_name_tool',
'-change',
'@rpath/%s' % ref_framework_path,
'%s/%s' % (ref_to, ref_framework_path),
'%s/%s' % (target, path),
]
RunOrDie(change_cmd)


def main():
opt = ParseOption()

if not opt.qtdir:
PrintErrorAndExit('--qtdir option is mandatory.')

if not opt.qtver:
PrintErrorAndExit('--qtver option is mandatory.')

if not opt.target:
PrintErrorAndExit('--target option is mandatory.')
args = ParseArgs()

qtdir = os.path.abspath(opt.qtdir)
target = os.path.abspath(opt.target)
qtdir = os.path.abspath(args.qtdir)
target = os.path.abspath(args.target)

ref_to = '@executable_path/../../../ConfigDialog.app/Contents/Frameworks'

if opt.qtver == 5:
if args.qtver == 5:
version = '5' # Qt5 uses '5' as the version name
elif opt.qtver == 6:
elif args.qtver == 6:
version = 'A' # Qt5 uses 'A' as the version name
else:
raise ValueError(f'Invalid qtver: {opt.qtver}')
raise ValueError(f'Invalid qtver: {args.qtver}')
qt_core = QtModule(name='QtCore', version=version)
qt_gui = QtModule(name='QtGui', version=version)
qt_widgets = QtModule(name='QtWidgets', version=version)
Expand All @@ -170,8 +167,10 @@ def main():
CopyQt(qtdir, qt_print_support, target)

libqcocoa = 'QtCore.framework/Resources/plugins/platforms/libqcocoa.dylib'
CopyFiles(['%s/plugins/platforms/libqcocoa.dylib' % qtdir],
'%s/%s' % (target, libqcocoa))
CopyFiles(
['%s/plugins/platforms/libqcocoa.dylib' % qtdir],
'%s/%s' % (target, libqcocoa),
)

changed_refs = []
for ref in [
Expand Down

0 comments on commit bb7c45d

Please sign in to comment.