Skip to content

Instantly share code, notes, and snippets.

@nondanee nondanee/helper.py
Last active Jan 12, 2020

Embed
What would you like to do?
script for vscode ffmpeg lib replacement
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author: Nzix
"""
import os, shutil, platform, subprocess
import re, zipfile
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
try:
import urllib.request as urllib
except:
import urllib
shell = lambda command, cwd = None: subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, cwd = cwd).stdout.read().decode().strip()
installation = ''
possibilities = []
electron_temp = 'electron.temp.zip'
system = {'Windows': 'win32', 'Linux': 'linux', 'Darwin': 'darwin'}[platform.system()]
cli = {'win32': 'bin', 'linux': 'bin', 'darwin': 'Contents/Resources/app/bin'}
lib = {'win32': 'ffmpeg.dll', 'linux': 'libffmpeg.so', 'darwin': 'Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Libraries/libffmpeg.dylib'}
if system == 'win32':
if 'PROGRAMW6432' in os.environ:
possibilities.append(os.environ['PROGRAMW6432'])
if 'PROGRAMFILES(X86)' in os.environ:
possibilities.append(os.environ['PROGRAMFILES(X86)'])
if 'PROGRAMFILES' in os.environ:
possibilities.append(os.environ['PROGRAMFILES'])
if 'LOCALAPPDATA' in os.environ:
possibilities.append(os.path.join(os.environ['LOCALAPPDATA'], 'Programs'))
possibilities = [os.path.join(path, 'Microsoft VS Code') for path in possibilities]
where_code = shell('where code 2> nul').split('\r\n')[0]
if where_code:
possibilities.append(os.path.abspath(os.path.join(os.path.realpath(where_code), os.path.pardir, os.path.pardir)))
elif system == 'linux':
which_code = shell('which code')
if which_code:
possibilities.append(os.path.abspath(os.path.join(os.path.realpath(which_code), os.path.pardir, os.path.pardir)))
elif system == 'darwin':
application = '/Applications/Visual Studio Code.app'
if os.path.exists(application):
possibilities.append(application)
if not installation:
possibilities = list(set(possibilities))
for path in possibilities:
if os.path.exists(path):
installation = path
break
assert installation
vscode_version = shell(('./' if system != 'win32' else '') + 'code -v --user-data-dir="."', os.path.join(installation, cli[system])).split()
print('vscode {version} {arch}'.format(version = vscode_version[0], arch = vscode_version[-1]))
yarnrc = urllib.urlopen('https://raw.githubusercontent.com/Microsoft/vscode/{version}/.yarnrc'.format(version = vscode_version[0])).read().decode()
electron_version = re.search(r'target "([^"]+)"', yarnrc).group(1)
print('electron {version}'.format(version = electron_version))
urllib.urlretrieve('https://npm.taobao.org/mirrors/electron/{version}/electron-v{version}-{system}-{arch}.zip'.format(version = electron_version, system = system, arch = vscode_version[-1]), electron_temp)
print('download well')
try:
with zipfile.ZipFile(electron_temp) as z:
with z.open(lib[system]) as src, open(os.path.join(installation, lib[system].replace('Electron.app', '.')), 'wb') as dst:
shutil.copyfileobj(src, dst)
print('replace done')
except Exception as error:
print(error)
finally:
if os.path.exists(electron_temp):
os.remove(electron_temp)
print('remove temp')
@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Jan 13, 2019

Shell

curl https://gist.githubusercontent.com/nondanee/f157bbbccecfe29e48d87273cd02e213/raw | python
@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Jan 13, 2019

Powershell

Invoke-RestMethod https://gist.githubusercontent.com/nondanee/f157bbbccecfe29e48d87273cd02e213/raw | python
@u3u

This comment has been minimized.

Copy link

u3u commented Jul 28, 2019

我用这个脚本报错了:

curl https://gist.githubusercontent.com/nondanee/f157bbbccecfe29e48d87273cd02e213/raw | python

vscode 1.36.1 x64
Traceback (most recent call last):
  File "<stdin>", line 58, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Jul 28, 2019

@u3u
这个 url https://raw.githubusercontent.com/Microsoft/vscode/1.36.1/.yarnrc 可以直接访问吗
再跑一遍呢?感觉是网络问题

@u3u

This comment has been minimized.

Copy link

u3u commented Jul 28, 2019

这个可以访问 试过很多次了 还是这个错

curl https://raw.githubusercontent.com/Microsoft/vscode/1.36.1/.yarnrc
disturl "https://atom.io/download/electron"
target "4.2.5"
runtime "electron"
@u3u

This comment has been minimized.

Copy link

u3u commented Jul 28, 2019

和 Python 版本有关系吗?

python --version
Python 2.7.16
@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Jul 28, 2019

@u3u
没关系的(我也是 python 2.7.16)
你 wget 下来 run 试试吧,我觉得没错啊

yarnrc = urllib.urlopen('https://raw.githubusercontent.com/Microsoft/vscode/{version}/.yarnrc'.format(version = vscode_version[0])).read().decode()
electron_version = re.search(r'target "([^"]+)"', yarnrc).group(1)
print('electron {version}'.format(version = electron_version))

yarnrc 打出来看看

@u3u

This comment has been minimized.

Copy link

u3u commented Jul 29, 2019

不知道为啥,好像是 .yarnrc 没有获取到,但是我直接打开链接是能打开的

# https://raw.githubusercontent.com/Microsoft/vscode/1.36.1/.yarnrc
yarnrc = urllib.urlopen('https://raw.githubusercontent.com/Microsoft/vscode/{version}/.yarnrc'.format(version = vscode_version[0])).read().decode()
print(type(yarnrc)) # <type 'unicode'>
print(len(yarnrc)) # 0
@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Jul 29, 2019

@u3u
我也不知道
要不你手动替换一下吧 =.=

@u3u

This comment has been minimized.

Copy link

u3u commented Jul 29, 2019

QAQ 我手动把脚本的 yarnrc 写死为 .yarnrc 的内容了,已经替换好了

@tw7613781

This comment has been minimized.

Copy link

tw7613781 commented Sep 3, 2019

使用自动替代脚本的时候出现如下错误
[Errno 13] Permission denied: u'/usr/share/code/libffmpeg.so'
操作系统
NAME="Linux Mint"
VERSION="19.1 (Tessa)"
ID=linuxmint
ID_LIKE=ubuntu
PRETTY_NAME="Linux Mint 19.1"
VERSION_ID="19.1"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.ubuntu.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=tessa
UBUNTU_CODENAME=bionic
解决方案
手动下载脚本
curl https://...... --output replacement_script.sh
修改文件属性
chmod 755 replacement_script.sh
使用sudo方式运行脚本
sudo ./replacement_script.sh

@yunyoulu

This comment has been minimized.

Copy link

yunyoulu commented Nov 15, 2019

Hello @nondanee, I installed vscode to a non-standard directory so I was unable to use the script.
But I've forked and added some auto-detection that you might want to take a look at.

Thank you very much for your excellent work on the extension. It's life-saving.

@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Nov 16, 2019

@yunyoulu
Check
But I find a easier way to get the code.cmd path using command where instead of traversing directories in PATH.

I will update this script soon.
Thank you for your suggestion.

@nondanee

This comment has been minimized.

Copy link
Owner Author

nondanee commented Nov 16, 2019

@tw7613781
先切到 su 再 run 就行

sudo su
curl https://gist.githubusercontent.com/nondanee/f157bbbccecfe29e48d87273cd02e213/raw | python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.