-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpysh.py
84 lines (63 loc) · 2.15 KB
/
getpysh.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
83
84
#!/usr/bin/env python3
# coding: utf-8
# This script is a convenience installer to be used in the Pythonista3 iOS app
from functools import partial
import os
import requests
import shutil
URL = "https://github.com/dani-jozsef/pysh/archive/refs/heads/main.zip"
DOWNLOADTO = "Documents/pysh-main.zip"
TEMPDIR = "Documents/_tmp_pysh_main"
PACKAGEDIR = "pysh-main/pysh"
ADDITIONAL_FILES = [ "pysh-main/LICENSE" ]
INSTALLDIR = "Documents/site-packages/pysh"
##
def postinst(install_path):
pass
##
def _httpget_internal(url, dst):
with requests.get(url, stream=True) as r:
r.raw.read = partial(r.raw.read, decode_content=True)
with open(dst, 'wb') as f:
shutil.copyfileobj(r.raw, f)
def _alreadyexists(path):
return FileExistsError(f"File or directory already exists: '{path}")
def install(
url = URL,
downloadto = DOWNLOADTO,
tempdir = TEMPDIR,
packagedir = PACKAGEDIR,
additional_files = ADDITIONAL_FILES,
installdir = INSTALLDIR):
homedir = os.environ['HOME']
download_path = os.path.join(homedir, downloadto)
expand_path = os.path.join(homedir, tempdir)
package_path = os.path.join(expand_path, packagedir)
install_path = os.path.join(homedir, installdir)
if os.path.exists(download_path):
raise _alreadyexists(download_path)
if os.path.exists(expand_path):
raise _alreadyexists(expand_path)
if os.path.exists(install_path):
raise _alreadyexists(install_path)
print(f"Attempting to download '{url}' to '{download_path}'")
_httpget_internal(url, download_path)
print(" done..")
print(f"Attempting to unpack to '{expand_path}'")
shutil.unpack_archive(download_path, expand_path)
print(" done..")
print(f"Attempting to install to '{install_path}'")
shutil.move(package_path, install_path)
for file in additional_files:
file_path = os.path.join(expand_path, file)
shutil.move(file_path, install_path)
print(" done!")
print(f"Attempting to remove '{download_path}' and '{expand_path}'")
os.remove(download_path)
shutil.rmtree(expand_path)
print(" done..")
print("Running post-install script..")
postinst(install_path)
print("Bye! <3")
if __name__ == "__main__":
install()