Skip to content

Instantly share code, notes, and snippets.

@elibroftw
Last active October 8, 2020 22:58
Show Gist options
  • Save elibroftw/1a94149a2f6232d062b666c4ee9ea530 to your computer and use it in GitHub Desktop.
Save elibroftw/1a94149a2f6232d062b666c4ee9ea530 to your computer and use it in GitHub Desktop.
A snippet of my addon build script that I use to automate publishing new versions.
from glob import glob
import io
import os
import time
import uuid
from zipfile import ZipFile
# third party libraries
import jwt # PyJWT
import requests
# get API keys from https://addons.mozilla.org/developers/addon/api/key/
# and put them into a .env file like so:
# jwt-issuer=string
# jwt-secret=string
GUID = 'GUID GOES HERE' # make sure to include the '{' and '}' if your GUID has it
addon_files = ['manifest.json', 'style.css'] + glob('icons/*.png')
# read environmental variables
with open('.env') as f:
line = f.readline()
while line:
k, v = line.split('=', 1)
os.environ[k] = v.strip()
line = f.readline()
def create_zip(file):
""" file: filename or file-type object """
with ZipFile(file, 'w') as zf:
for file in addon_files:
zf.write(file)
def upload(version):
# e.g. version = '1.1.1.1'
# create auth JWT token
jwt_secret = os.environ['jwt-secret']
jwt_issuer = os.environ['jwt-issuer']
jwt_obj = {
'iss': jwt_issuer,
'jti': str(uuid.uuid4()),
'iat': time.time(),
'exp': time.time() + 60
}
jwt_obj = jwt.encode(jwt_obj, jwt_secret, algorithm='HS256').decode()
file = io.BytesIO()
create_zip(file)
print(f'uploading version {version}')
data = {'upload': ('manifest.zip', file.getvalue()), 'channel': 'listed'}
headers = {'Authorization': f'JWT {jwt_obj}'}
url = f'https://addons.mozilla.org/api/v4/addons/{GUID}/versions/{version}/'
r = requests.put(url, data, headers=headers, files=data)
print(r.status_code)
print(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment