-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3git.py
165 lines (128 loc) · 5.93 KB
/
s3git.py
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
158
159
160
161
162
163
164
165
#
# Copyright 2016 Frank Wessels <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from io import BytesIO
import json
import shutil
import tempfile
from cffi import FFI
from ctypes import *
ffi = FFI()
ffi.cdef("""
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
extern GoInt s3git_init_repository(char* p0);
extern GoInt s3git_open_repository(char* p0);
extern GoInt s3git_clone(char* p0, char* p1, char* p2, char* p3);
extern char* s3git_add(char* p0, char* p1);
extern GoInt s3git_commit(char* p0, char* p1);
extern char* s3git_get(char* p0, char* p1);
extern GoInt s3git_push(char* p0, GoUint8 p1);
extern GoInt s3git_pull(char* p0);
extern char* s3git_list(char* p0, char* p1);
extern char* s3git_list_commits(char* p0);
extern GoInt s3git_remote_add(char* p0, char* p1, char* p2, char* p3, char* p4, char* p5);
extern GoInt s3git_snapshot_create(char* p0, char* p1);
extern GoInt s3git_snapshot_checkout(char* p0, char* p1, GoUint8 p2);
extern GoInt s3git_snapshot_list(char* p0, char* p1);
extern GoInt s3git_snapshot_status(char* p0, char* p1);
""")
__s3gitlib__ = ffi.dlopen("s3git-py.so")
class Repository(object):
path = ""
def __init__(self, path):
self.path = path
def add(self, contents):
stream = contents
if isinstance(contents, str): # Convert to stream for strings
stream = BytesIO(contents.encode('utf-8'))
with tempfile.NamedTemporaryFile() as temp:
shutil.copyfileobj (stream, temp)
temp.flush()
ret = __s3gitlib__.s3git_add(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", temp.name.encode('utf-8')))
return ffi.string(ret)
def commit(self, message):
ret = __s3gitlib__.s3git_commit(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", message.encode('utf-8')))
return ret
def get(self, hash):
ret = __s3gitlib__.s3git_get(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", hash.encode('utf-8')))
return open(ffi.string(ret), "rb")
def push(self, hydrate=False):
arg_hydrate = 1 if hydrate else 0
ret = __s3gitlib__.s3git_push(ffi.new("char[]", self.path.encode('utf-8')),
arg_hydrate)
return ret
def pull(self):
ret = __s3gitlib__.s3git_pull(ffi.new("char[]", self.path.encode('utf-8')))
return ret
def list(self, hash):
ret = __s3gitlib__.s3git_list(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", hash.encode('utf-8')))
return ffi.string(ret).decode('utf-8').split(',')
def list_commits(self):
json_str = __s3gitlib__.s3git_list_commits(ffi.new("char[]", self.path.encode('utf-8')))
return json.loads(ffi.string(json_str).decode('utf-8'))
def remote_add(self, name, resource, access="", secret="", endpoint=""):
arg_path = ffi.new("char[]", self.path.encode('utf-8'))
arg_name = ffi.new("char[]", name.encode('utf-8'))
arg_resource = ffi.new("char[]", resource.encode('utf-8'))
arg_access = ffi.new("char[]", access.encode('utf-8'))
arg_secret = ffi.new("char[]", secret.encode('utf-8'))
arg_endpoint = ffi.new("char[]", endpoint.encode('utf-8'))
ret = __s3gitlib__.s3git_remote_add(arg_path, arg_name, arg_resource, arg_access, arg_secret, arg_endpoint)
return ret
def snapshot_create(self, message):
ret = __s3gitlib__.s3git_snapshot_create(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", message.encode('utf-8')))
return ret
def snapshot_checkout(self, commit, dedupe=False):
arg_dedupe = 1 if dedupe else 0
ret = __s3gitlib__.s3git_snapshot_checkout(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", commit.encode('utf-8')), arg_dedupe)
return ret
def snapshot_list(self, commit):
ret = __s3gitlib__.s3git_snapshot_list(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", commit.encode('utf-8')))
return ret
def snapshot_status(self, commit):
ret = __s3gitlib__.s3git_snapshot_status(ffi.new("char[]", self.path.encode('utf-8')),
ffi.new("char[]", commit.encode('utf-8')))
return ret
def init_repository(path):
repo = Repository(path)
__s3gitlib__.s3git_init_repository(ffi.new("char[]", repo.path.encode('utf-8')))
return repo
def open_repository(path):
repo = Repository(path)
__s3gitlib__.s3git_open_repository(ffi.new("char[]", repo.path.encode('utf-8')))
return repo
def clone(url, path, access="", secret=""):
repo = Repository(path)
arg_url = ffi.new("char[]", url.encode('utf-8'))
arg_path = ffi.new("char[]", repo.path.encode('utf-8'))
arg_access = ffi.new("char[]", access.encode('utf-8'))
arg_secret = ffi.new("char[]", secret.encode('utf-8'))
__s3gitlib__.s3git_clone(arg_url, arg_path, arg_access, arg_secret)
return repo