[CRIU] [PATCH 4/5] p.haul: move arguments parsing to separate file

Nikita Spiridonov nspiridonov at odin.com
Fri Nov 20 08:49:02 PST 2015


Move arguments parsing code from p.haul and p.haul-service to
separate file (phaul/args_parser.py). This change is needed to
implement module specific arguments.

Signed-off-by: Nikita Spiridonov <nspiridonov at odin.com>
---
 p.haul               |   32 +----------------------------
 p.haul-service       |   11 +--------
 phaul/args_parser.py |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 39 deletions(-)
 create mode 100644 phaul/args_parser.py

diff --git a/p.haul b/p.haul
index 6c7a70a..b56fb5c 100755
--- a/p.haul
+++ b/p.haul
@@ -1,13 +1,10 @@
 #!/usr/bin/env python
 
 import sys
-import argparse
 import logging
+import phaul.args_parser
 import phaul.iters
 import phaul.connection
-import phaul.images
-import phaul.criu_api
-import phaul.htype
 
 # Usage idea
 # p.haul <type> <id> --fdrpc <fd> --fdmem <fd> --fdfs <fd>
@@ -28,33 +25,8 @@ def log_uncaught_exception(type, value, traceback):
 	logging.error(value, exc_info=(type, value, traceback))
 
 
-# Initialize arguments parser
-parser = argparse.ArgumentParser("Process HAULer")
-parser.set_defaults(pre_dump=phaul.iters.PRE_DUMP_AUTO_DETECT)
-parser.add_argument("type", choices=phaul.htype.get_haul_names(),
-	help="Type of hat to haul, e.g. vz, lxc, or docker")
-parser.add_argument("id", help="ID of what to haul")
-parser.add_argument("--to", help="IP where to haul")
-parser.add_argument("--fdrpc", type=int, required=True, help="File descriptor of rpc socket")
-parser.add_argument("--fdmem", type=int, required=True, help="File descriptor of memory socket")
-parser.add_argument("--fdfs", type=int, required=True, help="File descriptor of fs socket")
-parser.add_argument("-v", default=phaul.criu_api.def_verb, type=int, dest="verbose", help="Verbosity level")
-parser.add_argument("--keep-images", default=False, action='store_true', help="Keep images after migration")
-parser.add_argument("--dst-rpid", default=None, help="Write pidfile on restore")
-parser.add_argument("--img-path", default=phaul.images.def_path,
-	help="Directory where to put images")
-parser.add_argument("--pid-root", help="Path to tree's FS root")
-parser.add_argument("--force", default=False, action='store_true', help="Don't do any sanity (CPU compat) checks")
-parser.add_argument("--log-file", help="Write logging messages to specified file")
-parser.add_argument("-j", "--shell-job", default=False, action='store_true',
-	help="Allow migration of shell jobs")
-parser.add_argument('--no-pre-dump', dest='pre_dump', action='store_const',
-	const=phaul.iters.PRE_DUMP_DISABLE, help='Force disable pre-dumps')
-parser.add_argument('--pre-dump', dest='pre_dump', action='store_const',
-	const=phaul.iters.PRE_DUMP_ENABLE, help='Force enable pre-dumps')
-
 # Parse arguments
-args = parser.parse_args()
+args = phaul.args_parser.parse_client_args()
 
 # Configure logging
 logging.basicConfig(filename=args.log_file, filemode="w", level=logging.INFO,
diff --git a/p.haul-service b/p.haul-service
index cb20d71..465e396 100755
--- a/p.haul-service
+++ b/p.haul-service
@@ -2,8 +2,8 @@
 
 import sys
 import signal
-import argparse
 import logging
+import phaul.args_parser
 import phaul.xem_rpc
 import phaul.service
 import phaul.connection
@@ -19,15 +19,8 @@ def fin(foo, bar):
 	stop_fd.close()
 
 
-# Initialize arguments parser
-parser = argparse.ArgumentParser("Process HAULer service server")
-parser.add_argument("--fdrpc", type=int, required=True, help="File descriptor of rpc socket")
-parser.add_argument("--fdmem", type=int, required=True, help="File descriptor of memory socket")
-parser.add_argument("--fdfs", type=int, required=True, help="File descriptor of fs socket")
-parser.add_argument("--log-file", help="Write logging messages to specified file")
-
 # Parse arguments
-args = parser.parse_args()
+args = phaul.args_parser.parse_service_args()
 
 # Configure logging
 logging.basicConfig(filename=args.log_file, filemode="w", level=logging.INFO,
diff --git a/phaul/args_parser.py b/phaul/args_parser.py
new file mode 100644
index 0000000..cbe306b
--- /dev/null
+++ b/phaul/args_parser.py
@@ -0,0 +1,53 @@
+#
+# p.haul command line arguments parsers
+#
+
+import argparse
+import htype
+import images
+import criu_api
+import iters
+
+
+def parse_client_args():
+	"""Parse p.haul command line arguments"""
+
+	parser = argparse.ArgumentParser("Process HAULer")
+	parser.set_defaults(pre_dump=iters.PRE_DUMP_AUTO_DETECT)
+
+	parser.add_argument("type", choices=htype.get_haul_names(),
+		help="Type of hat to haul, e.g. vz, lxc, or docker")
+	parser.add_argument("id", help="ID of what to haul")
+	parser.add_argument("--to", help="IP where to haul")
+	parser.add_argument("--fdrpc", type=int, required=True, help="File descriptor of rpc socket")
+	parser.add_argument("--fdmem", type=int, required=True, help="File descriptor of memory socket")
+	parser.add_argument("--fdfs", type=int, required=True, help="File descriptor of fs socket")
+	parser.add_argument("-v", default=criu_api.def_verb, type=int, dest="verbose", help="Verbosity level")
+	parser.add_argument("--keep-images", default=False, action='store_true', help="Keep images after migration")
+	parser.add_argument("--dst-rpid", default=None, help="Write pidfile on restore")
+	parser.add_argument("--img-path", default=images.def_path,
+		help="Directory where to put images")
+	parser.add_argument("--pid-root", help="Path to tree's FS root")
+	parser.add_argument("--force", default=False, action='store_true', help="Don't do any sanity (CPU compat) checks")
+	parser.add_argument("--log-file", help="Write logging messages to specified file")
+	parser.add_argument("-j", "--shell-job", default=False, action='store_true',
+		help="Allow migration of shell jobs")
+	parser.add_argument('--no-pre-dump', dest='pre_dump', action='store_const',
+		const=iters.PRE_DUMP_DISABLE, help='Force disable pre-dumps')
+	parser.add_argument('--pre-dump', dest='pre_dump', action='store_const',
+		const=iters.PRE_DUMP_ENABLE, help='Force enable pre-dumps')
+
+	return parser.parse_args()
+
+
+def parse_service_args():
+	"""Parse p.haul-service command line arguments"""
+
+	parser = argparse.ArgumentParser("Process HAULer service server")
+
+	parser.add_argument("--fdrpc", type=int, required=True, help="File descriptor of rpc socket")
+	parser.add_argument("--fdmem", type=int, required=True, help="File descriptor of memory socket")
+	parser.add_argument("--fdfs", type=int, required=True, help="File descriptor of fs socket")
+	parser.add_argument("--log-file", help="Write logging messages to specified file")
+
+	return parser.parse_args()
-- 
1.7.1



More information about the CRIU mailing list