Author: johannes Date: 2007-11-22 07:51:03 -0600 (Thu, 22 Nov 2007) New Revision: 9829
Modified: trunk/gnue-common/src/setup/GSetup.py Log: More work on PEP8 Modified: trunk/gnue-common/src/setup/GSetup.py =================================================================== --- trunk/gnue-common/src/setup/GSetup.py 2007-11-22 10:16:02 UTC (rev 9828) +++ trunk/gnue-common/src/setup/GSetup.py 2007-11-22 13:51:03 UTC (rev 9829) @@ -21,6 +21,7 @@ # # $Id: GSetup.py,v 1.4 2003/10/06 18:33:16 reinhard Exp $ """ +Base classes for package setup based on distutils """ import sys @@ -40,6 +41,8 @@ from gnue.common.utils import version from gnue.common.setup import ChangeLog +__all__ = ['GSetup'] + # ----------------------------------------------------------------------------- # Check Python version # ----------------------------------------------------------------------------- @@ -47,18 +50,19 @@ try: if sys.hexversion < 0x02030000: raise AttributeError + except AttributeError: print "-" * 70 print """ - You are running Python %s. +You are running Python %s. - GNU Enterprise requires at least Python 2.3. - If you have a later version installed, you should run setup.py - against that version. For example, if you have Python 2.3 - installed, you may need to run: +GNU Enterprise requires at least Python 2.3. +If you have a later version installed, you should run setup.py +against that version. For example, if you have Python 2.3 +installed, you may need to run: - python2.3 setup.py - """ % sys.version.split()[0] +python2.3 setup.py +""" % sys.version.split()[0] print "-" * 70 sys.exit(1) @@ -69,28 +73,81 @@ _setup = None # ============================================================================= -# sdist: build files to be distributed first +# SDist: build files to be distributed first # ============================================================================= -class sdist(distutils.command.sdist.sdist): +class SDist(distutils.command.sdist.sdist): + """ + Build files to be distributed + """ + # ------------------------------------------------------------------------- + # Run the sdist command + # ------------------------------------------------------------------------- + def run(self): + """ + Run the sdist command + """ global _setup _setup.do_build_files('sdist') distutils.command.sdist.sdist.run(self) + + # ------------------------------------------------------------------------- + # Remove files not used + # ------------------------------------------------------------------------- + def prune_file_list(self): + """ + Prune the list of files included + """ distutils.command.sdist.sdist.prune_file_list(self) self.filelist.exclude_pattern('*.dist_template', anchor=0) + + # ------------------------------------------------------------------------- + # Create the directory tree that will become the source distribution arch. + # ------------------------------------------------------------------------- + def make_release_tree(self, base_dir, files): + """ + Create the directory tree that will become the source distribution + archive. + """ distutils.command.sdist.sdist.make_release_tree(self, base_dir, files) self.process_templates(base_dir) + _setup.do_build_svnrev(os.path.join(base_dir, 'src', 'svnrev.py')) + + # ------------------------------------------------------------------------- + # Process all template files + # ------------------------------------------------------------------------- + def process_templates(self, target): + """ + Process all distribution template files. Such a template file is + called "<filename>.dist_template" and will be stored as "<filename>" + after processing. This method replaces the following strings by their + corresponding values: + * :PACKAGE: Name of the package, i.e. 'gnue-forms' + * :TITLE: Title of the package, i.e. 'GNU Enterprise Forms' + * :VERSION: Full version of the package, i.e. '0.6.9+svn.9794' + * :MAJOR: The major version, i.e. '0' + * :MINOR: The minor version, i.e. '6' + * :PHASE: The phase, i.e. 'final' + * :BUILD: The build, i.e. '1' + * :SVN: The current SubVersion revision + * :DATE_ISO: The date of the package formatted as YYYY-MM-DD + * :DATE_RFC: The date of the package formatted according to RFC 822 + * :TIME: The time of the package formattes as HH:MM:SS + * :FINAL: If the build is final it contains the build number, + otherwise '0' + """ + # Build list of files to be processed. filelist = FileList() if filelist.include_pattern('*.dist_template', anchor=0) == 0: @@ -101,6 +158,7 @@ # module. Change to unconditional import in gnue-common 0.8. try: from src import version + except: return @@ -117,6 +175,7 @@ ':DATE_ISO:': time.strftime('%Y-%m-%d', time.gmtime()), ':DATE_RFC:': time.strftime('%a, %d %b %Y', time.gmtime()), ':TIME:': time.strftime('%H:%M:%S', time.gmtime())} + # Hack for version numbering schemes that are limited to x.y.z. if version.phase == 'final': keywords[':FINAL:'] = str(version.build) @@ -129,39 +188,65 @@ self.execute(self.__process_template, args, "generating %s from %s" % (dst, src)) + + # ------------------------------------------------------------------------- + # Process a single template + # ------------------------------------------------------------------------- + def __process_template(self, src, dst, keywords): + infile = open(src, 'r') content = infile.read() infile.close() + for keyword, value in keywords.iteritems(): content = content.replace(keyword, value) + outfile = open(dst, 'w') outfile.write(content) outfile.close() + # Let destination file have the same mode than the source file. os.chmod(dst, os.stat(src).st_mode) + # ============================================================================= # build: if done from SVN, build files to be installed first # ============================================================================= -class build(distutils.command.build.build): +class Build(distutils.command.build.build): + """ + Build files to be installed + """ + # ------------------------------------------------------------------------- + # Run the command + # ------------------------------------------------------------------------- + def run(self): + """ + Run the build command + """ global _setup if not os.path.isfile("PKG-INFO"): # downloaded from SVN? _setup.do_build_files('build') + distutils.command.build.build.run(self) + if not os.path.isfile("PKG-INFO"): _setup.do_build_svnrev(os.path.join(self.build_lib, 'gnue', _setup.package[5:].lower(), 'svnrev.py')) + # ============================================================================= # install: Some user_options are no longer allowed # ============================================================================= -class install(distutils.command.install.install): +class Install(distutils.command.install.install): + """ + Install the package + """ # Commented out because sometimes, to create packages, we want to install # other tools in a different target directory than common is installed @@ -172,6 +257,9 @@ #user_options = [opt for opt in user_options if opt [0] in allowed_options] + + # TODO: this code is executed as soon as this module get's importet. + # Need to check wether this is really what we want here. user_options = distutils.command.install.install.user_options i = 0 for option in user_options: @@ -181,19 +269,28 @@ "installation directory for configuration files")) break + # ------------------------------------------------------------------------- # Initalize options # ------------------------------------------------------------------------- def initialize_options(self): + """ + Initialize options + """ + distutils.command.install.install.initialize_options(self) self.install_config = None + # ------------------------------------------------------------------------- # Finalize options - set all install targets # ------------------------------------------------------------------------- def finalize_options(self): + """ + Finalize options and set all install targets + """ if self.install_lib is None: self.install_lib = gnue.paths.lib @@ -206,11 +303,16 @@ distutils.command.install.install.finalize_options(self) + # ------------------------------------------------------------------------- # install.run: generate and install path dependent files afterwards # ------------------------------------------------------------------------- def run(self): + """ + Run the install command + """ + global _setup _setup.check_dependencies() @@ -220,11 +322,11 @@ # install translations if os.path.isdir('po'): # copy files - for f in os.listdir('po'): - if f[-4:] == '.gmo': - src = os.path.join('po', f) - dst = os.path.join(self.install_data, 'share', 'locale', f - [:-4], 'LC_MESSAGES') + for fname in os.listdir('po'): + if fname[-4:] == '.gmo': + src = os.path.join('po', fname) + dst = os.path.join(self.install_data, 'share', 'locale', + fname[:-4], 'LC_MESSAGES') self.mkpath(dst) dst = os.path.join(dst, _setup.package + '.mo') self.copy_file(src, dst) @@ -232,38 +334,70 @@ distutils.command.install.install.run(self) + # ------------------------------------------------------------------------- # install.get_outputs: list all installed files # ------------------------------------------------------------------------- def get_outputs(self): + """ + List all installed files + """ + return distutils.command.install.install.get_outputs(self) \ + self.__outputs + # ============================================================================= # GSetup: Basic class for setup scripts of GNUe packages # ============================================================================= class GSetup: + """ + Base class for setup scripts of GNU Enterprise packages + """ # ------------------------------------------------------------------------- # Abstract methods: setup.py scripts generally override these # ------------------------------------------------------------------------- def set_params(self, params): + """ + Define setup paramters + """ pass + + # ------------------------------------------------------------------------- + # Build list of file + # ------------------------------------------------------------------------- + def build_files(self, action): + """ + Build a list of files depending on a given action + """ pass + + # ------------------------------------------------------------------------- + # Check if all dependencies are met + # ------------------------------------------------------------------------- + def check_dependencies(self): + """ + Check all dependencies + """ pass + # ------------------------------------------------------------------------- # Build files if called from SVN # ------------------------------------------------------------------------- def do_build_files(self, action): + """ + Build all files if called from SVN + """ if os.name == 'posix': @@ -310,14 +444,14 @@ argv0_path = os.path.dirname(os.path.abspath(sys.executable)) sys.path.append(argv0_path + "\\tools\\i18n") - msgfmtOK = 0 + msgfmt_ok = 0 try: import msgfmt - msgfmtOK = 1 + msgfmt_ok = 1 except: pass - if msgfmtOK == 1: + if msgfmt_ok == 1: # pygettext.py exist in Python, but no msgmerge, so # just create a placeholder... potfile = open('po/'+ self.package +'.pot', 'w') @@ -336,33 +470,46 @@ # do package specific stuff self.build_files(action) + # ------------------------------------------------------------------------- - # Build files if called from SVN + # Create the svnrev.py file # ------------------------------------------------------------------------- def do_build_svnrev(self, filename): + """ + Create the file 'svnrev.py' which contains the current SubVersion + revision. + """ log.info("building svnrev.py") output = open(filename, 'w') output.write('svnrev = %r' % version.get_svn_revision('src')) output.close() + # ------------------------------------------------------------------------- # Helper methods for descendants # ------------------------------------------------------------------------- def allfiles(self, directory): + """ + Get a list of files in a given directory, excluding some specials like + Makefile, .svn, CSV and so on + """ + directory = os.path.normpath(directory) exclude = [".svn", "CVS", "README.cvs", ".cvsignore", "Makefile"] - return [directory + "/" + file for file in os.listdir(directory) \ - if not file in exclude and - not os.path.isdir(os.path.join(directory, file))] + return [directory + "/" + fname for fname in os.listdir(directory) \ + if not fname in exclude and + not os.path.isdir(os.path.join(directory, fname))] + # ------------------------------------------------------------------------- # Get all packages in a directory # ------------------------------------------------------------------------- def __get_packages(self, directory, package): + content = os.listdir(directory) result = [] if "__init__.py" in content: @@ -374,44 +521,50 @@ "." + name) return result + # ------------------------------------------------------------------------- # Call the actual setup routine # ------------------------------------------------------------------------- def run(self): + """ + Run the setup routine + """ + global _setup # set global variable _setup = self - setup_params = {"cmdclass_sdist": sdist, - "cmdclass_build": build, - "cmdclass_install": install, - } + setup_params = {"cmdclass_sdist": SDist, + "cmdclass_build": Build, + "cmdclass_install": Install + } _setup.set_params(setup_params) # make package available self.package = setup_params["name"] - # find out all packages if not setup_params.has_key("packages"): packages = [] + for module, directory in setup_params["package_dir"].items(): packages = packages + self.__get_packages(directory, module) + setup_params["packages"] = packages # remove data files that are not available for target, files in setup_params["data_files"]: i = 0 while i < len(files): - file = files[i] - if os.path.isfile(file): + fname = files[i] + if os.path.isfile(fname): i += 1 else: - log.warn("warning: did not find file %s" % file) - files.remove(file) + log.warn("warning: did not find file %s" % fname) + files.remove(fname) # now call setup setup(name = setup_params["name"], _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue