Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
avocado
提交
b9a719cb
A
avocado
项目概览
openeuler
/
avocado
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
avocado
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b9a719cb
编写于
4月 11, 2018
作者:
C
Cleber Rosa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
avocado/utils/process.py: add cmd_split() function
Signed-off-by:
N
Cleber Rosa
<
crosa@redhat.com
>
上级
df98b07d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
49 addition
and
0 deletion
+49
-0
avocado/utils/process.py
avocado/utils/process.py
+21
-0
selftests/unit/test_utils_process.py
selftests/unit/test_utils_process.py
+28
-0
未找到文件。
avocado/utils/process.py
浏览文件 @
b9a719cb
...
...
@@ -34,6 +34,7 @@ import time
from
io
import
BytesIO
,
UnsupportedOperation
from
six
import
PY2
,
string_types
from
.
import
astring
from
.
import
gdb
from
.
import
runtime
from
.
import
path
...
...
@@ -281,6 +282,26 @@ def binary_from_shell_cmd(cmd, encoding=None):
raise
ValueError
(
"Unable to parse first binary from '%s'"
%
cmd
)
def
cmd_split
(
cmd
):
"""
Splits a command line into individual components
This is a simple wrapper around :func:`shlex.split`, which has the
requirement of having text (not bytes) as its argument on Python 3,
but bytes on Python 2.
:param cmd: text (a multi byte string) encoded as 'utf-8'
"""
if
sys
.
version_info
[
0
]
<
3
:
data
=
cmd
.
encode
(
'utf-8'
)
result
=
shlex
.
split
(
data
)
result
=
[
i
.
decode
(
'utf-8'
)
for
i
in
result
]
else
:
data
=
astring
.
to_text
(
cmd
,
'utf-8'
)
result
=
shlex
.
split
(
data
)
return
result
class
CmdResult
(
object
):
"""
...
...
selftests/unit/test_utils_process.py
浏览文件 @
b9a719cb
import
io
import
logging
import
os
import
shlex
import
unittest
try
:
...
...
@@ -9,6 +10,7 @@ except ImportError:
import
mock
from
avocado.utils
import
astring
from
avocado.utils
import
gdb
from
avocado.utils
import
process
from
avocado.utils
import
path
...
...
@@ -285,6 +287,32 @@ class MiscProcessTests(unittest.TestCase):
res
=
process
.
binary_from_shell_cmd
(
"FOO=bar ./bin var=value"
)
self
.
assertEqual
(
"./bin"
,
res
)
def
test_cmd_split
(
self
):
plain_str
=
''
unicode_str
=
u
''
empty_bytes
=
b
''
# shlex.split() can work with "plain_str" and "unicode_str" on both
# Python 2 and Python 3. While we're not testing Python itself,
# this will help us catch possible differences in the Python
# standard library should they arise.
self
.
assertEqual
(
shlex
.
split
(
plain_str
),
[])
self
.
assertEqual
(
shlex
.
split
(
astring
.
to_text
(
plain_str
)),
[])
self
.
assertEqual
(
shlex
.
split
(
unicode_str
),
[])
self
.
assertEqual
(
shlex
.
split
(
astring
.
to_text
(
unicode_str
)),
[])
# on Python 3, shlex.split() won't work with bytes, raising:
# AttributeError: 'bytes' object has no attribute 'read'.
# To turn bytes into text (when necessary), that is, on
# Python 3 only, use astring.to_text()
self
.
assertEqual
(
shlex
.
split
(
astring
.
to_text
(
empty_bytes
)),
[])
# Now let's test our specific implementation to split commands
self
.
assertEqual
(
process
.
cmd_split
(
plain_str
),
[])
self
.
assertEqual
(
process
.
cmd_split
(
unicode_str
),
[])
self
.
assertEqual
(
process
.
cmd_split
(
empty_bytes
),
[])
unicode_command
=
u
"avok
\xe1
do_test_runner arguments"
self
.
assertEqual
(
process
.
cmd_split
(
unicode_command
),
[
u
"avok
\xe1
do_test_runner"
,
u
"arguments"
])
class
CmdResultTests
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录