Klaus Demo klaus / a17be39
Merge pull request #229 from jelmer/tarball-names Try a bit harder to come up with sensible names for tarballs and prefixes in tarballs. Jonas Haag authored 1 year, 25 days ago GitHub committed 1 year, 25 days ago
2 changed file(s) with 45 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
00 # encoding: utf-8
1 import binascii
12 import os
23 import re
34 import time
168169 return email
169170
170171
172 def is_hex_prefix(s):
173 if len(s) % 2:
174 s += '0'
175 try:
176 binascii.unhexlify(s)
177 return True
178 except binascii.Error:
179 return False
180
181
171182 def shorten_sha1(sha1):
172 if re.match(r'[a-z\d]{20,40}', sha1):
183 if 20 <= len(sha1) <= 40 and is_hex_prefix(sha1):
173184 sha1 = sha1[:7]
174185 return sha1
175186
208219 ls[i] = replacement
209220 else:
210221 last = elem
211
212
213222
214223
215224 def guess_git_revision():
241250 def escape_html(s):
242251 return s.replace(b'&', b'&amp;').replace(b'<', b'&lt;') \
243252 .replace(b'>', b'&gt;').replace(b'"', b'&quot;')
253
254
255 def tarball_basename(repo_name, rev):
256 """Determine the name for a tarball."""
257 rev = sanitize_branch_name(rev, chars='/')
258 if rev.startswith(repo_name + '-'):
259 # If the rev is a tag name that already starts with the repo name,
260 # skip it.
261 return rev
262 elif len(rev) >= 2 and rev[0] == 'v' and not rev[1].isalpha():
263 # If the rev is a tag name prefixed by a 'v', skip the 'v'.
264 # So, v-1.0 -> 1.0, v1.0 -> 1.0, but vanilla -> vanilla.
265 return "%s-%s" % (repo_name, rev[1:])
266 elif len(rev) == 40 and is_hex_prefix(rev):
267 # If the rev is a commit hash, simply use that.
268 return "%s@%s" % (repo_name, rev)
269 else:
270 return "%s-%s" % (repo_name, rev)
1818 with mock.patch.object(utils, 'chardet', None):
1919 self.assertRaises(
2020 UnicodeDecodeError, utils.force_unicode, b'f\xce')
21
22
23 class TarballBasenameTests(unittest.TestCase):
24
25 def test_examples(self):
26 examples = [
27 ('v0.1', 'klaus-0.1'),
28 ('klaus-0.1', 'klaus-0.1'),
29 ('0.1', 'klaus-0.1'),
30 ('b3e70e08344ca3f83cc7033ecdbefa90443d7d2e',
31 'klaus@b3e70e08344ca3f83cc7033ecdbefa90443d7d2e'),
32 ('vanilla', 'klaus-vanilla'),
33 ]
34 for (rev, basename) in examples:
35 self.assertEqual(utils.tarball_basename('klaus', rev), basename)