From 16856ff75307ea8096f3965ed0398f808902960a Mon Sep 17 00:00:00 2001 From: Dylan Jay Date: Wed, 15 Jul 2015 11:23:27 +0700 Subject: [PATCH] handle working on a case insensitive case preserving filesystem like OSX --- pyjs/builtin/pyjslib.py | 3 +++ pyjs/lib/time.py | 1 + pyjs/linker.py | 32 +++++++++++++++++++---- pyjs/translator.py | 2 +- pyjs/translator_dict.py | 2 +- pyjs/translator_proto.py | 55 ++++++++++++++++++++++++++++++++++------ setup.py | 2 +- 7 files changed, 81 insertions(+), 16 deletions(-) diff --git a/pyjs/builtin/pyjslib.py b/pyjs/builtin/pyjslib.py index 27567ae30..54b896523 100644 --- a/pyjs/builtin/pyjslib.py +++ b/pyjs/builtin/pyjslib.py @@ -135,6 +135,9 @@ def __setattr__(self, name, value): return s + ""; }""") +#HACK +object.__reduce__ = object.__str__ + class basestring(object): pass diff --git a/pyjs/lib/time.py b/pyjs/lib/time.py index 7279a910a..8d4686b94 100644 --- a/pyjs/lib/time.py +++ b/pyjs/lib/time.py @@ -5,6 +5,7 @@ timezone = JS("60 * (new Date(new Date()['getFullYear'](), 0, 1))['getTimezoneOffset']()") altzone = JS("60 * (new Date(new Date()['getFullYear'](), 6, 1))['getTimezoneOffset']()") +daylight = altzone != timezone if altzone > timezone: # Probably on southern parth of the earth... d = timezone diff --git a/pyjs/linker.py b/pyjs/linker.py index de3de219a..e802b4492 100644 --- a/pyjs/linker.py +++ b/pyjs/linker.py @@ -35,6 +35,11 @@ assert set(non_boolean_opts) < set(translator_opts) def is_modified(in_file,out_file): + if not exits_casesensitive(outfile): + # due to case insensitivee filesystems, os.path.getmtime + # can return time for the wrong file + return True + modified = False in_mtime = os.path.getmtime(in_file) try: @@ -111,7 +116,8 @@ def out_translate(platform, file_names, out_file, module_name, if not incremental or do_translate: pydir = os.path.abspath(os.path.dirname(__file__)) if not 'PYJS_SYSPATH' in os.environ: - os.environ['PYJS_SYSPATH'] = sys.path[0] + #HACK: below assumes all we need is pyjs. how to get six and others? + os.environ['PYJS_SYSPATH'] = ':'.join(sys.path) opts = ["--module-name", module_name, "-o"] if sys.platform == 'win32': opts.append(out_file) @@ -148,6 +154,22 @@ def out_translate(platform, file_names, out_file, module_name, return deps, js_libs +def isfile_casesensitive(path): + if not os.path.isfile(path): return False # exit early + return exits_casesensitive(path) + +exists_cache = {} +def exits_casesensitive(path): + if not os.path.exists(path): return False # exit early + if exists_cache.get(path): return True + directory, filename = os.path.split(path) + if not directory or not filename: + return True + if not filename in os.listdir(directory): return False + exists_cache[path] = exits_casesensitive(directory) + return exists_cache[path] + + _path_cache= {} def module_path(name, path, platform=None): if name == '__pyjamas__' or name == '__javascript__': @@ -177,7 +199,7 @@ def module_path(name, path, platform=None): else: cache = {} _path_cache[name] = cache - if os.path.exists(cp + '.py'): + if exits_casesensitive(cp + '.py'): cache[p] = cp + '.py' else: tail = [] @@ -193,12 +215,12 @@ def module_path(name, path, platform=None): if p in cache: if cache[p] is None: break - elif os.path.isdir(cp) and os.path.exists( + elif os.path.isdir(cp) and exits_casesensitive( os.path.join(cp, '__init__.py')): cache[p] = os.path.join(cp, '__init__.py') - elif os.path.exists(cp + '.py'): + elif exits_casesensitive(cp + '.py'): cache[p] = cp + '.py' - elif pn.endswith('.js') and os.path.exists(cp): + elif pn.endswith('.js') and exits_casesensitive(cp): cache[p] = cp else: cache[p] = None diff --git a/pyjs/translator.py b/pyjs/translator.py index 664bcfec8..f5130825c 100644 --- a/pyjs/translator.py +++ b/pyjs/translator.py @@ -1,7 +1,7 @@ import sys import os if 'PYJS_SYSPATH' in os.environ: - sys.path[0:0] = [os.environ['PYJS_SYSPATH']] + sys.path[0:0] = os.environ['PYJS_SYSPATH'].split(':') import pyjs LIBRARY_PATH = os.path.abspath(os.path.dirname(__file__)) diff --git a/pyjs/translator_dict.py b/pyjs/translator_dict.py index eafeb4ecb..7dcee3b8d 100644 --- a/pyjs/translator_dict.py +++ b/pyjs/translator_dict.py @@ -3126,7 +3126,7 @@ def leaf_return(self, leaf): pythonic_options) if os.environ.has_key('PYJS_SYSPATH'): - sys.path[0:0] = [os.environ['PYJS_SYSPATH']] + sys.path[0:0] = os.environ['PYJS_SYSPATH'].split(':') import pyjs diff --git a/pyjs/translator_proto.py b/pyjs/translator_proto.py index 0f0f4dc8a..4af68a45b 100644 --- a/pyjs/translator_proto.py +++ b/pyjs/translator_proto.py @@ -17,6 +17,7 @@ import types import os import copy +import six from six.moves import cStringIO as StringIO import re try: @@ -28,11 +29,10 @@ from pyjs.options import (all_compile_options, add_compile_options, get_compile_options, debug_options, speed_options, pythonic_options) - if 'PYJS_SYSPATH' in os.environ: - sys.path[0:0] = [os.environ['PYJS_SYSPATH']] + sys.path[0:0] = os.environ['PYJS_SYSPATH'].split(':') -sys.path[1:1] = [os.path.join(os.path.dirname(__file__), "lib_trans")] +sys.path += [os.path.join(os.path.dirname(__file__), "lib_trans")] import pycompiler as compiler from pycompiler.visitor import ASTVisitor @@ -4449,16 +4449,43 @@ def translate(sources, output_file, module_name=None, **kw): compiler.walk(tree, v) return v.imported_modules, v.imported_js + deps = [] if hasattr(output_file, 'write'): output = output_file elif output_file == '-': output = sys.stdout else: - output = file(output_file, 'w') + # due to case insenstive filesystems two different modules might have to be in one file. + # open file and remove this modules code + # read in dep and remove + # open file in append mode and write + # combine dep and write them + matches = None + if os.path.exists(output_file): + with open(output_file, "r") as output: + compiled = output.read() + modules = re.findall("^/\* start module: (.*) \*/$", compiled, flags=re.MULTILINE) + if module_name in modules and len(modules) > 1: + r="(?ms)(.*)^/\* start module: %s \*/$(.*)^/\* end module: %s \*/$(.*)(/\*.*^PYJS_DEPS: (.*)$.*\*/).*" + matches = re.match(r % (module_name, module_name), compiled) + compiled = '\n'.join([matches.group(1), matches.group(3)]) + deps = eval(matches.group(5)) + elif module_name not in modules: + matches = re.match("(?ms)(.*)(/\*.*^PYJS_DEPS: (.*)$.*\*/).*", compiled) + compiled = '\n'.join([matches.group(1)]) + deps = eval(matches.group(3)) + #TODO: combine PYJS_DEPS + + if matches: + with open(output_file, "w") as output: + output.write(compiled) + output = file(output_file, "a") + else: + output = file(output_file, 'w') t = Translator(compiler, module_name, sources[0], src, tree, output, **kw) - return t.imported_modules, t.imported_js + return t.imported_modules+deps, t.imported_js def merge(ast, module_name, tree1, tree2, flags): if 'FULL_OVERRIDE' in flags: @@ -4661,6 +4688,18 @@ def dotreplace(fname): path, ext = os.path.splitext(fname) return path.replace(".", "/") + ext +def isfile_casesensitive(path): + if not os.path.isfile(path): return False # exit early + return exits_casesensitive(path) + +def exits_casesensitive(path): + if not os.path.exists(path): return False # exit early + directory, filename = os.path.split(path) + if not directory or not filename: + return True + if not filename in os.listdir(directory): return False + return exits_casesensitive(directory) + class ImportVisitor(object): def __init__(self, module_name): @@ -4768,14 +4807,14 @@ def __init__(self, compiler, self.parser.dynamic = dynamic def findFile(self, file_name): - if os.path.isfile(file_name): + if isfile_casesensitive(file_name): return file_name for library_dir in self.library_dirs: file_name = dotreplace(file_name) full_file_name = os.path.join( LIBRARY_PATH, library_dir, file_name) - if os.path.isfile(full_file_name): + if isfile_casesensitive(full_file_name): return full_file_name fnameinit, ext = os.path.splitext(file_name) @@ -4783,7 +4822,7 @@ def findFile(self, file_name): full_file_name = os.path.join( LIBRARY_PATH, library_dir, fnameinit) - if os.path.isfile(full_file_name): + if isfile_casesensitive(full_file_name): return full_file_name raise Exception("file not found: " + file_name) diff --git a/setup.py b/setup.py index d211ff1ad..fa9b76779 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ def read(fname): package_data=package_data, zip_safe = False, entry_points = entry_points, - install_requires=['six'], + install_requires=['six>=1.9.0'], license="Apache", keywords=["js", "javascript"],