This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
monolithic.py
77 lines (59 loc) · 2.33 KB
/
monolithic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
# This file is part of the Rocket Web Server
# Copyright (c) 2009 Timothy Farrell
"""\
monolithic.py is a module that contains the distutils add-in for creating
a monolithic source module of the Rocket web server. To use get a monolithic
Rocket module first install Rocket normally. Then run::
setup.py build_monolithic
The resulting monolithic module will be in the build/monolithic/ subdirectory.
"""
import os
import re
from glob import glob
from distribute_setup import use_setuptools
use_setuptools()
from setuptools import find_packages
from distutils.core import Command
package_imports = re.compile(r'^(\s*from \.[\w\.]* import .*( as \w+)?)$', re.I | re.M)
class build_monolithic(Command):
user_options = []
description = "Create a monolithic (one-file) source module."
def initialize_options (self):
self.files = []
def finalize_options (self):
packages = find_packages()
packages.remove('tests')
for p in packages:
self.files += sorted(glob(os.sep.join(p.split('.')) + os.sep + '*.py'))
def run(self):
build = self.get_finalized_command('build')
filepath = os.path.join(build.build_base, 'monolithic', 'rocket.py')
if os.path.exists(filepath):
os.unlink(filepath)
else:
os.makedirs(os.path.dirname(filepath))
out = open(filepath, 'w')
show_copyright = True
for filename in self.files:
f = open(filename, 'r')
filedata = f.readlines()
f.close()
if show_copyright:
filedata = ''.join(filedata)
show_copyright = False
else:
filedata = ''.join(filedata[4:])
out.write("# Monolithic build...start of module: %s\n" % filename)
i = 0
templist = []
showImportNotice = True
for item in package_imports.finditer(filedata, i):
out.write(filedata[i:item.start()])
if showImportNotice:
out.write('# package imports removed in monolithic build')
showImportNotice = False
i = item.end()
out.write(filedata[i:len(filedata)])
out.write("\n# Monolithic build...end of module: %s\n" % filename)
out.close()