[PATCH 15 of 17 v3] cli: add command 'kallithea-cli front-end-create'

Thomas De Schampheleire patrickdepinguin at gmail.com
Thu Oct 18 20:49:38 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1537814676 -7200
#      Mon Sep 24 20:44:36 2018 +0200
# Node ID 1cfd34ff0aa47e1ff21b91d683a1e6937c8d8417
# Parent  9070ecd3538890d6af539570f6e48ce0ee15d2f7
cli: add command 'kallithea-cli front-end-create'

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 cli command
'front-end-create' 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'.

(Commit message is mostly written by Mads Kiilerich)

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
         kallithea-cli config-create my.ini
         kallithea-cli db-create --user=user --email=user at example.com --password=password --repos=/tmp my.ini
+        kallithea-cli front-end-create
         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::
+
+    kallithea-cli front-end-create
+
 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
+    kallithea-cli front-end-create
 
 
 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.
+    kallithea-cli front-end-create --no-install-deps
 
 .. _bootstrap 3: https://getbootstrap.com/docs/3.3/
 .. _bootstrap variables: https://getbootstrap.com/docs/3.3/customize/#less-variables
diff --git a/kallithea/bin/kallithea_cli.py b/kallithea/bin/kallithea_cli.py
--- a/kallithea/bin/kallithea_cli.py
+++ b/kallithea/bin/kallithea_cli.py
@@ -21,6 +21,7 @@ import kallithea.bin.kallithea_cli_cache
 import kallithea.bin.kallithea_cli_celery
 import kallithea.bin.kallithea_cli_config
 import kallithea.bin.kallithea_cli_extensions
+import kallithea.bin.kallithea_cli_front_end
 import kallithea.bin.kallithea_cli_iis
 import kallithea.bin.kallithea_cli_index
 import kallithea.bin.kallithea_cli_ishell
diff --git a/kallithea/bin/kallithea_cli_front_end.py b/kallithea/bin/kallithea_cli_front_end.py
new file mode 100644
--- /dev/null
+++ b/kallithea/bin/kallithea_cli_front_end.py
@@ -0,0 +1,50 @@
+# -*- 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 click
+from kallithea.bin.kallithea_cli_base import cli
+
+import os
+import subprocess
+
+import kallithea
+
+ at cli.command()
+ at click.option('--install-deps/--no-install-deps', default=True,
+        help='Skip installation of dependencies, via "npm".')
+ at click.option('--generate/--no-generate', default=True,
+        help='Skip generation of front-end files.')
+def front_end_create(install_deps, generate):
+    """Create the front-end.
+
+    Install required dependencies for the front-end and generate the necessary
+    files.  This step is complementary to any 'pip install' step which only
+    covers Python dependencies.
+
+    The installation of front-end dependencies happens via the tool 'npm' which
+    is expected to be installed already.
+    """
+    rootdir = os.path.dirname(os.path.dirname(os.path.abspath(kallithea.__file__)))
+
+    if install_deps:
+        click.echo("Running 'npm install' to install front-end dependencies from package.json")
+        subprocess.check_call(['npm', 'install'], cwd=rootdir)
+
+    if generate:
+        click.echo("Generating CSS")
+        lesscpath = os.path.join(rootdir, 'node_modules', '.bin', 'lessc')
+        lesspath = os.path.join(rootdir, 'kallithea', 'public', 'less', 'main.less')
+        csspath = os.path.join(rootdir, 'kallithea', 'public', 'css', 'style.css')
+        subprocess.check_call([lesscpath, '--relative-urls', '--source-map',
+                '--source-map-less-inline', lesspath, csspath])
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -7,8 +7,5 @@
   "devDependencies": {
     "less": "~2.7",
     "less-plugin-clean-css": "~1.5"
-  },
-  "scripts": {
-    "less": "lessc --relative-urls --source-map --source-map-less-inline kallithea/public/less/main.less kallithea/public/css/style.css"
   }
 }


More information about the kallithea-general mailing list