pgfw/pgfw/Setup.py

132 lines
4.7 KiB
Python
Raw Normal View History

2012-07-05 04:21:49 -04:00
from os import walk, remove
2012-07-09 03:02:52 -04:00
from os.path import sep, join, exists, normpath
from re import findall, sub
from distutils.core import setup
from distutils.command.install import install
from pprint import pprint
from fileinput import FileInput
from re import sub, match
2012-07-05 04:21:49 -04:00
2012-07-16 04:56:04 -04:00
from Configuration import *
2012-07-05 04:21:49 -04:00
class Setup:
2012-07-09 03:02:52 -04:00
config = Configuration()
manifest_path = "MANIFEST"
2012-07-05 04:21:49 -04:00
def __init__(self):
pass
2012-07-05 04:21:49 -04:00
def remove_old_mainfest(self):
path = self.manifest_path
2012-07-05 04:21:49 -04:00
if exists(path):
remove(path)
def build_package_list(self):
packages = []
config = self.config.get_section("setup")
locations = [config["package-root"]] + config["additional-packages"]
2012-08-25 00:46:05 -04:00
for location in locations:
if exists(location):
for root, dirs, files in walk(location, followlinks=True):
packages.append(root.replace(sep, "."))
2012-07-05 04:21:49 -04:00
return packages
def build_data_map(self):
include = []
config = self.config.get_section("setup")
2012-07-09 03:02:52 -04:00
exclude = map(normpath, config["data-exclude"])
2012-07-05 04:21:49 -04:00
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["installation-path"],
root)), files))
2012-07-05 04:21:49 -04:00
return include
def remove_excluded(self, paths, root, exclude):
2012-07-09 03:02:52 -04:00
removal = []
for path in paths:
if normpath(join(root, path)) in exclude:
removal.append(path)
for path in removal:
paths.remove(path)
return paths
2012-07-09 03:02:52 -04:00
2012-07-05 04:21:49 -04:00
def translate_title(self):
config = self.config.get_section("setup")
title = config["title"].replace(" ", config["whitespace-placeholder"])
return sub("[^\w-]", config["special-char-placeholder"], title)
2012-07-05 04:21:49 -04:00
def build_description(self):
description = ""
path = self.config.get("setup", "description-file")
if exists(path):
2012-12-23 05:20:36 -05:00
description = "\n%s\n%s\n%s" % (file(path).read(),
"Changelog\n=========",
self.translate_changelog())
return description
2012-07-05 04:21:49 -04:00
def translate_changelog(self):
translation = ""
path = self.config.get("setup", "changelog")
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"
2012-07-05 04:21:49 -04:00
return translation
def setup(self, windows=[], options={}):
print "running setup..."
self.remove_old_mainfest()
config = self.config.get_section("setup")
scripts = []
if config["init-script"]:
scripts.append(config["init-script"])
setup(cmdclass={"install": insert_resources_path},
name=self.translate_title(),
packages=self.build_package_list(),
scripts=scripts,
data_files=self.build_data_map(),
requires=config["requirements"],
version=config["version"],
description=config["summary"],
classifiers=config["classifiers"],
long_description=self.build_description(),
license=config["license"],
platforms=config["platforms"],
author=config["contact-name"],
author_email=config["contact-email"],
url=config["url"],
windows=windows,
options=options)
class insert_resources_path(install):
def run(self):
install.run(self)
self.edit_game_object_file()
def edit_game_object_file(self):
config = Configuration().get_section("setup")
for path in self.get_outputs():
if path.endswith(config["main-object"]):
for line in FileInput(path, inplace=True):
pattern = "^ *{0} *=.*".\
format(config["resources-path-identifier"])
if match(pattern, line):
line = sub("=.*$", "= \"{0}\"".\
format(config["installation-path"]), line)
print line.strip("\n")