diff --git a/.gitignore b/.gitignore index 24d7c7f..5337c93 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ tests/*/*.err tests/*/*.py.js tests/*.err tests/*.out +build +dist +py2js.egg-info +VERSION \ No newline at end of file diff --git a/AUTHORS b/AUTHORS index bf93c3c..cb9d8c9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -13,5 +13,6 @@ People who contributed by sending at least one patch: Baniel Jonathan Fine Pablo Angulo + Gordon Pendleton Tests were taken from js4py written by Jonathan Fine. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..2f3fb3b --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,8 @@ +include AUTHORS +include LICENSE +include README.rst +include library/* +include run_tests.py +include _version.py +recursive-include tests *.js *.py +recursive-exclude tests *.py.js \ No newline at end of file diff --git a/_version.py b/_version.py new file mode 100644 index 0000000..bdf8fd2 --- /dev/null +++ b/_version.py @@ -0,0 +1,124 @@ +from os.path import join, dirname +from platform import system, version +from re import findall, match, IGNORECASE +from subprocess import Popen, PIPE + +version_file = join(dirname(__file__),'VERSION') + +class GITExecutable(object): + """ + Attempts to find git. + Returns the best guess + at the path for a git + executable. + """ + def __new__(self): + cmd = 'which' + if system() == 'Windows': + try: + major, minor = [int(x) for x in version().split('.',2)[0:2]] + # version number information + # http://msdn.microsoft.com/en-us/library/ms724834%28v=VS.85%29.aspx + # (6, 0) is Vista or Windows Server 2008 + # (6, 1) is Windows 7 or Server 2008 RC2 + if (major, minor) > (6, 0): + # Windows 7 brings the + # where command with + # functionality similar + # to which on unix. + cmd = 'where' + except: + pass + try: + p = Popen([cmd, 'git'], stdout=PIPE, stderr=PIPE) + p.stderr.close() + path = p.stdout.next().strip() + except: + path = None + return path or 'git' + +GIT_EXECUTABLE = GITExecutable() + +def get_version(): + file_version = get_file_version() + version = get_repo_version() or file_version + if version is None: + raise ValueError('Could not determine version.') + if version != file_version: + put_file_version(version) + return version + +def get_file_version(): + try: + with open(version_file, 'rb') as fp: + version = fp.next().strip() + except: + version = None + return version + +def put_file_version(version): + with open(version_file, 'wb') as fp: + fp.write(version) + +def get_repo_version(): + """ + Repo tags are assumed to be in the format: + vMajor.Minor + + Example: + v0.1 + + Function returns a version string of the form: + vMajor.Minor.PatchHash + + Example: + v0.1.1gc58ec0d + """ + try: + p = Popen([GIT_EXECUTABLE, 'describe'], stdout=PIPE, stderr=PIPE) + p.stderr.close() + parts = findall('[a-zA-Z0-9]+',p.stdout.next().strip()) + if parts: + version = "%s.%s" % (parts[0],parts[1]) + if len(parts) > 2: + version = "%s.%s%s" % (version,parts[2],parts[3]) + else: + version = "%s.0" % version + else: + raise ValueError('git describe did not return a valid version string.') + except: + version = None + return version + +def parse_version(version): + """ + input version string of the form: + 'vMajor.Minor.PatchHash' + like: + 'v0.1.5g95ffef4' + ------ or ------ + 'v0.1.0' + + returns version_info tuple of the form: + (major,minor,patch,hash) + like: + (0, 1, 5, '95ffef4') + -------- or -------- + (0, 1, 0, '') + """ + matches = match( + 'v(?P[0-9]+)\.(?P[0-9]+)\.(?P[0-9]+)(g(?P[a-z0-9]*))?', + version, + IGNORECASE + ) + if matches: + major = int(matches.group('major')) + minor = int(matches.group('minor')) + patch = int(matches.group('patch')) + hash = matches.group('hash') or '' + return (major,minor,patch,hash) + else: + raise ValueError("Version string, '%s' could not be parsed. It should be of the form: 'vMajor.Minor.PatchHash'." % version) + +if __name__ == '__main__': + print get_version() \ No newline at end of file diff --git a/py2js/__init__.py b/py2js/__init__.py index e9d5a16..fadf60c 100644 --- a/py2js/__init__.py +++ b/py2js/__init__.py @@ -3,6 +3,10 @@ import ast import inspect import formater +from _version import get_version, parse_version + +__version__ = get_version() +__version_info__ = parse_version(__version__) def scope(func): func.scope = True diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..269bc34 --- /dev/null +++ b/setup.py @@ -0,0 +1,57 @@ +from os import listdir,remove +from os.path import join +from setuptools import setup, find_packages +from sys import version_info +from _version import get_version, version_file + +version = get_version() + +DESCRIPTION = 'Python to JavaScript translator' +LONG_DESCRIPTION = None +try: + LONG_DESCRIPTION = open('README.rst').read() +except: + pass + +AUTHORS = None +try: + AUTHORS = open('AUTHORS').read() +except: + pass + +# create the py-builtins.js file +# NOTE: +# test runner expects +# for the py-builtins.js +# file to be available +# so it should not be +# removed by the installer +builtins_dir = 'library' +builtins_out = 'py-builtins.js' +with open(builtins_out,'wb') as f: + paths = [join(builtins_dir,path) for path in sorted(listdir(builtins_dir))] + for path in paths: + with open(path,'rb') as fp: + f.write(fp.read()) + +# require dependencies +if version_info < (2 , 7): + REQUIRES = ('unittest2',) +else: + REQUIRES = () + +setup( + author = 'Ondrej Certik', + author_email = 'ondrej@certik.cz', + data_files = [('', [builtins_out]), ('py2js', [version_file, '_version.py'])], + description = DESCRIPTION, + install_requires=REQUIRES, + license = 'MIT', + long_description = LONG_DESCRIPTION, + name = 'py2js', + version = version, + packages = find_packages(), + platforms = ['any'], + scripts = ['pyjs.py'], + url = 'https://github.com/qsnake/py2js', +) \ No newline at end of file