This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (46 loc) · 1.68 KB
/
utils.py
File metadata and controls
59 lines (46 loc) · 1.68 KB
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
import itertools
import os
import re
import subprocess
import tempfile
from typing import List, Tuple
import requests
def flatten(l):
"""Flatten list of lists.
Args:
l: A list of lists
Returns: A flattened iterable
"""
return itertools.chain.from_iterable(l)
def chunks(l: List, n: int):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def remap_nwo(nwo: str) -> Tuple[str, str]:
r = requests.get('https://github.com/{}'.format(nwo))
if r.status_code not in (404, 451, 502): # DMCA
if 'migrated' not in r.text:
if r.history:
return (nwo, '/'.join(re.findall(r'"https://github.com/.+"', r.history[0].text)[0].strip('"').split('/')[-2:]))
return (nwo, nwo)
return (nwo, None)
def get_sha(tmp_dir: tempfile.TemporaryDirectory, nwo: str):
os.chdir(os.path.join(tmp_dir.name, nwo))
# git rev-parse HEAD
cmd = ['git', 'rev-parse', 'HEAD']
sha = subprocess.check_output(cmd).strip().decode('utf-8')
os.chdir('/tmp')
return sha
def download(nwo: str):
os.environ['GIT_TERMINAL_PROMPT'] = '0'
tmp_dir = tempfile.TemporaryDirectory()
cmd = ['git', 'clone', '--depth=1', 'https://github.com/{}.git'.format(nwo), '{}/{}'.format(tmp_dir.name, nwo)]
subprocess.run(cmd, stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
return tmp_dir
def walk(tmp_dir: tempfile.TemporaryDirectory, ext: str):
results = []
for root, _, files in os.walk(tmp_dir.name):
for f in files:
if f.endswith('.' + ext):
results.append(os.path.join(root, f))
return results