Newer
Older
# Copyright (C) 2015-2016 The Software Heritage developers

Antoine R. Dumont
committed
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
"""Convert objects to dictionaries suitable for swh.storage"""

Antoine R. Dumont
committed
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import datetime
import os
from swh.model.hashutil import hash_to_hex
from swh.model import git
def to_datetime(ts):
"""Convert a timestamp to utc datetime.
"""
return datetime.datetime.utcfromtimestamp(ts).replace(
tzinfo=datetime.timezone.utc)
def format_to_minutes(offset_str):
"""Convert a git string timezone format string (e.g +0200, -0310) to minutes.
Args:
offset_str: a string representing an offset.
Returns:
A positive or negative number of minutes of such input
"""
sign = offset_str[0]
hours = int(offset_str[1:3])
minutes = int(offset_str[3:]) + (hours * 60)
return minutes if sign == '+' else -1 * minutes
def blob_to_content(obj, log=None, max_content_size=None,
origin_id=None):
"""Convert obj to a swh storage content.
Note:
- If obj represents a link, the length and data are already
provided so we use them directly.
- 'data' is returned only if max_content_size is not reached.
Returns:
obj converted to content as a dictionary.
"""
filepath = obj['path']
if 'length' in obj: # link already has it
size = obj['length']
else:
size = os.lstat(filepath).st_size
ret = {
'sha1': obj['sha1'],
'sha256': obj['sha256'],
'sha1_git': obj['sha1_git'],
'length': size,
'perms': obj['perms'].value,
'type': obj['type'].value,
}
if max_content_size and size > max_content_size:
if log:
log.info('Skipping content %s, too large (%s > %s)' %
(hash_to_hex(obj['sha1_git']),
size,
max_content_size))
ret.update({'status': 'absent',
'reason': 'Content too large',
'origin': origin_id})
return ret
if 'data' in obj: # link already has it
data = obj['data']
else:
data = open(filepath, 'rb').read()
ret.update({
'data': data,
'status': 'visible'
})
return ret
# Map of type to swh types
_entry_type_map = {
git.GitType.TREE: 'dir',
git.GitType.BLOB: 'file',
git.GitType.COMM: 'rev',
}
def tree_to_directory(tree, objects, log=None):
"""Format a tree as a directory
"""
entries = []
for entry in objects[tree['path']]:
entries.append({
'type': _entry_type_map[entry['type']],
'perms': int(entry['perms'].value),
'name': entry['name'],
'target': entry['sha1_git']
})
return {
'id': tree['sha1_git'],
'entries': entries
}
def commit_to_revision(commit, objects, log=None):
"""Format a commit as a revision.
"""
upper_directory = objects[git.ROOT_TREE_KEY][0]
return {
'date': {
'timestamp': commit['author_date'],
'offset': format_to_minutes(commit['author_offset']),
},
'committer_date': {
'timestamp': commit['committer_date'],
'offset': format_to_minutes(commit['committer_offset']),
},
'type': commit['type'],
'directory': upper_directory['sha1_git'],
'message': commit['message'].encode('utf-8'),
'author': {
'name': commit['author_name'].encode('utf-8'),
'email': commit['author_email'].encode('utf-8'),
},
'committer': {
'name': commit['committer_name'].encode('utf-8'),
'email': commit['committer_email'].encode('utf-8'),
},
'synthetic': True,
'metadata': commit['metadata'],
'parents': [],
}
def annotated_tag_to_release(release, log=None):
"""Format a swh release.
"""
return {
'target': release['target'],
'target_type': release['target_type'],
'name': release['name'].encode('utf-8'),
'message': release['comment'].encode('utf-8'),
'date': {
'timestamp': release['date'],
'offset': format_to_minutes(release['offset']),
},
'author': {
'name': release['author_name'].encode('utf-8'),
'email': release['author_email'].encode('utf-8'),
},
'synthetic': True,
}
def ref_to_occurrence(ref):
"""Format a reference as an occurrence"""
occ = ref.copy()
if 'branch' in ref:
branch = ref['branch']
if isinstance(branch, str):
occ['branch'] = branch.encode('utf-8')
else:
occ['branch'] = branch
return occ
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def shallow_blob(obj):
"""Convert a full swh content/blob to just what's needed by
swh-storage for filtering.
Returns:
A shallow copy of a full swh content/blob object.
"""
return {
'sha1': obj['sha1'],
'sha256': obj['sha256'],
'sha1_git': obj['sha1_git'],
'length': obj['length']
}
def shallow_tree(tree):
"""Convert a full swh directory/tree to just what's needed by
swh-storage for filtering.
Returns:
A shallow copy of a full swh directory/tree object.
"""
return tree['sha1_git']
def shallow_commit(commit):
"""Convert a full swh revision/commit to just what's needed by
swh-storage for filtering.
Returns:
A shallow copy of a full swh revision/commit object.
"""
return commit['id']
def shallow_tag(tag):
"""Convert a full swh release/tag to just what's needed by
swh-storage for filtering.
Returns:
A shallow copy of a full swh release/tag object.
"""
return tag['id']