Files
django-test/setup.py
2019-10-08 18:35:48 +02:00

128 lines
4.3 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import shutil
import sys
from setuptools import setup, find_packages
from setuptools import Command
class MyCommand(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
class CreatePythonEnvironment(MyCommand):
description = 'create a (virtual) python environment'
@staticmethod
def run():
python_bin = sys.executable if sys.executable else 'python'
python_ver = sys.version_info.major
if python_ver == 2:
# path = os.path.join('env', 'python2')
path = 'pythonenv'
# symlink_path = os.path.join('env', 'python')
symlink_path = ''
venv_module = 'virtualenv'
prompt = '(py2-django) '
elif python_ver == 3:
# path = os.path.join('env', 'python3')
path = 'pythonenv'
# symlink_path = os.path.join('env', 'python')
symlink_path = ''
venv_module = 'venv'
prompt = 'py3-django'
else:
sys.stderr.write('Python {} is not supported.\n'.format(python_ver))
sys.exit(os.EX_USAGE)
sys.stdout.write('Creating new python environment in {path}\n'.format(path=path))
cmd = ('{bin} -m {venv_module}'
' --prompt="{prompt}"'
' {path}'.format(bin=python_bin, path=path,
venv_module=venv_module, prompt=prompt))
os.system(cmd)
if symlink_path and symlink_path != path and not os.path.exists(symlink_path):
symlink_dir = os.path.dirname(symlink_path)
relpath = os.path.relpath(path, symlink_dir)
os.symlink(relpath, symlink_path)
print('')
print('Depending on your operating system or command shell,')
print('you should activate the new environment for this shell session')
print('by running ONE of the following commands:')
print('- Windows: %s' % os.path.join(path, 'Scripts', 'activate'))
print('- C Shell: source %s/bin/activate.csh' % path)
print('- All others: source %s/bin/activate' % path)
class SetupDjangoProject(MyCommand):
description = 'setup and configure the django project'
@staticmethod
def run():
# python_bin = sys.executable if sys.executable else 'python'
mgmt_script = 'manage.py'
settings_module_name = 'conf'
settings_file = os.path.join(settings_module_name, 'settings.py')
if os.path.exists(settings_file):
sys.stderr.write('Django settings file detected. Abort!\n')
return os.EX_NOPERM
if os.path.exists(mgmt_script) or os.path.exists(settings_module_name):
sys.stdout.write('Replacing django files...\n')
if os.path.exists(mgmt_script):
os.unlink(mgmt_script)
if os.path.exists(settings_module_name):
shutil.rmtree(settings_module_name)
else:
sys.stdout.write('Installing django files...\n')
cmd = 'django-admin startproject {name} .'.format(name=settings_module_name)
exitval = os.system(cmd)
if exitval != os.EX_OK:
return exitval
sys.stdout.write('Configure django...\n')
add_settings_file = os.path.join('etc', 'django', 'additional_settings.py')
with open(settings_file, 'a') as f_out:
with open(add_settings_file) as f_in:
f_out.write(f_in.read())
sys.stdout.write('Creating directories...\n')
dirs = [
os.path.join('var', 'db'),
os.path.join('var', 'log'),
os.path.join('var', 'www', 'static'),
]
for d in dirs:
sys.stdout.write(' - %s\n' % d)
if not os.path.exists(d):
os.makedirs(d)
setup(
name='django-test',
version='2.0.dev0',
description='An example django based web application.',
url='https://heinzelwelt.de',
maintainer='Jens Kleineheismann',
maintainer_email='heinzel@heinzelwelt.de',
cmdclass={
'python': CreatePythonEnvironment,
'django': SetupDjangoProject,
},
packages=find_packages(),
include_package_data=True,
)