Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2066)

Unified Diff: native_client_sdk/src/tools/create_nmf.py

Issue 720233003: [NaCl SDK] Convert python scripts from optparse to argparse. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/tools/create_nmf.py
diff --git a/native_client_sdk/src/tools/create_nmf.py b/native_client_sdk/src/tools/create_nmf.py
index aa269dd0b34d21c7373f28442d983825828d3f40..80cd18d24cfedceeb427cbb1f4dadf1ed2854541 100755
--- a/native_client_sdk/src/tools/create_nmf.py
+++ b/native_client_sdk/src/tools/create_nmf.py
@@ -11,7 +11,7 @@ any shared libraries dependencies that the executables might have.
import errno
import json
-import optparse
+import argparse
binji 2014/11/13 23:57:03 sort
Sam Clegg 2014/11/30 17:55:12 Done.
import os
import posixpath
import shutil
@@ -158,7 +158,7 @@ class ArchFile(object):
self.path = path
self.url = url
self.arch = arch
- if not arch:
+ if arch is None:
self.arch = ParseElfHeader(path)[0]
def __repr__(self):
@@ -402,7 +402,7 @@ class NmfUtils(object):
def GetManifest(self):
"""Returns a JSON-formatted dict containing the NaCl dependencies"""
- if not self.manifest:
+ if self.manifest is None:
if self.pnacl:
self._GeneratePNaClManifest()
else:
@@ -465,17 +465,30 @@ def ParseExtraFiles(encoded_list, err):
return canonicalized
+def IsValidSDKRoot(directory):
+ if not os.path.exists(os.path.join(directory, 'toolchain')):
binji 2014/11/13 23:57:03 just return os.path.exists(...) ?
Sam Clegg 2014/11/30 17:55:12 Split into seperate CL
+ return False
+
+ return True
+
+
def GetSDKRoot():
"""Determine current NACL_SDK_ROOT, either via the environment variable
itself, or by attempting to derive it from the location of this script.
"""
sdk_root = os.environ.get('NACL_SDK_ROOT')
- if not sdk_root:
- sdk_root = os.path.dirname(SCRIPT_DIR)
- if not os.path.exists(os.path.join(sdk_root, 'toolchain')):
- return None
+ if sdk_root:
binji 2014/11/13 23:57:03 Is this change necessary?
Sam Clegg 2014/11/30 17:55:12 Separate CL
+ if not IsValidSDKRoot(sdk_root):
+ raise Error('Invalid NACL_SDK_ROOT: %s' % sdk_root)
+ return sdk_root
- return sdk_root
+ # If no $NACL_SDK_ROOT set then use the current script location to
+ # attempt to drive the root.
binji 2014/11/13 23:57:03 sp: derive
Sam Clegg 2014/11/30 17:55:12 Separate CL
+ sdk_root = os.path.dirname(SCRIPT_DIR)
+ if IsValidSDKRoot(sdk_root):
+ return sdk_root
+
+ return None
def FindObjdumpExecutable():
@@ -483,7 +496,7 @@ def FindObjdumpExecutable():
object dependencies.
"""
sdk_root = GetSDKRoot()
- if not sdk_root:
+ if sdk_root is None:
return None
osname = getos.GetPlatform()
@@ -508,7 +521,7 @@ def GetDefaultLibPath(config):
"""
assert(config in ('Debug', 'Release'))
sdk_root = GetSDKRoot()
- if not sdk_root:
+ if sdk_root is None:
# TOOD(sbc): output a warning here? We would also need to suppress
# the warning when run from the chromium build.
return []
@@ -542,59 +555,59 @@ def GetDefaultLibPath(config):
def main(argv):
- parser = optparse.OptionParser(
- usage='Usage: %prog [options] nexe [extra_libs...]', description=__doc__)
- parser.add_option('-o', '--output', dest='output',
- help='Write manifest file to FILE (default is stdout)',
- metavar='FILE')
- parser.add_option('-D', '--objdump', dest='objdump',
- help='Override the default "objdump" tool used to find '
- 'shared object dependencies',
- metavar='TOOL')
- parser.add_option('--no-default-libpath', action='store_true',
- help="Don't include the SDK default library paths")
- parser.add_option('--debug-libs', action='store_true',
- help='Use debug library paths when constructing default '
- 'library path.')
- parser.add_option('-L', '--library-path', dest='lib_path',
- action='append', default=[],
- help='Add DIRECTORY to library search path',
- metavar='DIRECTORY')
- parser.add_option('-P', '--path-prefix', dest='path_prefix', default='',
- help='Deprecated. An alias for --lib-prefix.',
- metavar='DIRECTORY')
- parser.add_option('-p', '--lib-prefix', dest='lib_prefix', default='',
- help='A path to prepend to shared libraries in the .nmf',
- metavar='DIRECTORY')
- parser.add_option('-N', '--nexe-prefix', dest='nexe_prefix', default='',
- help='A path to prepend to nexes in the .nmf',
- metavar='DIRECTORY')
- parser.add_option('-s', '--stage-dependencies', dest='stage_dependencies',
- help='Destination directory for staging libraries',
- metavar='DIRECTORY')
- parser.add_option('--no-arch-prefix', action='store_true',
- help='Don\'t put shared libraries in the lib32/lib64 '
- 'directories. Instead, they will be put in the same '
- 'directory as the .nexe that matches its architecture.')
- parser.add_option('-t', '--toolchain', help='Legacy option, do not use')
- parser.add_option('-n', '--name', dest='name',
- help='Rename FOO as BAR',
- action='append', default=[], metavar='FOO,BAR')
- parser.add_option('-x', '--extra-files',
- help=('Add extra key:file tuple to the "files"' +
- ' section of the .nmf'),
- action='append', default=[], metavar='FILE')
- parser.add_option('-O', '--pnacl-optlevel',
- help='Set the optimization level to N in PNaCl manifests',
- metavar='N')
- parser.add_option('--pnacl-debug-optlevel',
- help='Set the optimization level to N for debugging '
- 'sections in PNaCl manifests',
- metavar='N')
- parser.add_option('-v', '--verbose',
- help='Verbose output', action='store_true')
- parser.add_option('-d', '--debug-mode',
- help='Debug mode', action='store_true')
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('-o', '--output', dest='output',
+ help='Write manifest file to FILE (default is stdout)',
+ metavar='FILE')
+ parser.add_argument('-D', '--objdump', dest='objdump',
+ help='Override the default "objdump" tool used to find '
+ 'shared object dependencies',
+ metavar='TOOL')
+ parser.add_argument('--no-default-libpath', action='store_true',
+ help="Don't include the SDK default library paths")
+ parser.add_argument('--debug-libs', action='store_true',
+ help='Use debug library paths when constructing default '
+ 'library path.')
+ parser.add_argument('-L', '--library-path', dest='lib_path',
+ action='append', default=[],
+ help='Add DIRECTORY to library search path',
+ metavar='DIRECTORY')
+ parser.add_argument('-P', '--path-prefix', dest='path_prefix', default='',
+ help='Deprecated. An alias for --lib-prefix.',
+ metavar='DIRECTORY')
+ parser.add_argument('-p', '--lib-prefix', dest='lib_prefix', default='',
+ help='A path to prepend to shared libraries in the .nmf',
+ metavar='DIRECTORY')
+ parser.add_argument('-N', '--nexe-prefix', dest='nexe_prefix', default='',
+ help='A path to prepend to nexes in the .nmf',
+ metavar='DIRECTORY')
+ parser.add_argument('-s', '--stage-dependencies', dest='stage_dependencies',
+ help='Destination directory for staging libraries',
+ metavar='DIRECTORY')
+ parser.add_argument('--no-arch-prefix', action='store_true',
+ help='Don\'t put shared libraries in the lib32/lib64 '
+ 'directories. Instead, they will be put in the same '
+ 'directory as the .nexe that matches its architecture.')
+ parser.add_argument('-t', '--toolchain', help='Legacy option, do not use')
+ parser.add_argument('-n', '--name', dest='name',
+ help='Rename FOO as BAR',
+ action='append', default=[], metavar='FOO,BAR')
+ parser.add_argument('-x', '--extra-files',
+ help=('Add extra key:file tuple to the "files"' +
+ ' section of the .nmf'),
+ action='append', default=[], metavar='FILE')
+ parser.add_argument('-O', '--pnacl-optlevel',
+ help='Set the optimization level to N in PNaCl manifests',
+ metavar='N')
+ parser.add_argument('--pnacl-debug-optlevel',
+ help='Set the optimization level to N for debugging '
+ 'sections in PNaCl manifests',
+ metavar='N')
+ parser.add_argument('-v', '--verbose',
+ help='Verbose output', action='store_true')
+ parser.add_argument('-d', '--debug-mode',
+ help='Debug mode', action='store_true')
+ parser.add_argument('executables', nargs='+')
# To enable bash completion for this command first install optcomplete
# and then add this line to your .bashrc:
@@ -605,7 +618,7 @@ def main(argv):
except ImportError:
pass
- options, args = parser.parse_args(argv)
+ options = parser.parse_args(argv)
if options.verbose:
Trace.verbose = True
if options.debug_mode:
@@ -614,9 +627,6 @@ def main(argv):
if options.toolchain is not None:
sys.stderr.write('warning: option -t/--toolchain is deprecated.\n')
- if len(args) < 1:
- parser.error('No nexe files specified. See --help for more info')
-
canonicalized = ParseExtraFiles(options.extra_files, sys.stderr)
if canonicalized is None:
parser.error('Bad --extra-files (-x) argument syntax')
@@ -661,7 +671,7 @@ def main(argv):
nmf_root = os.path.dirname(options.output)
nmf = NmfUtils(objdump=options.objdump,
- main_files=args,
+ main_files=options.executables,
lib_path=options.lib_path,
extra_files=canonicalized,
lib_prefix=options.lib_prefix,
@@ -672,7 +682,7 @@ def main(argv):
pnacl_debug_optlevel=pnacl_debug_optlevel,
nmf_root=nmf_root)
- if not options.output:
+ if options.output is None:
sys.stdout.write(nmf.GetJson())
else:
with open(options.output, 'w') as output:

Powered by Google App Engine
This is Rietveld 408576698