[pitivi] Port pitivi-flatpak to Python



commit 5829cc718df98621b60a55169967f7d4988b44c1
Author: Thibault Saunier <tsaunier gnome org>
Date:   Tue Aug 9 14:24:29 2016 -0400

    Port pitivi-flatpak to Python
    
    It has the same features as the previous one minus the possibility
    to developed inside a fully prebuilt env as that option was not
    supported anyway.
    
    Differential Revision: https://phabricator.freedesktop.org/D1264

 bin/pitivi-env                       |   16 +-
 build/flatpak/enter-env              |   19 +-
 build/flatpak/json-template-expander |   63 --
 build/flatpak/pitivi-flatpak         | 1070 +++++++++++++++++++---------------
 4 files changed, 616 insertions(+), 552 deletions(-)
---
diff --git a/bin/pitivi-env b/bin/pitivi-env
index 0777e11..845962d 100755
--- a/bin/pitivi-env
+++ b/bin/pitivi-env
@@ -12,11 +12,11 @@ export FLATPAK_ENVPATH=$(realpath $SCRIPTDIR/../../)
 export CURRENT_GST=$FLATPAK_ENVPATH
 export MYPITIVI=$FLATPAK_ENVPATH
 
-export make="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b make"
-export check="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b make check"
-export minstall="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b make install"
-export autogen="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b ./autogen.sh --prefix=/app 
--disable-gtk-doc"
-export configure="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b ./configure --prefix=/app"
+export make="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d make"
+export check="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d make check"
+export minstall="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d make install"
+export autogen="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d ./autogen.sh --prefix=/app 
--disable-gtk-doc"
+export configure="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d ./configure --prefix=/app"
 
 alias make=$make
 alias check=$check
@@ -24,7 +24,7 @@ alias minstall=$minstall
 alias autogen=$autogen
 alias configure=$configure
 
-alias ptvenv="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b"
+alias ptvenv="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d"
 
 echo "-> Setting up environment if needed..."
 ptvenv "echo"
@@ -32,10 +32,10 @@ if [ "$?" = "0" ];
 then
     for i in `ptvenv ls /app/bin/`;
     do
-        alias $i="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b $i"
+        alias $i="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d $i"
     done
 
-    alias pitivi="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -b $FLATPAK_ENVPATH/pitivi/bin/pitivi"
+    alias pitivi="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d $FLATPAK_ENVPATH/pitivi/bin/pitivi"
 
     export PS1="(ptv-flatpak) $PS1"
     export PATH="$FLATPAK_ENVPATH/bin/:$PATH"
diff --git a/build/flatpak/enter-env b/build/flatpak/enter-env
index 559a820..d8d7a8d 100755
--- a/build/flatpak/enter-env
+++ b/build/flatpak/enter-env
@@ -4,28 +4,19 @@ PITIVI_DIR=$(realpath $(dirname $0)/../../)
 
 function generate_path_and_completion_calls {
   echo "export PATH=$PITIVI_DIR/bin/:/app/bin/:/usr/bin/:\$PATH"
-  echo "export CC='ccache gcc'"
-  echo "export CXX='ccache g++'"
 }
 
 RCFILE=$PITIVI_DIR/.bashrc
 cp ~/.bashrc $RCFILE
-export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app/lib/
 
 echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app/lib/" >> $RCFILE
 echo "export PS1=[ptv]\ \$PS1" >> $RCFILE
-echo "export PITIVI_DEVELOPMENT=1" >> $RCFILE
 generate_path_and_completion_calls >> $RCFILE
 
 
-if [ -z "$*" ]
-then
-  echo "=============================="
-  echo "Entering own built environment"
-  echo "=============================="
+echo "=============================="
+echo "Entering own built environment"
+echo "=============================="
 
-  cd $PITIVI_DIR
-  bash --rcfile $RCFILE
-else
-  $@
-fi
+cd $PITIVI_DIR
+bash --rcfile $RCFILE
diff --git a/build/flatpak/pitivi-flatpak b/build/flatpak/pitivi-flatpak
index d983573..d10f746 100755
--- a/build/flatpak/pitivi-flatpak
+++ b/build/flatpak/pitivi-flatpak
@@ -1,467 +1,603 @@
-#!/bin/bash
-
-# ------------------------------------------- #
-# Repository and bundle setup variables.
-# ------------------------------------------- #
-
-# The usual application name
-APPLICATION_NAME=pitivi
-
-# Where to build the FLATPAK
-FLATPAK_ENVPATH=${FLATPAK_ENVPATH:-$HOME/$APPLICATION_NAME-flatpak}
-
-# The name of the bundled application (must respect flatpak app application naming convention).
-APPLICATION_REF_NAME=org.pitivi.Pitivi
-
-# Local path to the application repository
-APPLICATION_REPOSITORY=${APPLICATION_REPOSITORY:-$FLATPAK_ENVPATH/$APPLICATION_NAME}
-
-# The name given to the application in the .desktop file
-APPLICATION_DESKTOP_NAME="(Daily) Pitivi"
-# The default branch to use and create if generating the bundle
-BRANCH="master"
-
-# The URL of the bundle remote repository
-APPLICATION_FLATPAK_REPO=http://flatpak.pitivi.org
-APPLICATION_FLATPAK_REPO_FILE=http://flatpak.pitivi.org/pitivi.flatpakrepo
-
-# The name to give to the repository if it is not registered in flatpak yet
-APPLICATION_DEFAULT_REPO_NAME=pitivi
-
-# The name of the runtime to build the bundle against
-RUNTIME_NAME=org.gnome.Platform
-# The name of the Sdk to build the bundle against
-SDK_NAME=org.gnome.Sdk
-# The version of the Sdk to build the bundle against
-SDK_VERSION=3.20
-# The URL of the Sdk remote repository
-SDK_REPO=http://sdk.gnome.org/repo/
-SDK_REPO_FILE=https://sdk.gnome.org/gnome.flatpakrepo
-# The name to give to the repository if it is not registered in flatpak yet
-SDK_DEFAULT_REPO_NAME=gnome
-
-GST_VERSION=master
-
-# ------------------------------------------- #
-
-# Some terminal output color values
-RED='\033[0;31m'
-GREEN='\033[0;32m'
-NC='\033[0m'
-
-HELP="Helper script to work with flatpak for $APPLICATION_NAME
-
-Options:
-=======
-
-General:
--------
-  -u,  --update:     Update the runtime/sdk/app and rebuild the development environment if needed
-  -n,  --no-update:  Do not update anything (By default we update bundles when not in development mode)
-
-Development:
------------
-  -d,  --devel:      Setup a devel environment, meaning that it et you in
-                     a bash environment ready to work on $APPLICATION_NAME, not that the environment
-                     will be 'read only' and you will be only able to work on $APPLICATION_NAME
-                     itself easily. To have more control, you should use the --build option.
-  -b,  --build:      Build $APPLICATION_NAME and its dependencies inside a flatpak environment.
-  -b=, --branch=:    The flatpak branch to use (0.95, master...)
-  -c,  --clean:      Clean previous builds and restart from scratch
-       --use-remote: Build from the official repository (master branch)
-
-Building bundle for distribution:
---------------------------------
-       --bundle:                  Create bundle repository in $REPO, implies --build
-       --repo-commit-subject:     The commit subject to be used when updating the ostree repository
-       --repo-commit-body:        The commit body to be used when updating the ostree repository
-       --check:                   Run make check on Pitivi (work only when --bundle is used)
-       --gpg-sign=KEYID:          The GPG key to sign the commit with (work only when --bundle is used)
-       --generate-static-deltas:  Do not generate static deltas (check 'man flatpak-build-update-repo'
-                                  for more informations)
-
-You can find more information about flatpak at: http://flatpak.org/
-"
-
-for i in "$@"
-do
-  case $i in
-    -d|--devel)
-    DEVEL=true
-    shift
-    ;;
-    -u|--update)
-    UPDATE=true
-    shift
-    ;;
-    -n|--no-update)
-    NO_UPDATE=true
-    shift
-    ;;
-    -bu|--bundle)
-    BUNDLE=true
-    shift
-    ;;
-    -c|--clean)
-    CLEAN=true
-    shift
-    ;;
-    -b=*|--branch=*)
-    BRANCH="${i#*=}"
-    shift
-    ;;
-    -b|--build)
-    BUILD=true
-    shift
-    ;;
-    --installer)
-    INSTALLER=true
-    BRANCH="stable"
-    shift
-    ;;
-    --use-remote)
-    USE_REMOTE=true
-    shift
-    ;;
-    --repo-commit-subject=*)
-    COMMIT_SUBJECT="${i#*=}"
-    shift
-    ;;
-    --check)
-    CHECK=true
-    shift
-    ;;
-    --repo-commit-body=*)
-    COMMIT_BODY="${i#*=}"
-    shift
-    ;;
-    --gst-version=*)
-    GST_VERSION="${i#*=}"
-    shift
-    ;;
-    --gpg-sign=*)
-    GPG_KEY="${i#*=}"
-    shift
-    ;;
-    --generate-static-deltas)
-    GENERATE_STATIC_DELTAS="--generate-static-deltas"
-    shift
-    ;;
-  esac
-done
-
-if [ "$1" == "--help" ]
-then
-    echo "$HELP"
-    exit 0
-fi
-
-# ------------------------------------------- #
-
-flatpak remote-add --help | grep "\-\-from" > /dev/null 2>&1
-if [ ! $? -eq 0 ]
-then
-  printf "$RED \nYou need to install flatpak >= 0.6.4 to be able to use the '$0' script.
-
-You can find some informations about how to install it for your distribution at:
-
-* http://flatpak.org/ $NC\n\n\n"
-
-  if [ -n "$INSTALLER" ]
-  then
-    printf "\n\nYou can now exit that terminal."
-    read
-  fi
-  exit 1
-fi
-
-# ------------------------------------------- #
-
-# Computation of actual variable values
-APPLICATION_REPO_NAME=`flatpak remote-list --user -d | grep "$APPLICATION_FLATPAK_REPO" | awk '{ print $1 }'`
-flatpak remote-list --user | grep $APPLICATION_DEFAULT_REPO_NAME > /dev/null 2>&1
-res=$?
-# Update remote URL if needed
-if [ $res != 0 ] && [ -n "$APPLICATION_REPO_NAME" ]
-then
-  echo "-> Setting $APPLICATION_DEFAULT_REPO_NAME URL to $APPLICATION_FLATPAK_REPO"
-  flatpak remote-modify --user $APPLICATION_DEFAULT_REPO_NAME --url=$APPLICATION_FLATPAK_REPO
-fi
-
-APPLICATION_REPO_NAME=${APPLICATION_REPO_NAME:-$APPLICATION_DEFAULT_REPO_NAME}
-SCRIPTDIR="$( cd "$( dirname "$(readlink -f ${BASH_SOURCE[0]})"  )" && pwd  )"
-
-SDK_REPO_NAME=`flatpak remote-list --user -d | grep "$SDK_REPO" | awk '{ print $1 }'`
-SDK_REPO_NAME=${SDK_REPO_NAME:-$SDK_DEFAULT_REPO_NAME}
-
-REPO=$FLATPAK_ENVPATH/flatpak-repos/$APPLICATION_NAME
-
-SDK_INSTALLED=`flatpak list --runtime -d | grep "$SDK_NAME/.*/$SDK_VERSION"`
-SDK_LOCAL_INSTALLED=`flatpak list --runtime -d | grep "$SDK_NAME.Locale/.*/$SDK_VERSION"`
-
-RUNTIME_INSTALLED=`flatpak list --runtime -d | grep "$RUNTIME_NAME/.*/$SDK_VERSION"`
-RUNTIME_LOCAL_INSTALLED=`flatpak list --runtime -d | grep "$RUNTIME_NAME.Locale/.*/$SDK_VERSION"`
-APP_INSTALLED=`flatpak list -d | grep "$APPLICATION_REF_NAME/.*/$BRANCH"`
-
-if [ -n "$BUNDLE" ]
-then
-  BUILD=true
-  DEVEL=true
-fi
-
-if [ -n "$BUILD" ]
-then
-  which flatpak-builder > /dev/null 2>&1
-  if [ ! $? -eq 0 ]
-  then
-    echo "You need to install flatpak-builder to use the $0 script for development.
-"
-    exit 1
-  fi
-  DEVEL=true
-fi
-
-if [ -z "$DEVEL" ] && [ -z "$NO_UPDATE" ]
-then
-  UPDATE=true
-
-  if [ -n "$*" ]
-  then
-    echo "Unknown arguments: $*"
-    echo ""
-    echo "$HELP"
-
-    exit 1
-  fi
-
-  if [ -n "$RUNTIME_INSTALLED" ]
-  then
-    printf "$GREEN\n==========\nUpdating $APPLICATION_NAME \n==========$NC\n\n"
-  else
-    printf "$GREEN\n==========\nInstalling $APPLICATION_NAME \n==========$NC\n\n"
-  fi
-fi
-
-function run_in_sandbox {
-  flatpak build --socket=x11 --socket=session-bus --socket=pulseaudio \
-  --share=network \
-  --env=GST_PLUGIN_SYSTEM_PATH= \
-  --env=GST_CHECKS=$GST_CHECKS \
-  --env=GST_PRESET_PATH=/app/share/gstreamer-1.0/presets/:/app/share/pitivi/gstpresets/:$GST_PRESET_PATH \
-  --env=GST_ENCODING_TARGET_PATH=/app/share/pitivi/gstpresets/:$GST_ENCODING_TARGET_PATH \
-  --env=GST_DEBUG=$GST_DEBUG \
-  --env=GST_PLUGIN_SYSTEM_PATH= \
-  --env=GST_VALIDATE=$GST_VALIDATE \
-  --env=GST_VALIDATE_LAUNCHER_DEBUG=$GST_VALIDATE_LAUNCHER_DEBUG \
-  --env=PITIVI_DEBUG=$PITIVI_DEBUG \
-  --env=PITIVI_DEVELOPMENT=1 \
-  --env=DISPLAY=$DISPLAY \
-  --env=GST_DEBUG_DUMP_DOT_DIR=$GST_DEBUG_DUMP_DOT_DIR \
-  $PREFIX $SCRIPTDIR/enter-env "$@" || exit 1
-}
-
-function build_local_repo {
-  echo "-> Building $APPLICATION_NAME from current repository"
-  cd $APPLICATION_REPOSITORY
-  run_in_sandbox "./configure --prefix=/app --libdir=lib" || exit 1
-  run_in_sandbox make || exit 1
-}
-
-# Install needed runtimes
-if [ -n "$DEVEL" ]
-then
-  if [ -z "$SDK_INSTALLED" ]
-  then
-    echo "-> Installing $SDK_NAME $SDK_VERSION"
-    TEMPFILE=$(mktemp)
-    echo "-> Adding repository '$SDK_REPO_NAME' from file: $SDK_REPO_FILE"
-    curl $SDK_REPO_FILE -Sso $TEMPFILE || exit 1
-    flatpak remote-add --user --from=$TEMPFILE $SDK_REPO_NAME > /dev/null 2>&1
-    flatpak install --user $SDK_REPO_NAME $SDK_NAME $SDK_VERSION || exit 1
-  fi
-
-  if [ -z "$SDK_LOCAL_INSTALLED" ]
-  then
-    echo "-> Installing $SDK_NAME.Locale"
-    $flatpak install --user $SDK_REPO_NAME $SDK_NAME.Locale $SDK_VERSION || exit 1
-  fi
-fi
-
-if [ -z "$RUNTIME_INSTALLED" ]
-then
-  TEMPFILE=$(mktemp)
-  echo "-> Adding repository $SDK_REPO_NAME from file: $SDK_REPO_FILE"
-  curl $SDK_REPO_FILE -Sso $TEMPFILE || exit 1
-  flatpak remote-add --user --from=$TEMPFILE $SDK_REPO_NAME > /dev/null 2>&1
-
-  echo "-> Installing $RUNTIME_NAME $SDK_VERSION $RUNTIME_INSTALLED"
-  flatpak install --user $SDK_REPO_NAME $RUNTIME_NAME $SDK_VERSION || exit 1
-fi
-if [ -z "$RUNTIME_LOCAL_INSTALLED" ]
-then
-  echo "-> Installing $RUNTIME_NAME.Locale"
-  flatpak install --user $SDK_REPO_NAME $RUNTIME_NAME.Locale $SDK_VERSION || exit 1
-fi
-
-# Update runtimes if needed
-if [ -n "$UPDATE" ]
-then
-  echo "-> Updating $RUNTIME_NAME $SDK_VERSION"
-  flatpak update --user $RUNTIME_NAME $SDK_VERSION || exit 1
-
-  echo "-> Updating $RUNTIME_NAME.Locale"
-  flatpak update --user $RUNTIME_NAME.Locale $SDK_VERSION || exit 1
-
-  if [ -n "$DEVEL" ]
-  then
-    echo "-> Updating $SDK_NAME $SDK_VERSION"
-    flatpak update --user $SDK_NAME $SDK_VERSION || exit 1
-
-    echo "-> Updating $SDK_NAME.Locale $SDK_VERSION"
-    flatpak update --user $SDK_NAME.Locale $SDK_VERSION || exit 1
-  fi
-fi
-
-# Not using remote repo and not bundling
-if [ -n "$BUILD" ] && [ -z "$BUNDLE" ]
-then
-  cd $APPLICATION_REPOSITORY
-  # Either a branch name or "HEAD".
-  DEVELOPMENT_BRANCHNAME=`git rev-parse --abbrev-ref HEAD`
-  cd - > /dev/null
-  APP_BUILD_NAME="$APPLICATION_NAME-$DEVELOPMENT_BRANCHNAME"
-  LOCAL_REPOS_PATH=$FLATPAK_ENVPATH
-else
-  DEVELOPMENT_BRANCHNAME=${BRANCH:-master}
-  APP_BUILD_NAME=$APPLICATION_NAME
-  LOCAL_REPOS_PATH=/nowhere/really
-fi
-
-if [ -n "$BUNDLE" ]
-then
-  PREFIX="$FLATPAK_ENVPATH/$APP_BUILD_NAME-prefix/"
-else
-  PREFIX=$FLATPAK_ENVPATH/prefix
-fi
-
-if [ -n "$CLEAN" ]
-then
-  echo "Cleaning environment..."
-
-  echo "-> Removing $PREFIX"
-  rm -Rf $PREFIX
-fi
-
-if [ -z $DEVEL ] || [ -z $BUILD ]
-then
-   TEMPFILE=$(mktemp)
-
-   echo "-> Adding repository $APPLICATION_REPO_NAME from file: $APPLICATION_FLATPAK_REPO_FILE"
-   curl $APPLICATION_FLATPAK_REPO_FILE -Sso $TEMPFILE || exit 1
-   flatpak remote-add --user --from=$TEMPFILE $APPLICATION_REPO_NAME > /dev/null 2>&1
-
-   if [ -z "$APP_INSTALLED" ]
-   then
-     echo "Installing $APPLICATION_REF_NAME $BRANCH..."
-
-     flatpak install --user $APPLICATION_REPO_NAME $APPLICATION_REF_NAME $BRANCH
-   fi
-
-   if [ -n "$UPDATE" ]
-   then
-     echo "-> Updating $APPLICATION_REF_NAME $BRANCH..."
-
-     flatpak update --user $APPLICATION_REF_NAME $BRANCH
-   fi
-fi
-
-if [ -n "$DEVEL" ]
-then
-  if [ -z $BUILD ]
-  then
-    if [ -z "$APP_INSTALLED" ] && [ -z "$USE_REMOTE" ]
-    then
-      build_local_repo
-    fi
-
-    echo "=============================="
-    echo "Entering prebuilt environment "
-    echo "=============================="
-
-    flatpak run --branch=$BRANCH -d --command=$SCRIPTDIR/enter-env $APPLICATION_REF_NAME
-    exit $?
-  fi
-
-  if [ -n $BUILD ] && [ ! -f $PREFIX/metadata ]
-  then
-    UPDATE=true
-    echo "Updating env $PREFIX"
-  fi
-
-  if [ -n "$UPDATE" ]
-  then
-    # Update values in the json template
-    JSON=$SCRIPTDIR/$APP_BUILD_NAME.json
-    $SCRIPTDIR/json-template-expander $SCRIPTDIR/$APPLICATION_NAME.template.json $JSON $LOCAL_REPOS_PATH 
$GST_VERSION $BRANCH
-
-    echo "-> Removing prefix $PREFIX"
-    rm -Rf $PREFIX
-
-    FLATPAK_BUILD_OPTIONS=""
-    if [ -z $BUNDLE ]
-    then
-      FLATPAK_BUILD_OPTIONS=" $FLATPAK_BUILD_OPTIONS --build-only"
-    fi
-
-    echo "-> Building $APPLICATION_NAME $DEVELOPMENT_BRANCHNAME from $JSON (options: $FLATPAK_BUILD_OPTIONS)"
-    cd $SCRIPTDIR
-    echo "Building with: flatpak-builder $FLATPAK_BUILD_OPTIONS $PREFIX $JSON"
-    flatpak-builder --ccache $FLATPAK_BUILD_OPTIONS $PREFIX $JSON || exit 1
-
-    if [ -z $BUNDLE ] || [ -n "$CHECK" ]
-    then
-      build_local_repo
-    fi
-  fi
-
-  # Launch development environment if we are not updating it and not creating a bundle
-  if [ -z $BUNDLE ] && [ -z $UPDATE ] && [ -z $CHECK ]
-  then
-    run_in_sandbox "$@"
-  fi
-
-  if [ -n "$CHECK" ]
-  then
-    run_in_sandbox "./configure --prefix=/app --libdir=lib -Denable-xunit=true"
-
-    echo "-> Running unit tests"
-    run_in_sandbox "make check"
-  fi
-else
-  flatpak run org.pitivi.Pitivi
-fi
-
-
-if [ -n "$BUNDLE" ]
-then
-  echo "-> Creating repository $REPO if needed"
-  mkdir -p $REPO
-
-  if [ -z "$COMMIT_SUBJECT" ]
-  then
-      COMMIT_SUBJECT="Updating $APPLICATION_REF_NAME"
-  fi
-
-  if [ -n "$GPG_KEY" ]
-  then
-      GPG="--gpg-sign=$GPG_KEY"
-  fi
-
-  echo "-> Exporting repo $REPO $DEVELOPMENT_BRANCHNAME (args: --body="$COMMIT_BODY" 
--subject="$COMMIT_SUBJECT")"
-  flatpak build-export $GPG --body="$COMMIT_BODY" --subject="$COMMIT_SUBJECT" $REPO $PREFIX 
$DEVELOPMENT_BRANCHNAME || exit 1
-
-  echo "Updating repo $REPO"
-  flatpak build-update-repo $GENERATE_STATIC_DELTAS $REPO || exit 1
-
-  echo "DONE!"
-fi
+#!/usr/bin/env python3
+# Pitivi video editor
+# Copyright (c) 2016, Thibault Saunier <tsaunier gnome org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+# pylint: disable=missing-docstring,invalid-name
+
+import argparse
+import configparser
+import json
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+from urllib.parse import urlparse
+from urllib.request import urlretrieve
+
+SDK_BRANCH = "3.20"
+PITIVI_BRANCH = "master"
+FLATPAK_REQ = "0.6.4"
+
+
+class Colors:  # pylint: disable=too-few-public-methods
+    HEADER = "\033[95m"
+    OKBLUE = "\033[94m"
+    OKGREEN = "\033[92m"
+    WARNING = "\033[93m"
+    FAIL = "\033[91m"
+    ENDC = "\033[0m"
+
+
+def message(str_format, *args):
+    if args:
+        print(str_format % args)
+    else:
+        print(str_format)
+
+    # Flush so that messages are printed at the right time
+    # as we use many subprocesses.
+    sys.stdout.flush()
+
+
+def expand_json_file(json_template, outfile, basedir, gst_version, branchname):
+    """Creates the manifest file."""
+    try:
+        os.remove(outfile)
+    except FileNotFoundError:
+        pass
+
+    with open(json_template, "r") as tf:
+        template = json.load(tf)
+    if branchname == "stable":
+        try:
+            del template["desktop-file-name-prefix"]
+        except KeyError:
+            pass
+    elif branchname == "master":
+        template["desktop-file-name-prefix"] = "(Rolling) "
+    else:
+        template["desktop-file-name-prefix"] = "(%s) " % branchname
+
+    message("-> Generating %s against GStreamer %s", outfile, gst_version)
+
+    for module in template["modules"]:
+        if module["sources"][0]["type"] != "git":
+            continue
+
+        if module["name"].startswith("gst"):
+            module["sources"][0]["branch"] = gst_version
+            if gst_version != "master":
+                continue
+
+        repo = os.path.join(basedir, module["name"])
+        if not os.path.exists(os.path.join(repo, ".git")):
+            message("-> Module: %s using online repo: %s",
+                    module["name"], module["sources"][0]["url"])
+            continue
+
+        branch = subprocess.check_output(
+            r"git branch 2>&1 | grep \*", shell=True,
+            cwd=repo).decode(
+                "utf-8").split(" ")[1].strip("\n")
+
+        repo = "file://" + repo
+        message("-> Module: %s repo: %s branch: %s",
+                module["name"], repo, branch)
+        module["sources"][0]["url"] = repo
+        module["sources"][0]["branch"] = branch
+
+    with open(outfile, "w") as of:
+        print(json.dumps(template, indent=4), file=of)
+
+
+class FlatpakObject:  # pylint: disable=too-few-public-methods
+
+    def __init__(self, user):
+        self.user = user
+
+    def flatpak(self, command, *args, show_output=False, comment=None):
+        if comment:
+            message(comment)
+
+        command = ["flatpak", command]
+        if self.user:
+            res = subprocess.check_output(command + ["--help"]).decode("utf-8")
+            if "--user" in res:
+                command.append("--user")
+        command.extend(args)
+
+        if not show_output:
+            return subprocess.check_output(command).decode("utf-8")
+
+        return subprocess.check_call(command)
+
+
+class FlatpakPackages(FlatpakObject):  # pylint: disable=too-few-public-methods
+
+    def __init__(self, repos, user=True):
+        FlatpakObject.__init__(self, user=user)
+
+        self.repos = repos
+
+        self.runtimes = self.__detect_runtimes()
+        self.apps = self.__detect_apps()
+        self.packages = self.runtimes + self.apps
+
+    def __detect_packages(self, *args):
+        packs = []
+        package_defs = [rd for rd in
+                        self.flatpak("list", "-d", *args).split("\n") if rd]
+        for package_def in package_defs:
+            splited_packaged_def = [p for p in package_def.split(" ") if p]
+            name, arch, branch = splited_packaged_def[0].split("/")
+            repo = self.repos.repos[splited_packaged_def[1]]
+
+            packs.append(FlatpakPackage(name, branch, repo, arch))
+
+        return packs
+
+    def __detect_runtimes(self):
+        return self.__detect_packages("--runtime")
+
+    def __detect_apps(self):
+        return self.__detect_packages()
+
+    def __iter__(self):
+        for package in self.packages:
+            yield package
+
+
+class FlatpakRepos(FlatpakObject):
+
+    def __init__(self, user=True):
+        FlatpakObject.__init__(self, user=user)
+        self.repos = {}
+        self.update()
+
+    def update(self):
+        self.repos = {}
+        repo_defs = [rd for rd in
+                     self.flatpak("remote-list", "-d").split("\n") if rd]
+        for repo in repo_defs:
+            components = repo.split(" ")
+            name = components[0]
+            desc = ""
+            url = None
+            for elem in components[1:]:
+                if not elem:
+                    continue
+                parsed_url = urlparse(elem)
+                if parsed_url.scheme:
+                    url = elem
+                    break
+
+                if desc:
+                    desc += " "
+                desc += elem
+
+            if not url:
+                message("No valid URI found for: %s", repo)
+                continue
+
+            self.repos[name] = FlatpakRepo(name, desc, url, repos=self)
+
+        self.packages = FlatpakPackages(self)
+
+    def add(self, repo, override=True):
+        same_name = None
+        for name, tmprepo in self.repos.items():
+            if repo.url == tmprepo.url:
+                return tmprepo
+            elif repo.name == name:
+                same_name = tmprepo
+
+        if same_name:
+            if override:
+                self.flatpak("remote-modify", repo.name, "--url=" + repo.url,
+                             comment="Setting repo %s URL from %s to %s"
+                             % (repo.name, same_name.url, repo.url))
+                same_name.url = repo.url
+
+                return same_name
+            else:
+                return None
+        else:
+            self.flatpak("remote-add", repo.name, "--from=" +
+                         repo.repo_file.name,
+                         comment="Adding repo %s" % repo.name)
+
+        repo.repos = self
+        return repo
+
+
+class FlatpakRepo(FlatpakObject):  # pylint: disable=too-few-public-methods
+
+    def __init__(self, name, desc=None, url=None,  # pylint: disable=too-many-arguments
+                 repo_file=None, user=True, repos=None):
+        FlatpakObject.__init__(self, user=user)
+
+        self.name = name
+        self.url = url
+        self.desc = desc
+        self.repo_file_name = repo_file
+        self._repo_file = None
+        self.repos = repos
+        assert name
+        if repo_file and not url:
+            repo = configparser.ConfigParser()
+            repo.read(self.repo_file.name)
+            self.url = repo["Flatpak Repo"]["Url"]
+        else:
+            assert url
+
+    @property
+    def repo_file(self):
+        if self._repo_file:
+            return self._repo_file
+
+        assert self.repo_file_name
+        self._repo_file = tempfile.NamedTemporaryFile(mode="w")
+        urlretrieve(self.repo_file_name, self._repo_file.name)
+
+        return self._repo_file
+
+
+class FlatpakPackage(FlatpakObject):
+
+    def __init__(self, name, branch, repo, arch, user=True):  # pylint: disable=too-many-arguments
+        FlatpakObject.__init__(self, user=user)
+
+        self.name = name
+        self.branch = branch
+        self.repo = repo
+        self.arch = arch
+
+    def __str__(self):
+        return "%s/%s/%s %s" % (self.name, self.arch, self.branch, self.repo.name)
+
+    def is_installed(self):
+        self.repo.repos.update()
+        for package in self.repo.repos.packages:
+            if package.name == self.name and \
+                    package.branch == self.branch and \
+                    package.arch == self.arch:
+                return True
+
+        return False
+
+    def install(self):
+        self.flatpak("install", self.repo.name, self.name,
+                     self.branch, show_output=True,
+                     comment="Installing %s" % self.name)
+
+    def update(self):
+        if not self.is_installed():
+            return self.install()
+
+        self.flatpak("update", self.name, self.branch, show_output=True,
+                     comment="Updating %s" % self.name)
+
+    def run_app(self, args):
+        if not isinstance(args, list):
+            args = [args]
+
+        self.flatpak("run", "--branch=" + self.branch, self.name, *args,
+                     show_output=True,
+                     comment="Running %s" % self.name)
+
+
+class PitiviFlatpak:  # pylint: disable=too-many-instance-attributes
+
+    def __init__(self):
+        self.name = "Pitivi"
+        self.sdk_repo = None
+        self.app_repo = None
+        self.runtime = None
+        self.locale = None
+        self.sdk = None
+        self.app = None
+
+        self.packs = []
+        self.update = False
+        self.devel = False
+        self.json = None
+        self.args = []
+        self.build = False
+        self.scriptdir = os.path.abspath(os.path.dirname(__file__))
+        self.envpath = os.environ.get("FLATPAK_ENVPATH",
+                                      os.path.expanduser("~/%s-flatpak" %
+                                                         self.name.lower()))
+        self.prefix = os.path.join(
+            self.envpath, "%s-prefix" % self.name.lower())
+        self.repodir = os.path.join(
+            self.envpath, "flatpak-repos", self.name.lower())
+        self.local_repos_path = os.path.abspath(os.path.join(
+            self.scriptdir, os.pardir, os.pardir, os.pardir))
+        self.topdir = os.path.abspath(os.path.join(
+            self.scriptdir, os.pardir, os.pardir))
+
+        self.build_name = self.name
+        if os.path.exists(os.path.join(self.topdir, ".git")):
+            try:
+                devnull = open(os.devnull)
+                self.build_name = self.name + "." + subprocess.check_output(
+                    "git rev-parse --abbrev-ref HEAD".split(
+                        " "), stderr=devnull,
+                    cwd=self.topdir).decode("utf-8").strip("\n")
+            except subprocess.CalledProcessError:
+                pass
+            finally:
+                devnull.close()
+
+    def check_flatpak(self):
+        try:
+            version = subprocess.check_output(
+                ["flatpak", "--version"]).decode("utf-8").split(
+                    " ")[1].strip("\n")
+
+            if version < FLATPAK_REQ:
+                message("\n%sFlatpak %s required %s found. Please update and try again%s\n", Colors.FAIL,
+                        FLATPAK_REQ, version, Colors.ENDC)
+                self.exit(1)
+        except FileNotFoundError:
+            message("\n%sYou need to install flatpak >= %s"
+                    " to be able to use the '%s' script.\n\n"
+                    "You can find some informations about"
+                    " how to install it for your distribution at:\n"
+                    "    * http://flatpak.org/%s\n";, Colors.FAIL,
+                    FLATPAK_REQ, sys.argv[0], Colors.ENDC)
+            self.exit(1)
+
+    def exit(self, exitcode):
+        if self.installer:
+            input("Failure installing %s press <enter> to continue" % self.name)  # pylint: 
disable=bad-builtin
+
+        exit(exitcode)
+
+    def clean_args(self, args):
+        self.check_flatpak()
+
+        repos = FlatpakRepos()
+        self.sdk_repo = repos.add(
+            FlatpakRepo("gnome",
+                        url="http://sdk.gnome.org/repo/";,
+                        repo_file="https://sdk.gnome.org/gnome.flatpakrepo";))
+
+        self.app_repo = repos.add(
+            FlatpakRepo("pitivi",
+                        url="http://flatpak.pitivi.org/";,
+                        repo_file="http://flatpak.pitivi.org/pitivi.flatpakrepo";))
+        self.runtime = FlatpakPackage(
+            "org.gnome.Platform", SDK_BRANCH, self.sdk_repo, "x86_64")
+        self.locale = FlatpakPackage(
+            "org.gnome.Platform.Locale", SDK_BRANCH, self.sdk_repo, "x86_64")
+        self.sdk = FlatpakPackage(
+            "org.gnome.Sdk", SDK_BRANCH, self.sdk_repo, "x86_64")
+        self.app = FlatpakPackage(
+            "org.pitivi.Pitivi", PITIVI_BRANCH, self.app_repo, "x86_64")
+        self.packs = [self.runtime, self.locale]
+
+        self.args = args
+        if self.bundle:
+            self.build = True
+
+        if self.devel:
+            self.packs.append(self.sdk)
+        else:
+            self.packs.append(self.app)
+            self.local_repos_path = "/nowhere/really/"
+
+        self.json = os.path.join(self.scriptdir, self.build_name + ".json")
+
+    def run(self):
+        if self.clean and os.path.exists(self.prefix):
+            shutil.rmtree(self.prefix)
+
+        if self.update:
+            self.update_all()
+
+        if self.devel:
+            self.setup_dev_env()
+
+        if self.check:
+            self.run_in_sandbox("make", "check",
+                                exit_on_failure=True,
+                                cwd=self.topdir)
+
+        if self.bundle:
+            self.update_bundle()
+
+        if not self.devel:
+            self.install_all()
+            self.app.run_app(self.args)
+
+    def update_bundle(self):
+        if not os.path.exists(self.prefix):
+            self.setup_dev_env()
+
+        if not os.path.exists(self.repodir):
+            os.mkdir(self.repodir)
+
+        build_export_args = ["flatpak",
+                             "build-export", self.repodir, self.prefix]
+        if self.gpg_key:
+            build_export_args.append("--gpg-sign=%s" % self.gpg_key)
+        if self.commit_subject:
+            build_export_args.append("--subject=%s" % self.commit_subject)
+        if self.commit_body:
+            build_export_args.append("--body=%s" % self.commit_body)
+
+        message('-> Exporting repo %s %s (args: --body="%s" --subject="%s")',
+                self.repodir, self.branch, self.commit_body,
+                self.commit_subject)
+        try:
+            subprocess.check_call(build_export_args)
+        except subprocess.CalledProcessError:
+            self.exit(1)
+
+        message("Updating repo %s", self.repodir)
+        update_repo_args = ["flatpak", "build-update-repo"]
+
+        if self.generate_static_deltas:
+            update_repo_args.append("--generate-static-deltas")
+
+        update_repo_args.append(self.repodir)
+
+        try:
+            subprocess.check_call(update_repo_args)
+        except subprocess.CalledProcessError:
+            self.exit(1)
+
+    def setup_dev_env(self):
+        self.install_all()
+
+        if not os.path.exists(self.prefix) or self.update:
+            if os.path.exists(self.prefix):
+                shutil.rmtree(self.prefix)
+
+            json_template = os.path.join(
+                self.scriptdir, "%s.template.json" % self.name.lower())
+            expand_json_file(json_template, self.json,
+                             self.local_repos_path, self.gst_version,
+                             self.branch)
+
+            builder_args = ["flatpak-builder",
+                            "--ccache", self.prefix, self.json]
+            if self.bundle:
+                builder_args.append("--build-only")
+
+            message("Building Pitivi %s", self.branch)
+            subprocess.check_call(builder_args)
+
+            configure_args = ["./configure", "--prefix=/app", "--libdir=lib"]
+            if self.check:
+                configure_args.append("-Denable-xunit=true")
+
+            self.run_in_sandbox(*configure_args, exit_on_failure=True,
+                                cwd=self.topdir)
+            self.run_in_sandbox("make", exit_on_failure=True,
+                                cwd=self.topdir)
+
+        if not self.check and not self.update:
+            self.run_in_sandbox(*self.args, exit_on_failure=True)
+
+    def run_in_sandbox(self, *args, exit_on_failure=False, cwd=None):
+        env_args = ["flatpak", "build", "--socket=x11",
+                    "--socket=session-bus", "--socket=pulseaudio",
+                    "--share=network", "--env=PITIVI_DEVELOPMENT=1",
+                    "--env=CC=ccache gcc",
+                    "--env=CXX=ccache g++"]
+
+        for envvar in [
+                "GST_PLUGIN_SYSTEM_PATH", "GST_CHECKS", "GST_DEBUG",
+                "GST_PLUGIN_SYSTEM_PATH", "GST_VALIDATE", "GST_VALIDATE_LAUNCHER_DEBUG",
+                "PITIVI_DEBUG", "PITIVI_DEVELOPMENT", "DISPLAY", "GST_DEBUG_DUMP_DOT_DIR",
+                ("GST_PRESET_PATH",
+                 "/app/share/gstreamer-1.0/presets/:/app/share/pitivi/gstpresets/"),
+                ("GST_ENCODING_TARGET_PATH",
+                 "/app/share/gstreamer-1.0/presets/:/app/share/pitivi/gstpresets/")]:
+            if not isinstance(envvar, str):
+                value = envvar[1] + ":" + os.environ.get(envvar[0], "")
+                envvar = envvar[0]
+            else:
+                value = os.environ.get(envvar)
+
+            if value:
+                env_args.append("--env=%s=%s" % (envvar, value))
+
+        env_args.append(self.prefix)
+
+        if args:
+            env_args.extend(args)
+        else:
+            env_args.append(os.path.join(self.scriptdir, "enter-env"))
+
+        message("Running in sandbox: %s", ' '.join(args))
+        try:
+            subprocess.check_call(env_args, cwd=cwd)
+        except subprocess.CalledProcessError as e:
+            if exit_on_failure:
+                self.exit(e.returncode)
+
+    def install_all(self):
+        for m in self.packs:
+            if not m.is_installed():
+                m.install()
+
+    def update_all(self):
+        for m in self.packs:
+            m.update()
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(
+        prog="pitivi-flatpak")
+
+    general = parser.add_argument_group("General")
+    general.add_argument("-u", "--update", dest="update",
+                         action="store_true",
+                         default=True,
+                         help="Update the runtime/sdk/app and rebuild the development environment if needed")
+
+    # Only used by the installer to keep the Terminal window open on exit.
+    general.add_argument("--installer", dest="installer",
+                         action="store_true",
+                         default=False)
+
+    devel = parser.add_argument_group("Development")
+    devel.add_argument("-d", "--devel", dest="devel",
+                       action="store_true",
+                       help="Setup a devel environment")
+
+    devel.add_argument("--branch", dest="branch",
+                       help="The flatpak branch to use (stable, master...)",
+                       default="master")
+    devel.add_argument("--gst-version", dest="gst_version",
+                       help="The GStreamer version to build.",
+                       default="master")
+    devel.add_argument("--check", dest="check",
+                       help="Run unit tests once the build is done.",
+                       action="store_true")
+    devel.add_argument("-c", "--clean", dest="clean",
+                       action="store_true",
+                       help="Clean previous builds and restart from scratch")
+
+    bundling = parser.add_argument_group("Building bundle for distribution")
+    bundling.add_argument("--bundle", dest="bundle",
+                          action="store_true",
+                          help="Create bundle repository, implies --build")
+
+    bundling.add_argument(
+        "--repo-commit-subject", dest="commit_subject", default=None,
+        help="The commit subject to be used when updating the ostree repository")
+    bundling.add_argument(
+        "--repo-commit-body", dest="commit_body", default=None,
+        help="The commit body to be used when updating the ostree repository")
+    bundling.add_argument(
+        "--gpg-sign", dest="gpg_key", default=None,
+        help="The GPG key to sign the commit with (work only when --bundle is used)")
+    bundling.add_argument(
+        "--generate-static-deltas", dest="generate_static_deltas",
+        action="store_true", default=False,
+        help="Generate static deltas (check 'man flatpak-build-update-repo'"
+        " for more informations")
+
+    app_flatpak = PitiviFlatpak()
+    flatpak_args = parser.parse_known_args(namespace=app_flatpak)[1]
+    app_flatpak.clean_args(flatpak_args)
+    app_flatpak.run()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]