Mbed Host Tests
module_copy_shell.py
Go to the documentation of this file.
1"""
2mbed SDK
3Copyright (c) 2011-2015 ARM Limited
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
18"""
19
20import os
21from os.path import join, basename
22from .host_test_plugins import HostTestPluginBase
23
24
25class HostTestPluginCopyMethod_Shell(HostTestPluginBase):
26 # Plugin interface
27 name = 'HostTestPluginCopyMethod_Shell'
28 type = 'CopyMethod'
29 stable = True
30 capabilities = ['shell', 'cp', 'copy', 'xcopy']
31 required_parameters = ['image_path', 'destination_disk']
32
33 def __init__(self):
34 """ ctor
35 """
36 HostTestPluginBase.__init__(self)
37
38 def setup(self, *args, **kwargs):
39 """ Configure plugin, this function should be called before plugin execute() method is used.
40 """
41 return True
42
43 def execute(self, capability, *args, **kwargs):
44 """! Executes capability by name
45
46 @param capability Capability name
47 @param args Additional arguments
48 @param kwargs Additional arguments
49 @details Each capability e.g. may directly just call some command line program or execute building pythonic function
50 @return Capability call return value
51 """
52 if not kwargs['image_path']:
53 self.print_plugin_error("Error: image path not specified")
54 return False
55
56 if not kwargs['destination_disk']:
57 self.print_plugin_error("Error: destination disk not specified")
58 return False
59
60 # This optional parameter can be used if TargetID is provided (-t switch)
61 target_id = kwargs.get('target_id', None)
62 pooling_timeout = kwargs.get('polling_timeout', 60)
63
64 result = False
65 if self.check_parameters(capability, *args, **kwargs):
66 if kwargs['image_path'] and kwargs['destination_disk']:
67 image_path = os.path.normpath(kwargs['image_path'])
68 destination_disk = os.path.normpath(kwargs['destination_disk'])
69 # Wait for mount point to be ready
70 # if mount point changed according to target_id use new mount point
71 # available in result (_, destination_disk) of check_mount_point_ready
72 mount_res, destination_disk = self.check_mount_point_ready(destination_disk, target_id=target_id, timeout=pooling_timeout) # Blocking
73 if not mount_res:
74 return result # mount point is not ready return
75 # Prepare correct command line parameter values
76 image_base_name = basename(image_path)
77 destination_path = join(destination_disk, image_base_name)
78 if capability == 'shell':
79 if os.name == 'nt': capability = 'copy'
80 elif os.name == 'posix': capability = 'cp'
81 if capability == 'cp' or capability == 'copy' or capability == 'copy':
82 copy_method = capability
83 cmd = [copy_method, image_path, destination_path]
84 if os.name == 'posix':
85 result = self.run_command(cmd, shell=False)
86 if os.uname()[0] == 'Linux':
87 result = result and self.run_command(["sync", "-f", destination_path])
88 else:
89 result = result and self.run_command(["sync"])
90 else:
91 result = self.run_command(cmd)
92 return result
93
94
96 """ Returns plugin available in this module
97 """
def run_command(self, cmd, shell=True)
Runs command from command line.
def check_mount_point_ready(self, destination_disk, init_delay=0.2, loop_delay=0.25, target_id=None, timeout=60)
Waits until destination_disk is ready and can be accessed by e.g.
def print_plugin_error(self, text)
Interface helper methods - overload only if you need to have custom behaviour.
def check_parameters(self, capability, *args, **kwargs)
This function should be ran each time we call execute() to check if none of the required parameters i...
def execute(self, capability, *args, **kwargs)
Executes capability by name.