What is the pythonic way to pack a step-by-step web application?

I have a Python based web application that I am trying to package as a setuptools package so that it can be installed using pipand / or python setup.py xxxxx. This web application also contains static files for the React frontend. I am using webpack (and therefore node.js ) to create a JavaScript package for a website. I am trying to understand the most pythonic way of packing it. From a small number of search queries, I found nodeenvone that seems relevant.

Ideally, I would like this package to have the following features:

  • When installing with pip installor, python setup.py installit should not install nodeand webpack, but the installed package should include the output of webpack.

  • The output- webpackgenerated should not be checked in the original repo. (that is, it will need to be generated at some point in the packaging process).

  • When a package is configured for development through pip install -eor python setup.py develop, it must install nodeand webpack(I suspect the above nodeenvwill be useful in this regard.) Must also run webpackat this time so that subsequently- webpackgenerated content exists.

  • If it was easy, it would also be great if it webpackcould be started in watch mode when virtualenv is activated, and stopped when it is deactivated (but this is a fully stretched target.)

My guess, given these requirements, is that I will need to subclass the command sdistin order to force the output of the web package to generate when the source is generated. I also suggest that I need to subclass the team developto only embed development requirements.

It seems to be a bridge that someone used to cross. Does anyone have pointers?

+4
source share
3 answers

, , , ( , node, npm virtualenv )

  • python virtualenv.
  • webpack npm, webpack script.
  • webpack, .

, Makefile script ( Fabric, python), :

  • python-requirements
  • node-requirements
  • build-static
  • buildpython-requirements, node-requirements, build-static

! , , , build, .

0

, : node , webpack . ...

, , setup.py , , , dir .

  • , docker, , nodejs npm , curl... && apt-get .. Dockerfile.

  • sdist run npm install webpack .

, setup.py

setup(
    name='myapp',
    ...
    cmdclass={'sdist': MySdistCommand}
    ...)

MySdistCommand

from setuptools.command.sdist import sdist

class MySdistCommand(sdist):
   def run(self):
       import subprocess
       subprocess.check_call(['npm', 'install'])
       subprocess.check_call(['./node_modules/.bin/webpack', '-p'])
       sdist.run(self)

, , . , , prod ( + ). , , -, dev.:-D

0

( ) . , - this. script, , webpack.

, , :) , setup.py, setup.py install develop, npm install - .

, :

entry_points={
    "console_scripts": [
        "mywebpack=script_build:main",
    ]
},

script

from os.path import exists, dirname, join
from subprocess import Popen
import sys


def main():

    # Find the path of the script
    path = dirname(__file__)

    # Get the arguments that should pass to webpack
    args = sys.argv[1:]

    # Call webpack with the arguments passed to this program
    webpack_invocation = join(path, 'node_modules', '.bin', 'webpack')
    webpack_command = [webpack_invocation] + args
    process = Popen(webpack_invocation, cwd=path, shell=True)
    process.wait()


if __name__ == "__main__":
    main()

mywebpack <options>

, webpack:) , !

0

Source: https://habr.com/ru/post/1609129/


All Articles