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