replace build.sh by build.py
authorGavin Sharp <gavin@gavinsharp.com>
Sat, 31 Oct 2009 01:21:06 -0400
changeset 11 18807249b971
parent 10 e62fb09006d8
child 12 a17a63e59463
push id12
push usergsharp@mozilla.com
push dateSat, 31 Oct 2009 05:23:04 +0000
replace build.sh by build.py
build.py
build.sh
config_build.sh
new file mode 100755
--- /dev/null
+++ b/build.py
@@ -0,0 +1,138 @@
+#!/usr/bin/env python
+# build.py -- builds JAR and XPI files for mozilla extensions
+#   by Ted Mielczarek <ted.mielczarek@gmail.com>
+# Based on build.sh
+#   by Nickolay Ponomarev <asqueella@gmail.com>
+#   (original version based on Nathan Yergler's build script)
+
+# This script assumes the following directory structure:
+# ./
+#   chrome.manifest
+#   install.rdf
+#   (other files listed in $ROOT_FILES)
+#
+#   content/    |
+#   locale/     |} these can be named arbitrary and listed in $CHROME_PROVIDERS
+#   skin/       |
+#
+#   defaults/   |
+#   components/ |} these must be listed in $ROOT_DIRS in order to be packaged
+#   ...         |
+#
+# Script's output is:
+# ./$APP_NAME.xpi
+# ./$APP_NAME.jar  (only if $KEEP_JAR=1)
+#
+# Note: It modifies chrome.manifest when packaging so that it points to 
+#       chrome/$APP_NAME.jar!/*
+
+#
+# default configuration file is ./config_build.sh
+
+import os.path
+import sys
+import re
+from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED
+
+def ReadConfig(config_file):
+    """Read key=value pairs from a config file. Treat lines starting with
+    # as comments, strip quotes from around the value.
+    Returns a dict of key, value pairs.
+    """
+    d = {}
+    for line in file(config_file):
+        if line.startswith("#"):
+            continue
+        if line.find('=') == -1:
+            continue
+        (var,val) = line.split('=',1)
+        var = var.strip()
+        val = val.strip()
+        val = val.strip('\"\'')
+        d[var] = val
+    return d
+
+def GetZipPath(file, dir, dirname):
+    """Given an absolute file path, an absolute directory path, and the dirname
+    of the directory, return a relative path to the file from the parent
+    directory of the given directory, with unix-style path separators.
+    """
+    file = dirname + file[len(dir):]
+    return file.replace("\\", "/")
+
+def GetFiles(dir, dirname):
+    """Given a directory and the dirname of the directory, recursively
+    traverse the directory and return a list of tuples containing
+    (filename, relative filename), where 'relative filename' is
+    generated using GetZipPath.
+    """
+    files = []
+    for (dirpath, dirnames, filenames) in os.walk(dir, True):
+        # skip emacs backup files
+        files.extend([os.path.join(dirpath, f) for f in filenames
+                      if not f.endswith("~")])
+        # skip CVS and .svn dirs
+        # funky slice assignment here
+        dirnames[:] = [d for d in dirnames if d != 'CVS' and d != '.svn']
+    return [(f, GetZipPath(f, dir, dirname)) for f in files]
+
+path = os.path.dirname(os.path.abspath(sys.argv[0]))
+config = ReadConfig(os.path.join(path, "config_build.sh"))
+
+if not 'APP_NAME' in config or not 'CHROME_PROVIDERS' in config:
+    print >>sys.stderr, "You need a config_build.sh"
+    sys.exit(1)
+
+appname = config['APP_NAME']
+jarfile = os.path.join(path, appname+'.jar')
+xpifile = os.path.join(path, appname+'.xpi')
+# clean up existing jar/xpi files
+try:
+    os.remove(jarfile)
+    os.remove(xpifile)
+except:
+    pass
+
+# Get list of all files in chrome provider paths
+jarfiles = []
+for chromedir in config['CHROME_PROVIDERS'].split():
+    jarfiles.extend(GetFiles(os.path.join(path,chromedir), chromedir))
+
+jarzip = ZipFile(jarfile, 'w', ZIP_STORED)
+for j in jarfiles:
+    jarzip.write(j[0], j[1])
+jarzip.close()
+
+# Necessary files: the jar and install.rdf
+xpifiles = [(jarfile,'chrome/'+appname+'.jar'), ('install.rdf','install.rdf')]
+
+# Optional directories to add to the XPI
+if 'ROOT_DIRS' in config:
+    for rootdir in config['ROOT_DIRS'].split():
+        xpifiles.extend(GetFiles(os.path.join(path,rootdir), rootdir))
+
+# Optional files to add to the XPI
+if 'ROOT_FILES' in config:
+    xpifiles.extend([(f,f) for f in config['ROOT_FILES'].split()])
+
+xpizip = ZipFile(xpifile, 'w', ZIP_DEFLATED)
+for x in xpifiles:
+    xpizip.write(x[0], x[1])
+
+# munge chrome.manifest into having jar entries
+manifest = os.path.join(path, 'chrome.manifest')
+if os.path.exists(manifest):
+    manifest_munged = ''
+    contentre = re.compile(r'^(content\s+\S*\s+)(.*)$')
+    skinlocre = re.compile(r'^(skin|locale)(\s+\S*\s+\S*\s+)(.*)$')
+    for line in file(manifest):
+        line = line.rstrip()
+        line = contentre.sub(r'\1jar:chrome/'+ appname +r'.jar!/\2', line)
+        line = skinlocre.sub(r'\1\2jar:chrome/'+ appname +r'.jar!/\3', line)
+        manifest_munged += line + "\n"
+    xpizip.writestr('chrome.manifest', manifest_munged)
+xpizip.close()
+
+os.remove(jarfile)
+
+print "Done!"
deleted file mode 100755
--- a/build.sh
+++ /dev/null
@@ -1,128 +0,0 @@
-#!/bin/bash
-# build.sh -- builds JAR and XPI files for mozilla extensions
-#   by Nickolay Ponomarev <asqueella@gmail.com>
-#   (original version based on Nathan Yergler's build script)
-# Most recent version is at <http://kb.mozillazine.org/Bash_build_script>
-
-# This script assumes the following directory structure:
-# ./
-#   chrome.manifest (optional - for newer extensions)
-#   install.rdf
-#   (other files listed in $ROOT_FILES)
-#
-#   content/    |
-#   locale/     |} these can be named arbitrary and listed in $CHROME_PROVIDERS
-#   skin/       |
-#
-#   defaults/   |
-#   components/ |} these must be listed in $ROOT_DIRS in order to be packaged
-#   ...         |
-#
-# It uses a temporary directory ./build when building; don't use that!
-# Script's output is:
-# ./$APP_NAME.xpi
-# ./$APP_NAME.jar  (only if $KEEP_JAR=1)
-# ./files -- the list of packaged files
-#
-# Note: It modifies chrome.manifest when packaging so that it points to 
-#       chrome/$APP_NAME.jar!/*
-
-#
-# default configuration file is ./config_build.sh, unless another file is 
-# specified in command-line. Available config variables:
-APP_NAME=          # short-name, jar and xpi files name. Must be lowercase with no spaces
-CHROME_PROVIDERS=  # which chrome providers we have (space-separated list)
-CLEAN_UP=          # delete the jar / "files" when done?       (1/0)
-ROOT_FILES=        # put these files in root of xpi (space separated list of leaf filenames)
-ROOT_DIRS=         # ...and these directories       (space separated list)
-BEFORE_BUILD=      # run this before building       (bash command)
-AFTER_BUILD=       # ...and this after the build    (bash command)
-
-if [ -z $1 ]; then
-  . ./config_build.sh
-else
-  . $1
-fi
-
-if [ -z $APP_NAME ]; then
-  echo "You need to create build config file first!"
-  echo "Read comments at the beginning of this script for more info."
-  exit;
-fi
-
-ROOT_DIR=`pwd`
-TMP_DIR=build
-
-#uncomment to debug
-#set -x
-
-# remove any left-over files from previous build
-rm -f $APP_NAME.jar $APP_NAME.xpi files
-rm -rf $TMP_DIR
-
-$BEFORE_BUILD
-
-mkdir --parents --verbose $TMP_DIR/chrome
-
-# generate the JAR file, excluding CVS and temporary files
-JAR_FILE=$TMP_DIR/chrome/$APP_NAME.jar
-echo "Generating $JAR_FILE..."
-for CHROME_SUBDIR in $CHROME_PROVIDERS; do
-  find $CHROME_SUBDIR -path '*CVS*' -prune -o -type f -print | grep -v \~ >> files
-done
-
-zip -0 -r $JAR_FILE `cat files`
-# The following statement should be used instead if you don't wish to use the JAR file
-#cp --verbose --parents `cat files` $TMP_DIR/chrome
-
-# prepare components and defaults
-echo "Copying various files to $TMP_DIR folder..."
-for DIR in $ROOT_DIRS; do
-  mkdir $TMP_DIR/$DIR
-  FILES="`find $DIR -path '*CVS*' -prune -o -type f -print | grep -v \~`"
-  echo $FILES >> files
-  cp --verbose --parents $FILES $TMP_DIR
-done
-
-# Copy other files to the root of future XPI.
-for ROOT_FILE in $ROOT_FILES install.rdf chrome.manifest; do
-  cp --verbose $ROOT_FILE $TMP_DIR
-  if [ -f $ROOT_FILE ]; then
-    echo $ROOT_FILE >> files
-  fi
-done
-
-cd $TMP_DIR
-
-if [ -f "chrome.manifest" ]; then
-  echo "Preprocessing chrome.manifest..."
-  # You think this is scary?
-  #s/^(content\s+\S*\s+)(\S*\/)$/\1jar:chrome\/$APP_NAME\.jar!\/\2/
-  #s/^(skin|locale)(\s+\S*\s+\S*\s+)(.*\/)$/\1\2jar:chrome\/$APP_NAME\.jar!\/\3/
-  #
-  # Then try this! (Same, but with characters escaped for bash :)
-  sed -i -r s/^\(content\\s+\\S*\\s+\)\(\\S*\\/\)$/\\1jar:chrome\\/$APP_NAME\\.jar!\\/\\2/ chrome.manifest
-  sed -i -r s/^\(skin\|locale\)\(\\s+\\S*\\s+\\S*\\s+\)\(.*\\/\)$/\\1\\2jar:chrome\\/$APP_NAME\\.jar!\\/\\3/ chrome.manifest
-
-  # (it simply adds jar:chrome/whatever.jar!/ at appropriate positions of chrome.manifest)
-fi
-
-# generate the XPI file
-echo "Generating $APP_NAME.xpi..."
-zip -r ../$APP_NAME.xpi *
-
-cd "$ROOT_DIR"
-
-echo "Cleanup..."
-if [ $CLEAN_UP = 0 ]; then
-  # save the jar file
-  mv $TMP_DIR/chrome/$APP_NAME.jar .
-else
-  rm ./files
-fi
-
-# remove the working files
-rm -rf $TMP_DIR
-echo "Done!"
-
-$AFTER_BUILD
--- a/config_build.sh
+++ b/config_build.sh
@@ -1,9 +1,8 @@
 #!/bin/bash
 # Build config for build.sh
 APP_NAME=tabcloser
 CHROME_PROVIDERS="content locale"
-CLEAN_UP=1
 ROOT_FILES=
-#ROOT_DIRS="defaults"
+ROOT_DIRS=
 BEFORE_BUILD=
 AFTER_BUILD=