Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup your webtask code. Can be used to make sure you have the latest version of your code locally, or used to backup in case at some point webtask.io ceases to function. Considering they very recently started failing to install new npm modules, this could be any point now I fear.
import subprocess
from json import dumps
# NOTE: Files will be written to the current directory, without any checks to prevent overwriting. Archiving and/or version control are not considered here, but highly encouraged.
# get a list of all webtasks for the current user
list_request = subprocess.run(['wt', 'ls'], stdout=subprocess.PIPE, text=True)
# transform that output into a list of task names
tasks = [ ln.replace('Name:','').strip() for ln in list_request.stdout.split("\n") if 'Name:' in ln ]
for task in tasks:
print("processing", task)
# the output contains some meta information, might as well grab that while we're here
# it does include any npm modules configured so if you're looking to migrate to
# another service, host, etc this is useful information
meta = []
code = []
in_code = False
# grab the info for this specific webtask
task_request = subprocess.run(['wt', 'inspect', '--fetch-code', task], stdout=subprocess.PIPE, text=True)
# iterate through the lines of output splitting meta from code and stashing respectively
for line in task_request.stdout.split("\n"):
if in_code:
# we're past the meta section so this is all code now
code.append(line)
elif line == 'Code:':
# this is the beginning of the code section, we don't need this line
in_code = True
else:
# we're still in the header section so stash this line in meta
meta.append(line)
# write the meta file
with open("%s.meta" % task, 'w') as fp:
fp.write("\n".join(meta))
# write the code file
with open("%s.js" % task, 'w') as fp:
fp.write("\n".join(code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment