0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
3
|
16 |
import os
|
|
17 |
import shutil
|
0
|
18 |
|
|
19 |
def hex_to_bindata(hexdata):
|
|
20 |
hexdata = hexdata.replace(' ', '').replace('\r', '').replace('\n', '').replace('\t', '')
|
|
21 |
if len(hexdata) % 2 != 0:
|
|
22 |
raise ValueError("'%s' is not divisible by 2", hexdata)
|
|
23 |
for c in hexdata:
|
|
24 |
if c not in "0123456789abcdefABCDEF":
|
|
25 |
raise ValueError("'%s' is not a valid hex string", hexdata)
|
|
26 |
|
|
27 |
temp = []
|
|
28 |
for i in xrange(len(hexdata) / 2):
|
|
29 |
start = i * 2
|
|
30 |
end = start + 2
|
|
31 |
temp.append(chr(int(hexdata[start:end], 16)))
|
3
|
32 |
return ''.join(temp)
|
|
33 |
|
|
34 |
def remove_if_exists(path_or_paths):
|
|
35 |
"""Remove files or directories if they exist.
|
|
36 |
@param path_or_paths: The path to remove. Can also be a list of paths."""
|
|
37 |
if isinstance(path_or_paths, list):
|
|
38 |
paths = path_or_paths
|
|
39 |
else:
|
|
40 |
paths = [path_or_paths]
|
|
41 |
|
|
42 |
for path in paths:
|
|
43 |
if os.path.isdir(path):
|
|
44 |
shutil.rmtree(path)
|
|
45 |
elif os.path.isfile(path):
|
|
46 |
os.remove(path) |