Skip to content

Commit 0e88d07

Browse files
committed
Refactor setup.py.
1 parent 02a85c0 commit 0e88d07

File tree

6 files changed

+316
-351
lines changed

6 files changed

+316
-351
lines changed

NEWS.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
NEWS
2+
====
3+
4+
This file describes user-visible changes to Roundup.
5+
6+
Roundup 1.4.6
7+
=============
8+
9+
This is a bugfix release:
10+
11+
- Fix bug introduced in 1.4.5 in RDBMS full-text indexing
12+
- Make URL matching code less matchy

roundup/dist/command/bdist_rpm.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright (C) 2009 Stefan Seefeld
3+
# All rights reserved.
4+
# For license terms see the file COPYING.txt.
5+
#
6+
from distutils.command.bdist_rpm import bdist_rpm as base
7+
from distutils.file_util import write_file
8+
import os
9+
10+
class bdist_rpm(base):
11+
12+
def finalize_options(self):
13+
base.finalize_options(self)
14+
if self.install_script:
15+
# install script is overridden. skip default
16+
return
17+
# install script option must be file name.
18+
# create the file in rpm build directory.
19+
install_script = os.path.join(self.rpm_base, "install.sh")
20+
self.mkpath(self.rpm_base)
21+
self.execute(write_file, (install_script, [
22+
("%s setup.py install --root=$RPM_BUILD_ROOT "
23+
"--record=ROUNDUP_FILES") % self.python,
24+
# allow any additional extension for man pages
25+
# (rpm may compress them to .gz or .bz2)
26+
# man page here is any file
27+
# with single-character extension
28+
# in man directory
29+
"sed -e 's,\(/man/.*\..\)$,\\1*,' "
30+
"<ROUNDUP_FILES >INSTALLED_FILES",
31+
]), "writing '%s'" % install_script)
32+
self.install_script = install_script
33+

roundup/dist/command/build.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# Copyright (C) 2009 Stefan Seefeld
3+
# All rights reserved.
4+
# For license terms see the file COPYING.txt.
5+
#
6+
from roundup import msgfmt
7+
from distutils.command.build import build as base
8+
import os
9+
from glob import glob
10+
11+
def list_message_files(suffix=".po"):
12+
"""Return list of all found message files and their intallation paths"""
13+
_files = glob("locale/*" + suffix)
14+
_list = []
15+
for _file in _files:
16+
# basename (without extension) is a locale name
17+
_locale = os.path.splitext(os.path.basename(_file))[0]
18+
_list.append((_file, os.path.join(
19+
"share", "locale", _locale, "LC_MESSAGES", "roundup.mo")))
20+
return _list
21+
22+
def check_manifest():
23+
"""Check that the files listed in the MANIFEST are present when the
24+
source is unpacked.
25+
"""
26+
try:
27+
f = open('MANIFEST')
28+
except:
29+
print '\n*** SOURCE WARNING: The MANIFEST file is missing!'
30+
return
31+
try:
32+
manifest = [l.strip() for l in f.readlines()]
33+
finally:
34+
f.close()
35+
err = [line for line in manifest if not os.path.exists(line)]
36+
err.sort()
37+
# ignore auto-generated files
38+
if err == ['roundup-admin', 'roundup-demo', 'roundup-gettext',
39+
'roundup-mailgw', 'roundup-server']:
40+
err = []
41+
if err:
42+
n = len(manifest)
43+
print '\n*** SOURCE WARNING: There are files missing (%d/%d found)!'%(
44+
n-len(err), n)
45+
print 'Missing:', '\nMissing: '.join(err)
46+
47+
48+
class build(base):
49+
50+
def build_message_files(self):
51+
"""For each locale/*.po, build .mo file in target locale directory"""
52+
for (_src, _dst) in list_message_files():
53+
_build_dst = os.path.join("build", _dst)
54+
self.mkpath(os.path.dirname(_build_dst))
55+
self.announce("Compiling %s -> %s" % (_src, _build_dst))
56+
msgfmt.make(_src, _build_dst)
57+
58+
def run(self):
59+
check_manifest()
60+
self.build_message_files()
61+
base.run(self)
62+

roundup/dist/command/build_py.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# Copyright (C) 2009 Stefan Seefeld
3+
# All rights reserved.
4+
# For license terms see the file COPYING.txt.
5+
#
6+
from distutils.command.build_py import build_py
7+
8+
class build_py(build_py):
9+
10+
def find_modules(self):
11+
# Files listed in py_modules are in the toplevel directory
12+
# of the source distribution.
13+
modules = []
14+
for module in self.py_modules:
15+
path = module.split('.')
16+
package = '.'.join(path[0:-1])
17+
module_base = path[-1]
18+
module_file = module_base + '.py'
19+
if self.check_module(module, module_file):
20+
modules.append((package, module_base, module_file))
21+
return modules
22+
23+
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#
2+
# Copyright (C) 2009 Stefan Seefeld
3+
# All rights reserved.
4+
# For license terms see the file COPYING.txt.
5+
#
6+
from distutils.command.build_scripts import build_scripts as base
7+
import sys, os, string
8+
9+
class build_scripts(base):
10+
""" Overload the build_scripts command and create the scripts
11+
from scratch, depending on the target platform.
12+
13+
You have to define the name of your package in an inherited
14+
class (due to the delayed instantiation of command classes
15+
in distutils, this cannot be passed to __init__).
16+
17+
The scripts are created in an uniform scheme: they start the
18+
run() function in the module
19+
20+
<packagename>.scripts.<mangled_scriptname>
21+
22+
The mangling of script names replaces '-' and '/' characters
23+
with '-' and '.', so that they are valid module paths.
24+
25+
If the target platform is win32, create .bat files instead of
26+
*nix shell scripts. Target platform is set to "win32" if main
27+
command is 'bdist_wininst' or if the command is 'bdist' and
28+
it has the list of formats (from command line or config file)
29+
and the first item on that list is wininst. Otherwise
30+
target platform is set to current (build) platform.
31+
"""
32+
package_name = 'roundup'
33+
34+
def initialize_options(self):
35+
base.initialize_options(self)
36+
self.script_preamble = None
37+
self.target_platform = None
38+
self.python_executable = None
39+
40+
def finalize_options(self):
41+
base.finalize_options(self)
42+
cmdopt=self.distribution.command_options
43+
44+
# find the target platform
45+
if self.target_platform:
46+
# TODO? allow explicit setting from command line
47+
target = self.target_platform
48+
if cmdopt.has_key("bdist_wininst"):
49+
target = "win32"
50+
elif cmdopt.get("bdist", {}).has_key("formats"):
51+
formats = cmdopt["bdist"]["formats"][1].split(",")
52+
if formats[0] == "wininst":
53+
target = "win32"
54+
else:
55+
target = sys.platform
56+
if len(formats) > 1:
57+
self.warn(
58+
"Scripts are built for %s only (requested formats: %s)"
59+
% (target, ",".join(formats)))
60+
else:
61+
# default to current platform
62+
target = sys.platform
63+
self.target_platfom = target
64+
65+
# for native builds, use current python executable path;
66+
# for cross-platform builds, use default executable name
67+
if self.python_executable:
68+
# TODO? allow command-line option
69+
pass
70+
if target == sys.platform:
71+
self.python_executable = os.path.normpath(sys.executable)
72+
else:
73+
self.python_executable = "python"
74+
75+
# for windows builds, add ".bat" extension
76+
if target == "win32":
77+
# *nix-like scripts may be useful also on win32 (cygwin)
78+
# to build both script versions, use:
79+
#self.scripts = list(self.scripts) + [script + ".bat"
80+
# for script in self.scripts]
81+
self.scripts = [script + ".bat" for script in self.scripts]
82+
83+
# tweak python path for installations outside main python library
84+
if cmdopt.get("install", {}).has_key("prefix"):
85+
prefix = os.path.expanduser(cmdopt['install']['prefix'][1])
86+
version = '%d.%d'%sys.version_info[:2]
87+
self.script_preamble = """
88+
import sys
89+
sys.path.insert(1, "%s/lib/python%s/site-packages")
90+
"""%(prefix, version)
91+
else:
92+
self.script_preamble = ''
93+
94+
def copy_scripts(self):
95+
""" Create each script listed in 'self.scripts'
96+
"""
97+
98+
to_module = string.maketrans('-/', '_.')
99+
100+
self.mkpath(self.build_dir)
101+
for script in self.scripts:
102+
outfile = os.path.join(self.build_dir, os.path.basename(script))
103+
104+
#if not self.force and not newer(script, outfile):
105+
# self.announce("not copying %s (up-to-date)" % script)
106+
# continue
107+
108+
if self.dry_run:
109+
self.announce("would create %s" % outfile)
110+
continue
111+
112+
module = os.path.splitext(os.path.basename(script))[0]
113+
module = string.translate(module, to_module)
114+
script_vars = {
115+
'python': self.python_executable,
116+
'package': self.package_name,
117+
'module': module,
118+
'prefix': self.script_preamble,
119+
}
120+
121+
self.announce("creating %s" % outfile)
122+
file = open(outfile, 'w')
123+
124+
try:
125+
# could just check self.target_platform,
126+
# but looking at the script extension
127+
# makes it possible to build both *nix-like
128+
# and windows-like scripts on win32.
129+
# may be useful for cygwin.
130+
if os.path.splitext(outfile)[1] == ".bat":
131+
file.write('@echo off\n'
132+
'if NOT "%%_4ver%%" == "" "%(python)s" -c "from %(package)s.scripts.%(module)s import run; run()" %%$\n'
133+
'if "%%_4ver%%" == "" "%(python)s" -c "from %(package)s.scripts.%(module)s import run; run()" %%*\n'
134+
% script_vars)
135+
else:
136+
file.write('#! %(python)s\n%(prefix)s'
137+
'from %(package)s.scripts.%(module)s import run\n'
138+
'run()\n'
139+
% script_vars)
140+
finally:
141+
file.close()
142+
os.chmod(outfile, 0755)

0 commit comments

Comments
 (0)