#!/usr/bin/env python3 import sys import os import argparse import tempfile import shutil import subprocess import re import fileinput from collections import OrderedDict import json import hashlib import traceback # Hack to combine two argparse formatters class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): pass parser = argparse.ArgumentParser( description='Build a Zotero XPI', formatter_class=CustomFormatter, epilog=''' Example: build_xpi -s ~/zotero-client/build -x 5.0.1 -z - Builds zotero-build.xpi and update-build.rdf - Points update-build.rdf to https://download.zotero.org/extension/zotero-5.0.1.xpi - Points install.rdf to https://www.zotero.org/download/update.rdf Example: build_xpi -s ~/zotero-client/build -c beta -m 7c27a9bb5 -x 5.0b2 -r beta -z - Builds zotero-build.xpi and update-build.rdf - Points update-build.rdf to https://download.zotero.org/extension/zotero-5.0b2.xpi - Points install.rdf to https://www.zotero.org/download/update-beta.rdf Example: build_xpi -s ~/zotero-client/build -c alpha -m 7c27a9bb5 -x 5.0-alpha -r 5.0-branch --xpi-dir dev -z - Builds zotero-build.xpi and update-build.rdf - Points update-build.rdf to https://download.zotero.org/extension/dev/zotero-5.0-alpha.xpi - Points install.rdf to https://zotero.org/download/dev/update-5.0-branch.rdf''') parser.add_argument('--source-dir', '-s', required=True, metavar='DIR', help='Directory to build from') parser.add_argument('-c', '--channel', default='source', help='channel to add to dev build version number (e.g., "beta" for "5.0-beta.3+a5f28ca8"), or "release" or "source" to skip') parser.add_argument('--commit-hash', '-m', metavar='HASH', help='Commit hash (required for non-release builds)') parser.add_argument('--build-suffix', metavar='SUFFIX', default='build', help='suffix of output XPI') parser.add_argument('--xpi-suffix', '-x', metavar='SUFFIX', default='', help='suffix of XPI to reference in update.rdf') parser.add_argument('--rdf-suffix', '-r', metavar='SUFFIX', default='', help='suffix of update.rdf file to reference in install.rdf (e.g., "beta" for "update-beta.rdf")') parser.add_argument('--xpi-dir', metavar='DIR', default='', help='extra directory to point to when referencing the XPI in update.rdf') parser.add_argument('--zip', '-z', action='store_true', help="Create XPI instead of leaving files in build/staging") args = parser.parse_args() def main(): try: if args.xpi_suffix: args.xpi_suffix = "-" + args.xpi_suffix if args.rdf_suffix: args.rdf_suffix = "-" + args.rdf_suffix if args.build_suffix: args.build_suffix = "-" + args.build_suffix root_dir = os.path.dirname(os.path.realpath(__file__)) # Use BUILD_DIR environmental variable if present, and otherwise ./build build_dir = os.environ.get('BUILD_DIR', os.path.join(root_dir, 'build')) tmp_dir = os.path.join(build_dir, 'tmp') if not os.path.isdir(build_dir): raise Exception(build_dir + " is not a directory") src_dir = args.source_dir if not os.path.isdir(src_dir): raise Exception(src_dir + " is not a directory") if args.commit_hash: commit_hash = args.commit_hash[0:9] elif args.channel != "release": raise Exception("--commit-hash must be specified for non-release builds") log("Using source directory of " + src_dir) os.chdir(src_dir) if not os.path.exists('install.rdf'): raise FileNotFoundError("install.rdf not found in {0}".format(src_dir)) # Extract version number from install.rdf with open('install.rdf') as f: rdf = f.read() m = re.search('version>([0-9].+)\\.SOURCE