|
9 | 9 | import tempfile |
10 | 10 |
|
11 | 11 | try: |
12 | | - import hashlib |
| 12 | + import hashlib |
13 | 13 | except ImportError: |
14 | | - import sha |
| 14 | + import sha |
15 | 15 |
|
16 | 16 | def make_sha(source=''): |
17 | | - """ |
18 | | - A python2.4 workaround for the sha/hashlib module fiasco |
19 | | - |
| 17 | + """ |
| 18 | + A python2.4 workaround for the sha/hashlib module fiasco |
| 19 | + |
20 | 20 | Note |
21 | 21 | From the dulwich project |
22 | 22 | """ |
23 | | - try: |
24 | | - return hashlib.sha1(source) |
25 | | - except NameError: |
26 | | - sha1 = sha.sha(source) |
27 | | - return sha1 |
| 23 | + try: |
| 24 | + return hashlib.sha1(source) |
| 25 | + except NameError: |
| 26 | + sha1 = sha.sha(source) |
| 27 | + return sha1 |
| 28 | + |
| 29 | +def join_path(a, *p): |
| 30 | + """Join path tokens together similar to os.path.join, but always use |
| 31 | + '/' instead of possibly '\' on windows.""" |
| 32 | + path = a |
| 33 | + for b in p: |
| 34 | + if b.startswith('/'): |
| 35 | + path += b[1:] |
| 36 | + elif path == '' or path.endswith('/'): |
| 37 | + path += b |
| 38 | + else: |
| 39 | + path += '/' + b |
| 40 | + return path |
| 41 | + |
| 42 | +def to_native_path_windows(path): |
| 43 | + return path.replace('/','\\') |
| 44 | + |
| 45 | +def to_native_path_linux(path): |
| 46 | + return path.replace('\\','/') |
| 47 | + |
| 48 | +if sys.platform.startswith('win'): |
| 49 | + to_native_path = to_native_path_windows |
| 50 | +else: |
| 51 | + # no need for any work on linux |
| 52 | + def to_native_path_linux(path): |
| 53 | + return path |
| 54 | + to_native_path = to_native_path_linux |
| 55 | + |
| 56 | +def join_path_native(a, *p): |
| 57 | + """As join path, but makes sure an OS native path is returned. This is only |
| 58 | + needed to play it safe on my dear windows and to assure nice paths that only |
| 59 | + use '\'""" |
| 60 | + return to_native_path(join_path(a, *p)) |
28 | 61 |
|
29 | 62 |
|
30 | 63 | class SHA1Writer(object): |
31 | | - """ |
32 | | - Wrapper around a file-like object that remembers the SHA1 of |
33 | | - the data written to it. It will write a sha when the stream is closed |
34 | | - or if the asked for explicitly usign write_sha. |
35 | | - |
36 | | - Note: |
37 | | - Based on the dulwich project |
38 | | - """ |
39 | | - __slots__ = ("f", "sha1") |
40 | | - |
41 | | - def __init__(self, f): |
42 | | - self.f = f |
43 | | - self.sha1 = make_sha("") |
| 64 | + """ |
| 65 | + Wrapper around a file-like object that remembers the SHA1 of |
| 66 | + the data written to it. It will write a sha when the stream is closed |
| 67 | + or if the asked for explicitly usign write_sha. |
| 68 | + |
| 69 | + Note: |
| 70 | + Based on the dulwich project |
| 71 | + """ |
| 72 | + __slots__ = ("f", "sha1") |
| 73 | + |
| 74 | + def __init__(self, f): |
| 75 | + self.f = f |
| 76 | + self.sha1 = make_sha("") |
44 | 77 |
|
45 | | - def write(self, data): |
46 | | - self.sha1.update(data) |
47 | | - self.f.write(data) |
| 78 | + def write(self, data): |
| 79 | + self.sha1.update(data) |
| 80 | + self.f.write(data) |
48 | 81 |
|
49 | | - def write_sha(self): |
50 | | - sha = self.sha1.digest() |
51 | | - self.f.write(sha) |
52 | | - return sha |
| 82 | + def write_sha(self): |
| 83 | + sha = self.sha1.digest() |
| 84 | + self.f.write(sha) |
| 85 | + return sha |
53 | 86 |
|
54 | | - def close(self): |
55 | | - sha = self.write_sha() |
56 | | - self.f.close() |
57 | | - return sha |
| 87 | + def close(self): |
| 88 | + sha = self.write_sha() |
| 89 | + self.f.close() |
| 90 | + return sha |
58 | 91 |
|
59 | | - def tell(self): |
60 | | - return self.f.tell() |
| 92 | + def tell(self): |
| 93 | + return self.f.tell() |
61 | 94 |
|
62 | 95 |
|
63 | 96 | class LockFile(object): |
|
0 commit comments