-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsender.py
82 lines (67 loc) · 2.45 KB
/
sender.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
import io
import json
import base64
from typing import IO
from functools import lru_cache
import httpx
from git import Repo
class Sender(object):
def __init__(self, path: str, url: str = None):
super(Sender, self).__init__()
self.repo = Repo(path)
self.url = url or 'http://localhost:5001/api/v0'
self.client = httpx.Client(base_url=self.url)
def _add(self, data: IO) -> str:
file = {'file': data}
response = self.client.post('/add', files=file)
response = response.json()
return {'/': response['Hash']}
def _put(self, data: dict) -> str:
data = json.dumps(data).encode()
data = io.BytesIO(data)
file = {'file': data}
response = self.client.post('/dag/put', files=file)
response = response.json()
return response['Cid']
@lru_cache(maxsize=None)
def _send_blob(self, blob: object) -> dict:
data = blob.data_stream.read()
data = io.BytesIO(data)
return self._add(data)
def _send_tree(self, tree: object) -> dict:
if tree.type == 'blob':
return self._send_blob(tree)
data = {b.name: self._send_blob(b) for b in tree.blobs}
return self._put(data)
def _send_commit(self, commit: object) -> dict:
parents = ((p.hexsha, p) for p in commit.parents)
parents = {s: self._send_commit(p) for s, p in parents}
tree = {t.name: self._send_tree(t) for t in commit.tree}
node = {
'author': {
'date': commit.authored_datetime.isoformat(),
'email': commit.author.email,
'name': commit.author.name,
},
'committer': {
'date': commit.committed_datetime.isoformat(),
'email': commit.committer.email,
'name': commit.committer.name,
},
'tree': tree,
'message': commit.message,
'parents': parents,
}
return self._put(node)
def _send_branch(self, branch: object) -> dict:
commits = self.repo.iter_commits(branch.name)
commits = {c.hexsha: self._send_commit(c) for c in commits}
return self._put(commits)
def send(self):
branches = {b.name: self._send_branch(b) for b in self.repo.branches}
branches = {'blobs': branches}
return self._put(branches)
sender = Sender('./')
sent = sender.send()
from pprint import pprint as pp
pp(sent)