Skip to content

Commit ea0a291

Browse files
authored
Merge pull request #199 from AnswerDotAI/fix-deps
rm `GitPython` dep
2 parents fbad99e + 417fd51 commit ea0a291

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

00_core.ipynb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"from datetime import datetime, timedelta, timezone\n",
3737
"from pprint import pprint\n",
3838
"from time import sleep\n",
39-
"import os, shutil, tempfile, git"
39+
"import os, shutil, tempfile, subprocess"
4040
]
4141
},
4242
{
@@ -1056,27 +1056,28 @@
10561056
"source": [
10571057
"#|export\n",
10581058
"img_md_pat = re.compile(r'!\\[(?P<alt>.*?)\\]\\((?P<url>[^\\s]+)\\)')\n",
1059-
"def replace_md_imgs(content, func): return img_md_pat.sub(func, content)\n",
1059+
"\n",
1060+
"def _run_subp(cmd): \n",
1061+
" r = subprocess.run(cmd, check=False, capture_output=True, text=True)\n",
1062+
" if r.returncode != 0: raise RuntimeError(r.stderr)\n",
10601063
"\n",
10611064
"@patch\n",
10621065
"def create_gist(self:GhApi, description, content, filename='gist.txt', public=False, img_paths=None):\n",
10631066
" 'Create a gist, optionally with images where each md img url will be placed with img upload urls.'\n",
10641067
" gist = self.gists.create(description, public=public, files={filename: {\"content\": content}})\n",
10651068
" if not img_paths: return gist\n",
10661069
" with tempfile.TemporaryDirectory() as clone_dir:\n",
1067-
" gist_id, gist_url = gist.id, gist.html_url\n",
10681070
" token = self.headers['Authorization'].split('token ')[1]\n",
1069-
" repo = git.Repo.clone_from(f'https://{token}@gist.github.com/{gist_id}.git', clone_dir)\n",
1071+
" _run_subp(['git', 'clone', f'https://{token}@gist.github.com/{gist.id}.git', clone_dir])\n",
10701072
" clone_dir, img_paths = Path(clone_dir), L(img_paths).map(Path)\n",
1071-
" for o in img_paths: \n",
1072-
" shutil.copy2(o, clone_dir/o.name)\n",
1073-
" repo.index.add([o.name])\n",
1074-
" repo.index.commit(\"Add images\")\n",
1075-
" repo.remotes.origin.push() \n",
1076-
" updated_gist = self.gists.get(gist_id)\n",
1073+
" for o in img_paths: shutil.copy2(o, clone_dir/o.name)\n",
1074+
" _run_subp(['git', '-C', clone_dir, 'add', '.'])\n",
1075+
" _run_subp(['git', '-C', clone_dir, 'commit', '-m', 'Add images'])\n",
1076+
" _run_subp(['git', '-C', clone_dir, 'push']) \n",
1077+
" updated_gist = self.gists.get(gist.id)\n",
10771078
" img_urls = {o.name: updated_gist.files[o.name].raw_url for o in img_paths}\n",
1078-
" content = replace_md_imgs(content, lambda m: f\"![{m['alt']}]({img_urls.get(m['url'], m['url'])})\")\n",
1079-
" return self.gists.update(gist_id, files={filename:{'content':content}})"
1079+
" content = img_md_pat.sub(lambda m: f\"![{m['alt']}]({img_urls.get(m['url'], m['url'])})\", content)\n",
1080+
" return self.gists.update(gist.id, files={filename:{'content':content}})"
10801081
]
10811082
},
10821083
{
@@ -1088,7 +1089,7 @@
10881089
"name": "stdout",
10891090
"output_type": "stream",
10901091
"text": [
1091-
"https://gist.github.com/jph00/634c4553fac5fdd3b2d810dcdae2d841\n"
1092+
"https://gist.github.com/KeremTurgutlu/c2150e4efd1177439ff35a4633584144\n"
10921093
]
10931094
}
10941095
],
@@ -1126,7 +1127,7 @@
11261127
"name": "stdout",
11271128
"output_type": "stream",
11281129
"text": [
1129-
"https://gist.github.com/jph00/dbd28d7ef14117b4aa4b0d8fc9a9aa81\n"
1130+
"https://gist.github.com/KeremTurgutlu/4c60f8b06baf7b57dc225dd86b65028f\n"
11301131
]
11311132
}
11321133
],
@@ -1143,7 +1144,7 @@
11431144
{
11441145
"data": {
11451146
"text/plain": [
1146-
"'some image\\n\\n![image](https://gist.githubusercontent.com/jph00/dbd28d7ef14117b4aa4b0d8fc9a9aa81/raw/c7f420c839f58c6ac0c05f1116317645d31d7e80/puppy.jpg)'"
1147+
"'some image\\n\\n![image](https://gist.githubusercontent.com/KeremTurgutlu/4c60f8b06baf7b57dc225dd86b65028f/raw/c7f420c839f58c6ac0c05f1116317645d31d7e80/puppy.jpg)'"
11471148
]
11481149
},
11491150
"execution_count": null,
@@ -1806,9 +1807,9 @@
18061807
],
18071808
"metadata": {
18081809
"kernelspec": {
1809-
"display_name": "python3",
1810+
"display_name": "python",
18101811
"language": "python",
1811-
"name": "python3"
1812+
"name": "python"
18121813
}
18131814
},
18141815
"nbformat": 4,

ghapi/_modidx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
'ghapi.core._mk_sig': ('core.html#_mk_sig', 'ghapi/core.py'),
8787
'ghapi.core._mk_sig_detls': ('core.html#_mk_sig_detls', 'ghapi/core.py'),
8888
'ghapi.core._preview_hdr': ('core.html#_preview_hdr', 'ghapi/core.py'),
89+
'ghapi.core._run_subp': ('core.html#_run_subp', 'ghapi/core.py'),
8990
'ghapi.core.date2gh': ('core.html#date2gh', 'ghapi/core.py'),
9091
'ghapi.core.gh2date': ('core.html#gh2date', 'ghapi/core.py'),
91-
'ghapi.core.print_summary': ('core.html#print_summary', 'ghapi/core.py'),
92-
'ghapi.core.replace_md_imgs': ('core.html#replace_md_imgs', 'ghapi/core.py')},
92+
'ghapi.core.print_summary': ('core.html#print_summary', 'ghapi/core.py')},
9393
'ghapi.event': { 'ghapi.event.GhApi.fetch_events': ('event.html#ghapi.fetch_events', 'ghapi/event.py'),
9494
'ghapi.event.GhApi.list_events': ('event.html#ghapi.list_events', 'ghapi/event.py'),
9595
'ghapi.event.GhApi.list_events_parallel': ('event.html#ghapi.list_events_parallel', 'ghapi/event.py'),

ghapi/core.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_core.ipynb.
44

55
# %% auto 0
6-
__all__ = ['GH_HOST', 'img_md_pat', 'EMPTY_TREE_SHA', 'print_summary', 'GhApi', 'date2gh', 'gh2date', 'replace_md_imgs']
6+
__all__ = ['GH_HOST', 'img_md_pat', 'EMPTY_TREE_SHA', 'print_summary', 'GhApi', 'date2gh', 'gh2date']
77

88
# %% ../00_core.ipynb 2
99
from fastcore.all import *
@@ -17,7 +17,7 @@
1717
from datetime import datetime, timedelta, timezone
1818
from pprint import pprint
1919
from time import sleep
20-
import os, shutil, tempfile, git
20+
import os, shutil, tempfile, subprocess
2121

2222
# %% ../00_core.ipynb 4
2323
GH_HOST = os.getenv('GH_HOST', "https://api.github.com")
@@ -164,27 +164,28 @@ def gh2date(dtstr:str)->datetime:
164164

165165
# %% ../00_core.ipynb 69
166166
img_md_pat = re.compile(r'!\[(?P<alt>.*?)\]\((?P<url>[^\s]+)\)')
167-
def replace_md_imgs(content, func): return img_md_pat.sub(func, content)
167+
168+
def _run_subp(cmd):
169+
r = subprocess.run(cmd, check=False, capture_output=True, text=True)
170+
if r.returncode != 0: raise RuntimeError(r.stderr)
168171

169172
@patch
170173
def create_gist(self:GhApi, description, content, filename='gist.txt', public=False, img_paths=None):
171174
'Create a gist, optionally with images where each md img url will be placed with img upload urls.'
172175
gist = self.gists.create(description, public=public, files={filename: {"content": content}})
173176
if not img_paths: return gist
174177
with tempfile.TemporaryDirectory() as clone_dir:
175-
gist_id, gist_url = gist.id, gist.html_url
176178
token = self.headers['Authorization'].split('token ')[1]
177-
repo = git.Repo.clone_from(f'https://{token}@gist.github.com/{gist_id}.git', clone_dir)
179+
_run_subp(['git', 'clone', f'https://{token}@gist.github.com/{gist.id}.git', clone_dir])
178180
clone_dir, img_paths = Path(clone_dir), L(img_paths).map(Path)
179-
for o in img_paths:
180-
shutil.copy2(o, clone_dir/o.name)
181-
repo.index.add([o.name])
182-
repo.index.commit("Add images")
183-
repo.remotes.origin.push()
184-
updated_gist = self.gists.get(gist_id)
181+
for o in img_paths: shutil.copy2(o, clone_dir/o.name)
182+
_run_subp(['git', '-C', clone_dir, 'add', '.'])
183+
_run_subp(['git', '-C', clone_dir, 'commit', '-m', 'Add images'])
184+
_run_subp(['git', '-C', clone_dir, 'push'])
185+
updated_gist = self.gists.get(gist.id)
185186
img_urls = {o.name: updated_gist.files[o.name].raw_url for o in img_paths}
186-
content = replace_md_imgs(content, lambda m: f"![{m['alt']}]({img_urls.get(m['url'], m['url'])})")
187-
return self.gists.update(gist_id, files={filename:{'content':content}})
187+
content = img_md_pat.sub(lambda m: f"![{m['alt']}]({img_urls.get(m['url'], m['url'])})", content)
188+
return self.gists.update(gist.id, files={filename:{'content':content}})
188189

189190
# %% ../00_core.ipynb 76
190191
@patch

0 commit comments

Comments
 (0)