Skip to content

Instantly share code, notes, and snippets.

@FabianSchurig
Last active February 14, 2019 22:50
Show Gist options
  • Save FabianSchurig/bab617b4cd51ddae513c57423c7e02aa to your computer and use it in GitHub Desktop.
Save FabianSchurig/bab617b4cd51ddae513c57423c7e02aa to your computer and use it in GitHub Desktop.
Fusion 360 Add-In self update
#Author-
#Description-
import adsk.core, adsk.fusion, adsk.cam, traceback
import requests
import os
import tempfile
import tarfile
import shutil
import pip
import sys
import subprocess
def install(path, requirementsFileName):
if hasattr(pip, 'main'):
with open(requirementsFileName) as f:
for line in f:
pip.main(['install', '-U', line, '-t', path, '--ignore-installed', '-q'])
else:
with open(requirementsFileName) as f:
for line in f:
pip._internal.main(['install', '-U', line, '-t', path, '--ignore-installed', '-q'])
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
currentFolder = os.path.dirname(os.path.realpath(__file__))
currentFolder = os.path.join(currentFolder, '')
cwd = os.getcwd()
releasesURI = 'https://api.github.com/repos/<github name>/<your repo>/releases'
r = requests.get(releasesURI)
if r.status_code == 200:
releases = r.json()
tag_name = releases[0]['tag_name']
tarball_url = releases[0]['tarball_url']
# Create a local temporary folder
with tempfile.TemporaryDirectory() as temp:
tarball = requests.get(tarball_url)
tempFileName = os.path.join(temp, str(tag_name+'.tar.gz'))
tempFile = open(tempFileName, 'wb')
tempFile.write(tarball.content)
tempFile.close()
tar = tarfile.open(tempFileName, "r:gz")
folderName = os.path.join(tar.getmembers()[0].name.split('/')[0], '')
tar.extractall(path=temp)
tar.close()
tempDirectory = os.path.join(temp, folderName)
# delete all files in directory
for file in os.listdir(currentFolder):
filePath = os.path.join(currentFolder, file)
try:
if os.path.isfile(filePath):
os.unlink(filePath)
elif os.path.isdir(filePath): shutil.rmtree(filePath)
except Exception as e:
print(e)
# delete directory
os.rmdir(currentFolder)
# copy all extracted contents to add in folder
shutil.copytree(tempDirectory, os.path.join(currentFolder, ''))
if os.path.isfile(os.path.join(currentFolder, 'requirements.txt')):
modulesFolder= os.path.join(currentFolder, 'modules')
if not os.path.exists(modulesFolder):
os.makedirs(modulesFolder)
install(modulesFolder, os.path.join(currentFolder, 'requirements.txt'))
if os.path.isfile(os.path.join(currentFolder, '<pythonscript>.py')):
# pythonPath = 'C:\Users\<User>\AppData\Local\Autodesk\webdeploy\production\1460235013ebd0c1ef7542ea7ef3e87a095972d9\Python\python.exe'
ui.messageBox(str('restarting'))
# importlib.reload(sys.modules[__name__])
# os.execv(pythonPath, [pythonPath] + sys.argv)
# os.execl(sys.executable, 'python', __file__, *sys.argv[1:])
# popen = subprocess.Popen([sys.executable, '<pythonscript>.py'])
ui.messageBox(str(currentFolder))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment