[PATCH 4 of 4] setup: add gearbox command to setup front-end

Thomas De Schampheleire thomas.de_schampheleire at nokia.com
Thu Aug 30 20:42:08 UTC 2018


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1535658206 -7200
#      Thu Aug 30 21:43:26 2018 +0200
# Node ID 2f34d029eaa9a39da1641dbefdd9f0da7c67c9a6
# Parent  fe061085789cd4a342891e990023cffcf9ef1ff2
setup: add gearbox command to setup front-end

Kallithea is under the GPL license, and we can thus only distribute any
generated code if we also ship the corresponding source.

We are moving towards a web front-end that use npm to download and compile
various open source components. The components might not be GPL, but if we
distribute any parts of their code (compiled or converted to other
representation), then we also must distribute the corresponding source under
the GPL.

It doesn't seem feasible for us to distribute the source of everything that
npm downloads and includes when we are building. It thus also doesn't seem
feasible for us to build and ship the compiled (possibly minified) front-end
code. Instead, we have to make it as smooth as possible for our users to
get up and running.

It doesn't seem feasible for us to ship or install npm. We must assume it is
available. That requirement must be documented clearly, and we must recommend
how to install npm for the most common platforms.

We could perhaps just document what manual steps to run. Kallithea doesn't
work out of the box anyway - it has to be configured and initialized. Extra
steps might not be a big problem.

Another approach is to call out to npm while pip is installing Kallithea and
download the requirements and build the files. It can be done by customizing
setuptools commands in setup.py. But: Python packaging is fragile. Even
though we only support pip, it really isn't built for things like this.
Custom output is muted and buffered and only shown if running with -v or the
command fails. And pip and setup.py can be used to build and install in so
many ways that we probably can't make it work reliably with all ways of
installing Kallithea.

The approach implemented by this commit is to add a custom gearbox command
'setup-frontend' to run the required commands. This single user-facing
command can internally run various steps as needed. The only current
requirement is the presence of npm and an internet connection.

For now, this will just create/update style.css ... but currently probably
without any actual changes. The files created by npm (and the node_modules
directory) must *not* be a part of the release package made with 'setup.py
sdist'.

[Thomas De Schampheleire:
- update commit message
- convert initial implementation of using setup.py into a custom gearbox cmd
- update docs]

diff --git a/docs/contributing.rst b/docs/contributing.rst
--- a/docs/contributing.rst
+++ b/docs/contributing.rst
@@ -37,10 +37,9 @@ To get started with Kallithea developmen
         pip install --upgrade pip setuptools
         pip install --upgrade -e .
         pip install --upgrade -r dev_requirements.txt
-        npm install     # install dependencies - both tools and data
-        npm run less    # for generating css from less
         gearbox make-config my.ini
         gearbox setup-db -c my.ini --user=user --email=user at example.com --password=password --repos=/tmp
+        gearbox setup-frontend
         gearbox serve -c my.ini --reload &
         firefox http://127.0.0.1:5000/
 
diff --git a/docs/overview.rst b/docs/overview.rst
--- a/docs/overview.rst
+++ b/docs/overview.rst
@@ -69,11 +69,9 @@ installed.
   (``pip install kallithea`` from a source tree will do pretty much the same
   but build the Kallithea package itself locally instead of downloading it.)
 
-.. note:: The front-end code is built with Node. Currently, it must be built
-          locally after installing Kallithea. Assuming Node and the Node
-          Package Manager is available, other tools and source code will be
-          downloaded and installed. The front-end code can then be built from
-          source locally.
+.. note:: Kallithea includes front-end code that needs to be processed first.
+The tool npm_ is used to download external dependencies and orchestrate the
+processing. The ``npm`` binary must thus be available.
 
 
 Web server
@@ -144,3 +142,4 @@ continuous hammering from the internet.
 .. _WSGI: http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface
 .. _HAProxy: http://www.haproxy.org/
 .. _Varnish: https://www.varnish-cache.org/
+.. _npm: https://www.npmjs.com/
diff --git a/docs/setup.rst b/docs/setup.rst
--- a/docs/setup.rst
+++ b/docs/setup.rst
@@ -5,22 +5,6 @@ Setup
 =====
 
 
-Preparing front-end
--------------------
-
-Temporarily, in the current Kallithea version, some extra steps are required to
-build front-end files:
-
-Find the right ``kallithea/public/less`` path with::
-
-    python -c "import os, kallithea; print os.path.join(os.path.dirname(os.path.abspath(kallithea.__file__)), 'public', 'less')"
-
-Then run::
-
-    npm install
-    npm run less
-
-
 Setting up Kallithea
 --------------------
 
@@ -70,6 +54,10 @@ path to the root).
           but when trying to do a push it will fail with permission
           denied errors unless it has write access.
 
+Finally, prepare the front-end by running::
+
+    gearbox setup-frontend
+
 You are now ready to use Kallithea. To run it simply execute::
 
     gearbox serve -c my.ini
diff --git a/docs/upgrade.rst b/docs/upgrade.rst
--- a/docs/upgrade.rst
+++ b/docs/upgrade.rst
@@ -86,18 +86,7 @@ If you originally installed from version
     cd my-kallithea-clone
     hg pull -u
     pip install --upgrade -e .
-
-Temporarily, in the current version, an extra step is required to build
-front-end files:
-
-Find the right ``kallithea/public/less`` path with::
-
-    python -c "import os, kallithea; print os.path.join(os.path.dirname(os.path.abspath(kallithea.__file__)), 'public', 'less')"
-
-Then run::
-
-    npm install
-    npm run less
+    gearbox setup-frontend
 
 
 5. Upgrade your configuration
diff --git a/docs/usage/customization.rst b/docs/usage/customization.rst
--- a/docs/usage/customization.rst
+++ b/docs/usage/customization.rst
@@ -28,13 +28,10 @@ directory, you can use this file to over
 you can use this to override ``@kallithea-theme-main-color``,
 ``@kallithea-logo-url`` or other `Bootstrap variables`_.
 
-After creating the ``theme.less`` file, you need to regenerate the CSS files.
-Install npm for your platform and run::
+After creating the ``theme.less`` file, you need to regenerate the CSS files, by
+running::
 
-    npm install
-    npm run less
-
-in the Kallithea root directory.
+    gearbox setup-frontend
 
 .. _bootstrap 3: https://getbootstrap.com/docs/3.3/
 .. _bootstrap variables: https://getbootstrap.com/docs/3.3/customize/#less-variables
diff --git a/kallithea/lib/paster_commands/setup_frontend.py b/kallithea/lib/paster_commands/setup_frontend.py
new file mode 100644
--- /dev/null
+++ b/kallithea/lib/paster_commands/setup_frontend.py
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import subprocess
+
+import kallithea
+from kallithea.lib.paster_commands.common import BasePasterCommand
+
+class Command(BasePasterCommand):
+    """Kallithea: setup the front-end (JS and CSS)"""
+
+    takes_config_file = False
+    requires_db_session = False
+
+    def take_action(self, args):
+        rootdir = os.path.dirname(os.path.dirname(os.path.abspath(kallithea.__file__)))
+        print "Running 'npm install' to install frontend dependencies from package.json\n"
+        subprocess.check_call(['npm', 'install'], cwd=rootdir)
+        print "\nRunning 'npm run less' to build css"
+        subprocess.check_call(['npm', 'run', 'less'], cwd=rootdir)
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -166,6 +166,7 @@ setuptools.setup(
     make-rcext=kallithea.lib.paster_commands.make_rcextensions:Command
     repo-scan=kallithea.lib.paster_commands.repo_scan:Command
     setup-db=kallithea.lib.paster_commands.setup_db:Command
+    setup-frontend=kallithea.lib.paster_commands.setup_frontend:Command
     update-repoinfo=kallithea.lib.paster_commands.update_repoinfo:Command
     upgrade-db=kallithea.lib.dbmigrate:UpgradeDb
     """,


More information about the kallithea-general mailing list