[PATCH] libosmocore[master]: utils/conv_gen.py: use getopt instead of argparse for sysmoB...

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Fri Feb 10 13:10:37 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1792

to look at the new patch set (#2).

utils/conv_gen.py: use getopt instead of argparse for sysmoBTS SDK

The poky based sysmoBTS SDK has no argparse in its python, but it does provide
getopt. With slight bloat due to getopt avoid adding a dependency to the SDK.

Change-Id: Ic301ee3674c8a86add43d1b1410263f8bc8bea07
---
M utils/conv_gen.py
1 file changed, 65 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/92/1792/2

diff --git a/utils/conv_gen.py b/utils/conv_gen.py
index e6eb50c..1a8c1be 100644
--- a/utils/conv_gen.py
+++ b/utils/conv_gen.py
@@ -23,7 +23,7 @@
  */
 """
 
-import sys, os, math, argparse
+import sys, os, math, getopt
 from functools import reduce
 import conv_codes_gsm
 
@@ -281,26 +281,74 @@
 
 		code.gen_tables(prefix, f, shared_tables = shared)
 
+# (Using getopt instead of argparse because the sysmoBTS SDK has no argparse)
+def usage():
+        print '''usage: x.py [-h] [-p PREFIX] [-n TARGET_NAME] [-P TARGET_PATH]
+            {gen_codes} {gsm}
+
+positional arguments:
+  {gen_codes}           what to generate
+  {gsm}                 convolutional code family
+
+optional arguments:
+  -h, --help            show this help message and exit
+  -p PREFIX, --prefix PREFIX
+                        internal naming prefix
+  -n TARGET_NAME, --target-name TARGET_NAME
+                        target name for generated file
+  -P TARGET_PATH, --target-path TARGET_PATH
+                        target path for generated file
+usage: x.py [-h] [-p PREFIX] [-n TARGET_NAME] [-P TARGET_PATH]
+            {gen_codes} {gsm}'''
+
+class CmdlineOpts:
+        action = None
+        family = None
+        prefix = None
+        target_name = None
+        target_path = None
+
 def parse_argv():
-	parser = argparse.ArgumentParser()
+        try:
+                opts, args = getopt.getopt(sys.argv[1:], "hp:n:P:",
+                                           ["help", "prefix=", "target-name=", "target-path="])
+        except getopt.GetoptError as err:
+                print str(err)
+                usage()
+                sys.exit(2)
 
-	# Positional arguments
-	parser.add_argument("action",
-		help = "what to generate",
-		choices = ["gen_codes"])
-	parser.add_argument("family",
-		help = "convolutional code family",
-		choices = ["gsm"])
+        argv = CmdlineOpts()
+        for o, a in opts:
+                if o in ("-h", "--help"):
+                        usage()
+                        sys.exit()
+                elif o in ("-p", "--prefix"):
+                        argv.prefix = a
+                elif o in ("-n", "--target-name"):
+                        argv.target_name = a
+                elif o in ("-P", "--target-path"):
+                        argv.target_path = a
+                else:
+                        assert False, "unknown option: %r" % o
 
-	# Optional arguments
-	parser.add_argument("-p", "--prefix",
-		help = "internal naming prefix")
-	parser.add_argument("-n", "--target-name",
-		help = "target name for generated file")
-	parser.add_argument("-P", "--target-path",
-		help = "target path for generated file")
+        try:
+                argv.action, argv.family = args
+        except:
+                usage()
+                assert False, "need two arguments gen_codes and gsm, got %d" % len(args)
 
-	return parser.parse_args()
+        action_choices = ('gen_codes', )
+        if argv.action not in action_choices:
+                usage()
+                assert False, ("argument 'action': invalid choice: %r (choose from %s)"
+                               % (argv.action, ', '.join(action_choices)))
+
+        family_choices = ('gsm', )
+        if argv.family not in family_choices:
+                usage()
+                assert False, ("argument 'family': invalid choice: %r (choose from %s)"
+                                           % (argv.family, ', '.join(family_choices)))
+        return argv
 
 if __name__ == '__main__':
 	# Parse and verify arguments

-- 
To view, visit https://gerrit.osmocom.org/1792
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Ic301ee3674c8a86add43d1b1410263f8bc8bea07
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder


More information about the gerrit-log mailing list