Skip to content

Instantly share code, notes, and snippets.

@bencord0
Last active September 29, 2021 11:51
Show Gist options
  • Save bencord0/9d6ddd8e5c467341c7c57de079e63003 to your computer and use it in GitHub Desktop.
Save bencord0/9d6ddd8e5c467341c7c57de079e63003 to your computer and use it in GitHub Desktop.
A script to update VSCode on linux (which has no autoupdater)
import os
import re
import requests
import tarfile
from pathlib import Path
from tqdm import tqdm
from urllib.parse import urlparse
INDEX_URL = "https://nodejs.org/dist/index.json"
FETCH_URL = 'https://nodejs.org/dist/{version}/node-{version}-linux-x64.tar.xz'
def latest_lts_version():
index = requests.get(INDEX_URL).json()
for entry in index:
if entry['lts']:
return entry['version']
raise RuntimeError("Couldn't find latest lts version")
def main():
version = latest_lts_version()
saved_tarball = Path(f'~/Software/node-{version}-linux-x64.tar.xz').expanduser()
if not saved_tarball.exists():
save_tarball(FETCH_URL.format(version=version), saved_tarball)
archive = tarfile.open(saved_tarball)
unpacked_root = Path(f'~/Software/node-{version}-linux-x64').expanduser()
root_tar_dir = f'node-{version}-linux-x64'
for member in archive:
save_member(archive, unpacked_root, member, root_tar_dir)
symlink = Path('~/Software/node-linux-x64').expanduser()
if symlink.exists():
symlink.unlink()
symlink.symlink_to(unpacked_root)
def save_tarball(url, path):
with path.open('wb') as tarball:
print(f'Downloading: {url}')
download = requests.get(url, stream=True)
content_length = int(download.headers['content-length'])
with tqdm(total=content_length) as progress:
for chunk in download.iter_content(chunk_size=4096):
progress.update(len(chunk))
tarball.write(chunk)
def save_member(archive, root, member, root_tar_dir):
path = Path(
member.name.replace(root_tar_dir, str(root))
)
print(f'{str(path)}')
if member.isdir():
if not path.exists():
path.mkdir()
elif member.isfile():
content = archive.extractfile(member)
# Set file content
buf = content.read()
path.write_bytes(buf)
# Set file attribute bits
path.chmod(member.mode)
# Set file timestamps
# Path.utime(member.mtime) does not yet exists
os.utime(path, (member.mtime, member.mtime))
elif member.issym():
path.symlink_to(member.linkname)
else:
breakpoint()
print(member.name)
if __name__ == '__main__':
main()
import os
import re
import requests
import tarfile
import secrets
import shutil
from pathlib import Path
from tqdm import tqdm
from urllib.parse import urlparse
URL = 'https://typora.io/linux/Typora-linux-x64.tar.gz'
def main():
saved_tarball = Path(f'~/Software/Typora-linux-x64.tar.gz').expanduser()
if not saved_tarball.exists():
save_tarball(URL, saved_tarball)
archive = tarfile.open(saved_tarball)
suffix = secrets.token_urlsafe(4)
unpacked_root = Path(f'~/Software/Typora-linux-x64-{suffix}').expanduser()
for member in archive:
save_member(archive, unpacked_root, member)
with (unpacked_root / 'version').open() as version_file:
version = version_file.read()
target_root = Path(f'~/Software/Typora-linux-x64-{version}').expanduser()
if target_root.exists():
print("Typora already installed, removing temporary directory"
shutil.rmtree(unpacked_root)
return
unpacked_root.rename(target_root)
symlink = Path('~/Software/Typora-linux-x64').expanduser()
print(f"Symlinking: {symlink}")
if symlink.exists():
symlink.unlink()
symlink.symlink_to(target_root)
def save_tarball(url, path):
with path.open('wb') as tarball:
print(f'Downloading: {url}')
download = requests.get(url, stream=True)
content_length = int(download.headers['content-length'])
with tqdm(total=content_length) as progress:
for chunk in download.iter_content(chunk_size=4096):
progress.update(len(chunk))
tarball.write(chunk)
def save_member(archive, root, member):
path = Path(
member.name.replace('bin/Typora-linux-x64', str(root))
)
print(f'{str(path)}')
if member.isdir():
if not path.exists():
path.mkdir()
elif member.isfile():
content = archive.extractfile(member)
# Set file content
buf = content.read()
path.write_bytes(buf)
# Set file attribute bits
path.chmod(member.mode)
# Set file timestamps
# Path.utime(member.mtime) does not yet exists
os.utime(path, (member.mtime, member.mtime))
else:
breakpoint()
print(member.name)
if __name__ == '__main__':
main()
import os
import re
import requests
import tarfile
from pathlib import Path
from tqdm import tqdm
from urllib.parse import urlparse
URL = 'https://code.visualstudio.com/sha/download?build=stable&os=linux-x64'
class LocationParser:
def __init__(self, url: str):
self.url = url
self.parsed_url = urlparse(url)
self.path = Path(self.parsed_url.path)
# 'code-stable-x64-xxxxxxxx.tar.gz
self.name = self.path.name
self.version = re.fullmatch(
'code-stable-x64-(?P<version>\d+).tar.gz',
self.name,
)['version']
def main():
redirect = requests.get(URL, allow_redirects=False)
location = LocationParser(redirect.headers['location'])
version = location.version
saved_tarball = Path(f'~/Software/code-{version}.tar.gz').expanduser()
if not saved_tarball.exists():
save_tarball(location.url, saved_tarball)
archive = tarfile.open(saved_tarball)
unpacked_root = Path(f'~/Software/VSCode-linux-x64-{version}').expanduser()
for member in archive:
save_member(archive, unpacked_root, member)
symlink = Path('~/Software/VSCode-linux-x64').expanduser()
if symlink.exists():
symlink.unlink()
symlink.symlink_to(unpacked_root)
def save_tarball(url, path):
with path.open('wb') as tarball:
print(f'Downloading: {url}')
download = requests.get(url, stream=True)
content_length = int(download.headers['content-length'])
with tqdm(total=content_length) as progress:
for chunk in download.iter_content(chunk_size=4096):
progress.update(len(chunk))
tarball.write(chunk)
def save_member(archive, root, member):
path = Path(
member.name.replace('VSCode-linux-x64', str(root))
)
print(f'{str(path)}')
if member.isdir():
if not path.exists():
path.mkdir()
elif member.isfile():
content = archive.extractfile(member)
# Set file content
buf = content.read()
path.write_bytes(buf)
# Set file attribute bits
path.chmod(member.mode)
# Set file timestamps
# Path.utime(member.mtime) does not yet exists
os.utime(path, (member.mtime, member.mtime))
else:
breakpoint()
print(member.name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment