124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
from os import walk, remove
|
|
from os.path import sep, join, exists, normpath
|
|
from re import findall
|
|
from distutils.core import setup
|
|
from distutils.command.install import install
|
|
from pprint import pprint
|
|
from fileinput import FileInput
|
|
from re import sub, match
|
|
|
|
from configuration.Configuration import *
|
|
|
|
class Setup:
|
|
|
|
config = Configuration()
|
|
|
|
def remove_old_mainfest(self):
|
|
path = "MANIFEST"
|
|
if exists(path):
|
|
remove(path)
|
|
|
|
def build_package_list(self):
|
|
packages = []
|
|
package_root = self.config.get("setup", "package-root", "path")
|
|
if exists(package_root):
|
|
for root, dirs, files in walk(package_root, followlinks=True):
|
|
packages.append(root.replace(sep, "."))
|
|
return packages
|
|
|
|
def build_data_map(self):
|
|
include = []
|
|
config = self.config
|
|
exclude = map(normpath, config.get("setup", "data-exclude", "list"))
|
|
for root, dirs, files in walk("."):
|
|
dirs = self.remove_excluded(dirs, root, exclude)
|
|
files = [join(root, f) for f in self.remove_excluded(files, root,
|
|
exclude)]
|
|
if files:
|
|
include.append((normpath(join(config.get("setup",
|
|
"installation-path",
|
|
"path"), root)),
|
|
files))
|
|
return include
|
|
|
|
def remove_excluded(self, paths, root, exclude):
|
|
removal = []
|
|
for path in paths:
|
|
if normpath(join(root, path)) in exclude:
|
|
removal.append(path)
|
|
for path in removal:
|
|
paths.remove(path)
|
|
return paths
|
|
|
|
def translate_title(self):
|
|
return self.config.get("setup", "title").replace(" ", "-")
|
|
|
|
def build_description(self):
|
|
description = ""
|
|
path = self.config.get("setup", "description-file", "path")
|
|
if exists(path):
|
|
description = "\n{0}\n{1}\n{2}".format(file(path).read(),
|
|
"Changelog\n=========",
|
|
self.translate_changelog())
|
|
return description
|
|
|
|
def translate_changelog(self):
|
|
translation = ""
|
|
path = self.config.get("setup", "changelog", "path")
|
|
if exists(path):
|
|
lines = file(path).readlines()
|
|
package_name = lines[0].split()[0]
|
|
for line in lines:
|
|
line = line.strip()
|
|
if line.startswith(package_name):
|
|
version = findall("\((.*)\)", line)[0]
|
|
translation += "\n%s\n%s\n" % (version, "-" * len(version))
|
|
elif line and not line.startswith("--"):
|
|
if line.startswith("*"):
|
|
translation += line + "\n"
|
|
else:
|
|
translation += " " + line + "\n"
|
|
return translation
|
|
|
|
def setup(self):
|
|
config = self.config
|
|
section = config.get_section("setup")
|
|
setup(cmdclass={"install": insert_resources_path},
|
|
name=self.translate_title(),
|
|
packages=self.build_package_list(),
|
|
scripts=[config.get("setup", "init-script", "path")],
|
|
data_files=self.build_data_map(),
|
|
requires=config.get("setup", "requirements", "list"),
|
|
version=section["version"],
|
|
description=section["summary"],
|
|
classifiers=config.get("setup", "classifiers", "list"),
|
|
long_description=self.build_description(),
|
|
license=section["license"],
|
|
platforms=config.get("setup", "platforms", "list"),
|
|
author=section["contact-name"],
|
|
author_email=section["contact-email"],
|
|
url=section["url"])
|
|
|
|
|
|
class insert_resources_path(install):
|
|
|
|
def run(self):
|
|
install.run(self)
|
|
self.edit_game_object_file()
|
|
|
|
def edit_game_object_file(self):
|
|
config = Configuration()
|
|
section = "setup"
|
|
for path in self.get_outputs():
|
|
if path.endswith(config.get(section, "main-object", "path")):
|
|
for line in FileInput(path, inplace=True):
|
|
pattern = "^ *{0} *=.*".\
|
|
format(config.get(section,
|
|
"resources-path-identifier"))
|
|
if match(pattern, line):
|
|
line = sub("=.*$", "= \"{0}\"".\
|
|
format(config.get(section,
|
|
"installation-path",
|
|
"path"), line)
|
|
print line.strip("\n")
|