File: util.py

package info (click to toggle)
synce-sync-engine 0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 996 kB
  • ctags: 923
  • sloc: python: 8,586; xml: 949; makefile: 51; sh: 7
file content (157 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
############################################################################
#    Copyright (C) 2006  Ole André Vadla Ravnås <oleavr@gmail.com>       #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import random
import os

def generate_id():
    w1 = random.randint(0x0, 0x0FFF)
    w2 = random.randint(0x0, 0xFFFF)
    return (w1 << 16) | w2

def generate_guid():
    d1 = random.randint(0, 0xFFFFFFFF)
    d2 = random.randint(0, 0xFFFF)
    d3 = random.randint(0, 0xFFFF)
    d4 = []
    for i in range(8):
        d4.append(random.randint(0, 0xFF))

    guid = u"{%08X-%04X-%04X-" % (d1, d2, d3)
    for i in xrange(len(d4)):
        guid += u"%02X" % d4[i]
        if i == 1:
            guid += u"-"
    guid += u"}"

    return guid

#def generate_opensync_guid():
#    return "pas-id-%08X%08X" % (random.randint(0, 0xFFFFFFFF),
#                                random.randint(0, 0xFFFFFFFF))

def hexdump(raw):
    buf = ""
    line = ""

    start = 0
    done = False
    while not done:
        buf += "%04x: " % start

        end = start + 16
        max = len(raw)
        if end > max:
            end = max
            done = True

        chunk = raw[start:end]
        for i in xrange(len(chunk)):
            if i > 0:
                spacing = " "
            else:
                spacing = ""
            buf += "%s%02x" % (spacing, ord(chunk[i]))

        if done:
            for i in xrange(16 - (end % 16)):
                buf += "   "

        buf += "  "

        for c in chunk:
            val = ord(c)
            if val >= 33 and val <= 126:
                buf += c
            else:
                buf += "."

        buf += "\n"

        start += 16

    return buf

def escape_str(s):
    ret = u""
    for c in s:
        if c == u"\r":
            continue
        elif c == u"\n":
            ret += u"\\n"
            continue
        elif c == u"\\":
            ret += u"\\\\"
            continue
        elif c in (u";", u","):
            ret += u"\\"
        ret += c
    return ret

def encode_wstr(s):
    return s.encode("utf_16_le") + "\x00\x00"

def decode_wstr(s):
    return s.decode("utf_16_le").rstrip("\0")

#
# 'deltree' - recursively delete a non-empty directory
# Why, oh why , is this never provided as an API by the OS?
#

def deltree(folder):

    # belts and braces..
    # We really don't want to risk nuking our root or home dirs.

    if folder == '/' or folder == "~" or folder==os.path.expanduser("~"):
        return

    # walk the path and trash the contents

    if os.path.isdir(folder):
        for path,dirs,files in os.walk(folder,topdown=False):
            for p in files:

                # Errors will most likely be permissions-related
                # so we just ignore them. Deal with files first

                try:
                    os.remove(os.path.join(path,p))
                except:
                    pass

            for p in dirs:

                # Same applies here for errors -  just ignore them,
                # the result will leave the dir in place.

                try:
                    os.rmdir(os.path.join(path,p))
                except:
                    pass

    # Dump the actual folder.

    try:
        os.rmdir(folder)
    except:
        pass