Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a setup.py file for installation and packaging #18

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ tests/*/*.err
tests/*/*.py.js
tests/*.err
tests/*.out
build
dist
py2js.egg-info
VERSION
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ People who contributed by sending at least one patch:
Baniel <[email protected]>
Jonathan Fine <[email protected]>
Pablo Angulo <[email protected]>
Gordon Pendleton <[email protected]>

Tests were taken from js4py written by Jonathan Fine.
8 changes: 8 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -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
124 changes: 124 additions & 0 deletions _version.py
Original file line number Diff line number Diff line change
@@ -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<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(g(?P<hash>[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()
4 changes: 4 additions & 0 deletions py2js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]',
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',
)