# HG changeset patch # User Paul Boddie # Date 1276125025 -7200 # Node ID 533880275f46082b92b9916912a0172ec57e18e8 # Parent 76eca0704c6b01c005284bed8be14082e58ca09e Added extension package installation and stylesheet integration, adjusting the stylesheet import regular expression. Added an error reporting function. diff -r 76eca0704c6b -r 533880275f46 moinsetup.py --- a/moinsetup.py Tue Jun 08 00:59:33 2010 +0200 +++ b/moinsetup.py Thu Jun 10 01:10:25 2010 +0200 @@ -19,7 +19,7 @@ moin_cgi_fix_script_name = compile_definition("fix_script_name") moin_cgi_force_cgi = re.compile("^#(os.environ\['FCGI_FORCE_CGI'\].*)$", re.MULTILINE) -css_import_stylesheet = re.compile("^(\s*@import\s+[\"'])(.*?)([\"'].*)$", re.MULTILINE) +css_import_stylesheet = re.compile("(\s*@import\s+[\"'])(.*?)([\"']\s*;)") # Templates for Apache site definitions. @@ -66,8 +66,8 @@ def status(message): print message -def note(message): - print message +note = status +error = status class Configuration: @@ -304,6 +304,26 @@ if not exists(d): os.makedirs(d) + def get_theme_directories(self, theme_name=None): + + """ + Return tuples of the form (theme name, theme directory) for all themes, + or for a single theme if the optional 'theme_name' is specified. + """ + + filenames = theme_name and [theme_name] or os.listdir(self.htdocs_dir) + directories = [] + + for filename in filenames: + theme_dir = join(self.htdocs_dir, filename) + + if not exists(theme_dir) or not isdir(theme_dir): + continue + + directories.append((filename, theme_dir)) + + return directories + # Main methods. def setup(self): @@ -643,6 +663,15 @@ if exists(css_file_path): shutil.copy(css_file_path, target_dir) + def install_extension_package(self, extension_dir): + + "Install any libraries from 'extension_dir' using a setup script." + + this_dir = os.getcwd() + os.chdir(extension_dir) + os.system("python setup.py install --prefix=%s" % self.prefix) + os.chdir(this_dir) + def install_plugins(self, plugins_dir, plugin_type): """ @@ -679,15 +708,9 @@ specified resources. """ - # Copy the resources. - - filenames = theme_name and [theme_name] or os.listdir(self.htdocs_dir) + for theme_name, theme_dir in self.get_theme_directories(theme_name): - for filename in filenames: - theme_dir = join(self.htdocs_dir, filename) - - if not exists(theme_dir) or not isdir(theme_dir): - continue + # Copy the resources. copied = 0 @@ -705,6 +728,82 @@ if copied: status("Copied theme resources into %s..." % theme_dir) + def edit_theme_stylesheet(self, theme_stylesheet, imported_stylesheet, action="ensure", theme_name=None): + + """ + Edit the given 'theme_stylesheet', ensuring (or removing) a reference to + the 'imported_stylesheet' according to the given 'action' (optional, + defaulting to "ensure"). If a specific 'theme_name' is given, only that + theme will be affected. + """ + + if action == "ensure": + ensure = 1 + elif action == "remove": + ensure = 0 + else: + error("Action %s not valid: it must be given as either 'ensure' or 'remove'." % action) + return + + for theme_name, theme_dir in self.get_theme_directories(theme_name): + + # Locate the resources. + + css_dir = join(theme_dir, "css") + + if not exists(css_dir): + continue + + theme_stylesheet_filename = join(css_dir, theme_stylesheet) + imported_stylesheet_filename = join(css_dir, imported_stylesheet) + + if not exists(theme_stylesheet_filename): + error("Stylesheet %s not defined in theme %s." % (theme_stylesheet, theme_name)) + continue + + if not exists(imported_stylesheet_filename): + error("Stylesheet %s not defined in theme %s." % (imported_stylesheet, theme_name)) + continue + + # Edit the resources. + + s = readfile(theme_stylesheet_filename) + after_point = 0 + + for stylesheet_import in css_import_stylesheet.finditer(s): + before, filename, after = stylesheet_import.groups() + before_point, after_point = stylesheet_import.span() + + # Test the import for a reference to the requested imported + # stylesheet. + + if filename == imported_stylesheet: + if ensure: + break + else: + if s[after_point:after_point+1] == "\n": + after_point += 1 + s = "%s%s" % (s[:before_point], s[after_point:]) + + status("Removing %s from %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) + writefile(theme_stylesheet_filename, s) + break + + # Where no import references the imported stylesheet, insert a + # reference into the theme stylesheet. + + else: + if ensure: + + # Assume that the stylesheet can follow other imports. + + if s[after_point:after_point+1] == "\n": + after_point += 1 + s = "%s%s\n%s" % (s[:after_point], '@import "%s";' % imported_stylesheet, s[after_point:]) + + status("Adding %s to %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) + writefile(theme_stylesheet_filename, s) + # Command line option syntax. syntax_description = " ... --method=METHOD [ ... ]"