Created
May 10, 2021 21:05
-
-
Save xcsrz/2d68e6c52bf1e88c153292f525431233 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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