# -*- coding: utf-8 -*-
from setuphelpers import *
import json
import platform
# Usable WAPT package functions: install(), uninstall(), session_setup(), audit(), update_package()
# Declaring global variables - Warnings: 1) WAPT context is only available in package functions; 2) Global variables are not persistent between calls
ext_uid_name = 'ms-python-release'
ext_file_name = '%s.vsix' % ext_uid_name
def install():
# Initializing variables
if get_os_name() == 'Windows':
app_dir = makepath(programfiles, 'Microsoft VS Code')
elif get_os_name() == 'Linux':
app_dir = '/usr/share/code/'
elif get_os_name() == 'Darwin':
app_dir = '/Applications/Visual Studio Code.app/'
ext_path = makepath(app_dir, ext_file_name)
# Copying extension to app_dir
print("Copying: %s to: %s" % (ext_file_name, app_dir))
filecopyto(ext_file_name, app_dir)
def uninstall():
# Initializing variables
if get_os_name() == 'Windows':
app_dir = makepath(programfiles, 'Microsoft VS Code')
elif get_os_name() == 'Linux':
app_dir = '/usr/share/code/'
elif get_os_name() == 'Darwin':
app_dir = '/Applications/Visual Studio Code.app/'
ext_path = makepath(app_dir, ext_file_name)
# Removing extension from app_dir
print("Removing: %s" % ext_path)
remove_file(ext_path)
def session_setup():
# Initializing variables
if get_os_name() == 'Windows':
app_dir = makepath(programfiles, 'Microsoft VS Code')
elif get_os_name() == 'Linux':
app_dir = '/usr/share/code/'
elif get_os_name() == 'Darwin':
app_dir = '/Applications/Visual Studio Code.app/'
ext_path = makepath(app_dir, ext_file_name)
# Installing extension in user env
print("Installing %s extension in user env" % ext_file_name)
try:
run('code --install-extension "%s"' % ext_path)
except:
run_notfatal('code --install-extension %s --force' % ext_uid_name)
def update_package():
# Declaring local variables
result = False
proxies = get_proxies()
if not proxies:
proxies = get_proxies_from_wapt_console()
app_name = control.name
git_repo = 'microsoft/vscode-python'
url_api = 'https://api.github.com/repos/%s/releases/latest' % git_repo
# Getting latest version from official sources
print('API used is: %s' % url_api)
json_load = json.loads(wgets(url_api, proxies=proxies))
for download in json_load['assets']:
if download['name'] == ext_file_name:
url_dl = download['browser_download_url']
break
version = json_load['tag_name']
latest_bin = ext_file_name
print("Latest %s version is %s" % (app_name, version))
print("Download url is %s" % url_dl)
# Downloading latest binaries
print('Downloading %s' % latest_bin)
wget(url_dl, latest_bin, proxies=proxies)
# Changing version of the package
if Version(version) > control.get_software_version():
print("Software version updated from: %s to: %s" % (control.get_software_version(), Version(version)))
result = True
control.version = '%s-%s' % (Version(version), control.version.split('-', 1)[-1])
#control.set_software_version(Version(version))
control.save_control_to_wapt()
# Validating update-package-sources
return result
def get_os_name():
return platform.system()
def get_proxies():
import platform
if platform.python_version_tuple()[0] == '3':
from urllib.request import getproxies
else:
from urllib import getproxies
return getproxies()
def get_proxies_from_wapt_console():
proxies = {}
if platform.system() == 'Windows':
waptconsole_ini_path = makepath(user_local_appdata(), 'waptconsole', 'waptconsole.ini')
else:
waptconsole_ini_path = makepath(user_home_directory(), '.config', 'waptconsole', 'waptconsole.ini')
if isfile(waptconsole_ini_path):
proxy_wapt = inifile_readstring(waptconsole_ini_path, 'global', 'http_proxy')
if proxy_wapt:
proxies = {'http': proxy_wapt, 'https': proxy_wapt}
return proxies