From f3a121563512d51133cbacf50c96b1a3dccde31d Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Wed, 17 Aug 2011 11:51:08 -0400 Subject: setup script enhancements * added code to make builtin snapshots & releases with correct version # easier, no longer dependant on egg_info's tag_date (which didn't make PEP compatible version strings anyways). * moved passlib.setup to passlib._setup - not really publically useful anyways, and name was causing nose/unitest to get confused * added tests/*.cfg to setup & manifest --- MANIFEST.in | 2 +- passlib.komodoproject | 2 +- passlib/_setup/__init__.py | 5 ++ passlib/_setup/cond2to3.py | 152 +++++++++++++++++++++++++++++++++++++++++++++ passlib/_setup/stamp.py | 57 +++++++++++++++++ passlib/setup/__init__.py | 1 - passlib/setup/cond2to3.py | 152 --------------------------------------------- setup.cfg | 7 --- setup.py | 109 +++++++++++++++++++++++--------- 9 files changed, 297 insertions(+), 190 deletions(-) create mode 100644 passlib/_setup/__init__.py create mode 100644 passlib/_setup/cond2to3.py create mode 100644 passlib/_setup/stamp.py delete mode 100644 passlib/setup/__init__.py delete mode 100644 passlib/setup/cond2to3.py diff --git a/MANIFEST.in b/MANIFEST.in index b3853b0..f29430e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ recursive-include docs * -include LICENSE README CHANGES passlib/*.cfg +include LICENSE README CHANGES passlib/*.cfg passlib/tests/*.cfg prune docs/_build prune *.komodoproject diff --git a/passlib.komodoproject b/passlib.komodoproject index 324caf1..b579f50 100644 --- a/passlib.komodoproject +++ b/passlib.komodoproject @@ -3,7 +3,7 @@ - *$py.class;*.egg-info;cover;_build;dist;build;.*;*.*~;*.bak;*.tmp;CVS;.#*;*.pyo;*.pyc;.svn;*%*;tmp*.html;.DS_Store + __pycache__;*$py.class;*.egg-info;cover;_build;dist;build;.*;*.*~;*.bak;*.tmp;CVS;.#*;*.pyo;*.pyc;.svn;*%*;tmp*.html;.DS_Store 1 diff --git a/passlib/_setup/__init__.py b/passlib/_setup/__init__.py new file mode 100644 index 0000000..814c93a --- /dev/null +++ b/passlib/_setup/__init__.py @@ -0,0 +1,5 @@ +"""passlib.setup - helpers used by passlib's setup.py script + +note that unlike the rest of passlib, the code in this package must +work *unaltered* under both python 2 & 3 +""" diff --git a/passlib/_setup/cond2to3.py b/passlib/_setup/cond2to3.py new file mode 100644 index 0000000..12cb0a8 --- /dev/null +++ b/passlib/_setup/cond2to3.py @@ -0,0 +1,152 @@ +"""passlib.setup.cond2to3 - moneypatches 2to3 to provide conditional macros, ala SQLAlchemy""" +#========================================================= +#imports +#========================================================= +#core +from lib2to3.refactor import RefactoringTool +import re +#site +#local +__all__ = [ + "patch2to3", +] + +#========================================================= +#macro preprocessor +#========================================================= +py3k_start_re = re.compile(r"^(\s*)# Py3K #", re.I) +py3k_stop_re = re.compile(r"^(\s*)# end Py3K #", re.I) + +py2k_start_re = re.compile(r"^(\s*)# Py2K #", re.I) +py2k_stop_re = re.compile(r"^(\s*)# end Py2K #", re.I) + +bare_comment_re = re.compile(r"^(\s*)#(.*)") +bare_re = re.compile(r"^(\s*)(.*)") + +def preprocess(data, name): + #TODO: add flag so this can also function in reverse, for 3to2 + changed = False + + lines = data.split("\n") + state = 0 + #0: parsing normally, looking for start-p3k or start-py2k + #1: in Py3K block - removing comment chars until end-py3k + #2: in Py2K block - adding comment chars until end-py2k + idx = 0 + indent = '' + while idx < len(lines): + line = lines[idx] + + #hack to detect ''"abc" strings - using this as py25-compat way to indicate bytes. + #should really turn into a proper fixer. + #also, this check is really weak, and might fail in some cases + if '\'\'".*"' in line: + line = lines[idx] = line.replace("''", "b") + changed = True + + #check for py3k start marker + m = py3k_start_re.match(line) + if m: + if state in (0,2): + ident = m.group(1) + state = 1 + idx += 1 + continue + #states 1 this is an error... + raise SyntaxError("unexpected py3k-start marker on line %d of %r: %r" % (idx, name, line)) + + #check for py3k stop marker + if py3k_stop_re.match(line): + if state == 1: + state = 0 + idx += 1 + continue + #states 0,2 this is an error... + raise SyntaxError("unexpected py3k-stop marker on line %d of %r: %r" % (idx, name, line)) + + #check for py2k start marker + m = py2k_start_re.match(line) + if m: + if state in (0,1): + ident = m.group(1) + state = 2 + idx += 1 + continue + #states 2 this is an error... + raise SyntaxError("unexpected py2k-start marker on line %d of %r: %r" % (idx, name, line)) + + #check for py2k end marker + if py2k_stop_re.match(line): + if state == 2: + state = 0 + idx += 1 + continue + #states 0,1 this is an error... + raise SyntaxError("unexpected py2k-stop marker on line %d of %r: %r" % (idx, name, line)) + + #state 0 - leave non-marker lines alone + if state == 0: + idx += 1 + continue + + #state 1 - uncomment comment lines, throw error on bare lines + if state == 1: + m = bare_comment_re.match(line) + if not m: + raise SyntaxError("unexpected non-comment in py3k block on line %d of %r: %r" % (idx,name, line)) + pad, content = m.group(1,2) + lines[idx] = pad + content + changed = True + idx += 1 + continue + + #state 2 - comment out all lines + if state == 2: + m = bare_re.match(line) + if not m: + raise RuntimeError("unexpected failure to parse line %d of %r: %r" % (idx, name, line)) + pad, content = m.group(1,2) + if pad.startswith(ident): #try to put comments on same level + content = pad[len(ident):] + content + pad = ident + lines[idx] = "%s#%s" % (pad,content) + changed = True + idx += 1 + continue + + #should never get here + raise AssertionError("invalid state: %r" % (state,)) + + if changed: + return "\n".join(lines) + else: + return data + +orig_rs = RefactoringTool.refactor_string + +def refactor_string(self, data, name): + "replacement for RefactoringTool.refactor_string which honors conditional includes" + newdata = preprocess(data, name) + tree = orig_rs(self, newdata, name) + if tree and newdata != data: + tree.was_changed = True + return tree + +#========================================================= +#main +#========================================================= + +def patch2to3(): + "frontend to patch preprocessor into lib2to3" + RefactoringTool.refactor_string = refactor_string + +#helper for development purposes - runs 2to3 w/ patch +if __name__ == "__main__": + import sys + from lib2to3.main import main + patch2to3() + sys.exit(main("lib2to3.fixes")) + +#========================================================= +#eof +#========================================================= diff --git a/passlib/_setup/stamp.py b/passlib/_setup/stamp.py new file mode 100644 index 0000000..14abd59 --- /dev/null +++ b/passlib/_setup/stamp.py @@ -0,0 +1,57 @@ +"update version string during build" +#========================================================= +# imports +#========================================================= +from __future__ import with_statement +#core +import os +import re +import time +from distutils.dist import Distribution +#pkg +#local +__all__ = [ + "stamp_source", + "stamp_distutils_output", +] +#========================================================= +# helpers +#========================================================= +def get_command_class(opts, name): + return opts['cmdclass'].get(name) or Distribution().get_command_class(name) + +def stamp_source(base_dir, version, dry_run=False): + "update version string in passlib dist" + path = os.path.join(base_dir, "passlib", "__init__.py") + with open(path) as fh: + input = fh.read() + output = re.sub('(?m)^__version__\s*=.*$', + '__version__ = ' + repr(version), + input) + assert output != input, "failed to match" + if not dry_run: + os.unlink(path) # sdist likes to use hardlinks + with open(path, "w") as fh: + fh.write(output) + +def stamp_distutils_output(opts, version): + + # subclass buildpy to update version string in source + _build_py = get_command_class(opts, "build_py") + class build_py(_build_py): + def build_packages(self): + _build_py.build_packages(self) + stamp_source(self.build_lib, version, self.dry_run) + opts['cmdclass']['build_py'] = build_py + + # subclass sdist to do same thing + _sdist = get_command_class(opts, "sdist") + class sdist(_sdist): + def make_release_tree(self, base_dir, files): + _sdist.make_release_tree(self, base_dir, files) + stamp_source(base_dir, version, self.dry_run) + opts['cmdclass']['sdist'] = sdist + +#========================================================= +# eof +#========================================================= diff --git a/passlib/setup/__init__.py b/passlib/setup/__init__.py deleted file mode 100644 index 2966610..0000000 --- a/passlib/setup/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"passlib.setup - package containing helpers used by passlib's setup.py script" diff --git a/passlib/setup/cond2to3.py b/passlib/setup/cond2to3.py deleted file mode 100644 index 12cb0a8..0000000 --- a/passlib/setup/cond2to3.py +++ /dev/null @@ -1,152 +0,0 @@ -"""passlib.setup.cond2to3 - moneypatches 2to3 to provide conditional macros, ala SQLAlchemy""" -#========================================================= -#imports -#========================================================= -#core -from lib2to3.refactor import RefactoringTool -import re -#site -#local -__all__ = [ - "patch2to3", -] - -#========================================================= -#macro preprocessor -#========================================================= -py3k_start_re = re.compile(r"^(\s*)# Py3K #", re.I) -py3k_stop_re = re.compile(r"^(\s*)# end Py3K #", re.I) - -py2k_start_re = re.compile(r"^(\s*)# Py2K #", re.I) -py2k_stop_re = re.compile(r"^(\s*)# end Py2K #", re.I) - -bare_comment_re = re.compile(r"^(\s*)#(.*)") -bare_re = re.compile(r"^(\s*)(.*)") - -def preprocess(data, name): - #TODO: add flag so this can also function in reverse, for 3to2 - changed = False - - lines = data.split("\n") - state = 0 - #0: parsing normally, looking for start-p3k or start-py2k - #1: in Py3K block - removing comment chars until end-py3k - #2: in Py2K block - adding comment chars until end-py2k - idx = 0 - indent = '' - while idx < len(lines): - line = lines[idx] - - #hack to detect ''"abc" strings - using this as py25-compat way to indicate bytes. - #should really turn into a proper fixer. - #also, this check is really weak, and might fail in some cases - if '\'\'".*"' in line: - line = lines[idx] = line.replace("''", "b") - changed = True - - #check for py3k start marker - m = py3k_start_re.match(line) - if m: - if state in (0,2): - ident = m.group(1) - state = 1 - idx += 1 - continue - #states 1 this is an error... - raise SyntaxError("unexpected py3k-start marker on line %d of %r: %r" % (idx, name, line)) - - #check for py3k stop marker - if py3k_stop_re.match(line): - if state == 1: - state = 0 - idx += 1 - continue - #states 0,2 this is an error... - raise SyntaxError("unexpected py3k-stop marker on line %d of %r: %r" % (idx, name, line)) - - #check for py2k start marker - m = py2k_start_re.match(line) - if m: - if state in (0,1): - ident = m.group(1) - state = 2 - idx += 1 - continue - #states 2 this is an error... - raise SyntaxError("unexpected py2k-start marker on line %d of %r: %r" % (idx, name, line)) - - #check for py2k end marker - if py2k_stop_re.match(line): - if state == 2: - state = 0 - idx += 1 - continue - #states 0,1 this is an error... - raise SyntaxError("unexpected py2k-stop marker on line %d of %r: %r" % (idx, name, line)) - - #state 0 - leave non-marker lines alone - if state == 0: - idx += 1 - continue - - #state 1 - uncomment comment lines, throw error on bare lines - if state == 1: - m = bare_comment_re.match(line) - if not m: - raise SyntaxError("unexpected non-comment in py3k block on line %d of %r: %r" % (idx,name, line)) - pad, content = m.group(1,2) - lines[idx] = pad + content - changed = True - idx += 1 - continue - - #state 2 - comment out all lines - if state == 2: - m = bare_re.match(line) - if not m: - raise RuntimeError("unexpected failure to parse line %d of %r: %r" % (idx, name, line)) - pad, content = m.group(1,2) - if pad.startswith(ident): #try to put comments on same level - content = pad[len(ident):] + content - pad = ident - lines[idx] = "%s#%s" % (pad,content) - changed = True - idx += 1 - continue - - #should never get here - raise AssertionError("invalid state: %r" % (state,)) - - if changed: - return "\n".join(lines) - else: - return data - -orig_rs = RefactoringTool.refactor_string - -def refactor_string(self, data, name): - "replacement for RefactoringTool.refactor_string which honors conditional includes" - newdata = preprocess(data, name) - tree = orig_rs(self, newdata, name) - if tree and newdata != data: - tree.was_changed = True - return tree - -#========================================================= -#main -#========================================================= - -def patch2to3(): - "frontend to patch preprocessor into lib2to3" - RefactoringTool.refactor_string = refactor_string - -#helper for development purposes - runs 2to3 w/ patch -if __name__ == "__main__": - import sys - from lib2to3.main import main - patch2to3() - sys.exit(main("lib2to3.fixes")) - -#========================================================= -#eof -#========================================================= diff --git a/setup.cfg b/setup.cfg index 38574b1..cce5f16 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,10 +1,3 @@ -[egg_info] -# XXX: should stop relying on this section, -# it's only used by setuptools/distribute, -# and will be phased out in distutils2 -tag_build = a1.dev -tag_date = true - [upload] sign = true diff --git a/setup.py b/setup.py index c3b7a38..12c420f 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,18 @@ """passlib setup script""" #========================================================= -#init script env -- ensure cwd = root of source dir +# init script env -- ensure cwd = root of source dir #========================================================= import os root_dir = os.path.abspath(os.path.join(__file__,"..")) os.chdir(root_dir) #========================================================= -#imports +# imports #========================================================= import re import sys +import time + py3k = (sys.version_info[0] >= 3) try: @@ -19,31 +21,68 @@ try: except ImportError: from distutils import setup has_distribute = False - + #========================================================= -#enable various 2to3 options +# init setup options #========================================================= -opts = { "cmdclass": {} } +opts = { "cmdclass": { } } +args = sys.argv[1:] +#========================================================= +# 2to3 translation +#========================================================= if py3k: - #monkeypatch preprocessor into lib2to3 - from passlib.setup.cond2to3 import patch2to3 + # monkeypatch preprocessor into lib2to3 + from passlib._setup.cond2to3 import patch2to3 patch2to3() - #enable 2to3 translation in build_py + # enable 2to3 translation in build_py if has_distribute: opts['use_2to3'] = True else: - #if we can't use distribute's "use_2to3" flag, - #have to override build_py command + # if we can't use distribute's "use_2to3" flag, + # have to override build_py command from distutils.command.build_py import build_py_2to3 as build_py opts['cmdclass']['build_py'] = build_py #========================================================= -#version string +# version string / datestamps #========================================================= from passlib import __version__ as VERSION +# if this is an hg checkout of passlib, add datestamp to version string. +# XXX: could check for *absence* of PKG-INFO instead +if os.path.exists(os.path.join(root_dir, "passlib.komodoproject")): + + # check for --for-release flag indicating this isn't a snapshot + for_release = False + i = 0 + while i < len(args): + v = args[i] + if v == '--for-release': + for_release = True + del args[i] + break + elif not v.startswith("-"): + break + i += 1 + + if for_release: + assert '.dev' not in VERSION and '.post' not in VERSION + else: + # add datestamp if doing a snapshot + dstr = time.strftime("%Y%m%d") + if VERSION.endswith(".dev0") or VERSION.endswith(".post0"): + VERSION = VERSION[:-1] + dstr + else: + assert '.dev' not in VERSION and '.post' not in VERSION + VERSION += ".post" + dstr + + # subclass build_py & sdist so they rewrite passlib/__init__.py + # to have the correct version string + from passlib._setup.stamp import stamp_distutils_output + stamp_distutils_output(opts, VERSION) + #========================================================= #static text #========================================================= @@ -71,21 +110,44 @@ All releases are signed with the gpg key """ KEYWORDS = "password secret hash security crypt md5-crypt \ -sha256-crypt sha512-crypt bcrypt apache htpasswd htdigest pbkdf2 ntlm" +sha256-crypt sha512-crypt bcrypt apache htpasswd htdigest pbkdf2" + +CLASSIFIERS = """\ +Intended Audience :: Developers +License :: OSI Approved :: BSD License +Natural Language :: English +Operating System :: OS Independent +Programming Language :: Python :: 2.5 +Programming Language :: Python :: 2.6 +Programming Language :: Python :: 2.7 +Programming Language :: Python :: 3 +Topic :: Security :: Cryptography +Topic :: Software Development :: Libraries +""".splitlines() + +is_release = False +if '.dev' in VERSION: + CLASSIFIERS.append("Development Status :: 3 - Alpha") +elif '.post' in VERSION: + CLASSIFIERS.append("Development Status :: 4 - Beta") +else: + is_release = True + CLASSIFIERS.append("Development Status :: 5 - Production/Stable") #========================================================= #run setup #========================================================= +# XXX: could omit 'passlib.setup' from eggs, but not sdist setup( #package info packages = [ "passlib", "passlib.handlers", - "passlib.setup", "passlib.tests", "passlib.utils", + "passlib._setup", ], - package_data = { "passlib": ["*.cfg"] }, + package_data = { "passlib": ["*.cfg" ], "passlib.tests": ["*.cfg"] }, zip_safe=True, #metadata @@ -96,29 +158,20 @@ setup( license = "BSD", url = "http://passlib.googlecode.com", - download_url = "http://passlib.googlecode.com/files/passlib-" + VERSION + ".tar.gz", + download_url = + ("http://passlib.googlecode.com/files/passlib-" + VERSION + ".tar.gz") + if is_release else None, description = SUMMARY, long_description = DESCRIPTION, keywords = KEYWORDS, - classifiers = [ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: BSD License", - "Natural Language :: English", - "Operating System :: OS Independent", - "Programming Language :: Python :: 2.5", - "Programming Language :: Python :: 2.6", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3", - "Topic :: Security :: Cryptography", - "Topic :: Software Development :: Libraries", - ], + classifiers = CLASSIFIERS, tests_require = 'nose >= 1.0', test_suite = 'nose.collector', #extra opts + script_args=args, **opts ) #========================================================= -- cgit v1.2.1