From 53d06180335be7811d1834fe69eab6737579d50f Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Mon, 7 Aug 2017 23:40:18 +0530 Subject: [PATCH 1/3] harcoded tmp folder prevent windows compatibilty of past module --- src/past/translation/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/past/translation/__init__.py b/src/past/translation/__init__.py index 7b21d9f5..069f8f34 100644 --- a/src/past/translation/__init__.py +++ b/src/past/translation/__init__.py @@ -208,6 +208,7 @@ def detect_python2(source, pathname): """ Returns a bool indicating whether we think the code is Py2 """ + temppath=os.environ["TMP"] if os.environ["TMP"] else '/tmp' RTs.setup_detect_python2() try: tree = RTs._rt_py2_detect.refactor_string(source, pathname) @@ -219,20 +220,20 @@ def detect_python2(source, pathname): if source != str(tree)[:-1]: # remove added newline # The above fixers made changes, so we conclude it's Python 2 code logger.debug('Detected Python 2 code: {0}'.format(pathname)) - with open('/tmp/original_code.py', 'w') as f: + with open(os.path.join(temppath,'original_code.py'), 'w') as f: f.write('### Original code (detected as py2): %s\n%s' % (pathname, source)) - with open('/tmp/py2_detection_code.py', 'w') as f: + with open(os.path.join(temppath,'py2_detection_code.py'), 'w') as f: f.write('### Code after running py3 detection (from %s)\n%s' % (pathname, str(tree)[:-1])) return True else: logger.debug('Detected Python 3 code: {0}'.format(pathname)) - with open('/tmp/original_code.py', 'w') as f: + with open(os.path.join(temppath,'original_code.py'), 'w') as f: f.write('### Original code (detected as py3): %s\n%s' % (pathname, source)) try: - os.remove('/tmp/futurize_code.py') + os.remove(os.path.join(temppath,'futurize_code.py')) except OSError: pass return False @@ -395,7 +396,8 @@ def load_module(self, fullname): if detect_python2(source, self.pathname): source = self.transform(source) - with open('/tmp/futurized_code.py', 'w') as f: + temppath = os.environ["TMP"] if os.environ["TMP"] else '/tmp' + with open(os.path.join(temppath,'futurized_code.py'), 'w') as f: f.write('### Futurized code (from %s)\n%s' % (self.pathname, source)) From 37c8664dc679d8dabb88d31aa9f4f35f25c47ddd Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Tue, 8 Aug 2017 05:51:08 +0530 Subject: [PATCH 2/3] correcting TMP environment None error --- src/past/translation/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/past/translation/__init__.py b/src/past/translation/__init__.py index 069f8f34..16971799 100644 --- a/src/past/translation/__init__.py +++ b/src/past/translation/__init__.py @@ -208,7 +208,7 @@ def detect_python2(source, pathname): """ Returns a bool indicating whether we think the code is Py2 """ - temppath=os.environ["TMP"] if os.environ["TMP"] else '/tmp' + temppath=os.environ["TMP"] if os.environ["TMP"] is not None else '/tmp' RTs.setup_detect_python2() try: tree = RTs._rt_py2_detect.refactor_string(source, pathname) @@ -396,7 +396,7 @@ def load_module(self, fullname): if detect_python2(source, self.pathname): source = self.transform(source) - temppath = os.environ["TMP"] if os.environ["TMP"] else '/tmp' + temppath = os.environ["TMP"] if os.environ["TMP"] is not None else '/tmp' with open(os.path.join(temppath,'futurized_code.py'), 'w') as f: f.write('### Futurized code (from %s)\n%s' % (self.pathname, source)) From 5eef178dfbc751cb6cbb077de0a83ea4d4332246 Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Wed, 9 Aug 2017 00:24:32 +0530 Subject: [PATCH 3/3] using tempfile.mkdtemp to create a temp directory for writing file onto --- src/past/translation/__init__.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/past/translation/__init__.py b/src/past/translation/__init__.py index 16971799..73b25746 100644 --- a/src/past/translation/__init__.py +++ b/src/past/translation/__init__.py @@ -38,6 +38,7 @@ import os import sys import copy +import tempfile from lib2to3.pgen2.parse import ParseError from lib2to3.refactor import RefactoringTool @@ -208,7 +209,6 @@ def detect_python2(source, pathname): """ Returns a bool indicating whether we think the code is Py2 """ - temppath=os.environ["TMP"] if os.environ["TMP"] is not None else '/tmp' RTs.setup_detect_python2() try: tree = RTs._rt_py2_detect.refactor_string(source, pathname) @@ -220,20 +220,20 @@ def detect_python2(source, pathname): if source != str(tree)[:-1]: # remove added newline # The above fixers made changes, so we conclude it's Python 2 code logger.debug('Detected Python 2 code: {0}'.format(pathname)) - with open(os.path.join(temppath,'original_code.py'), 'w') as f: + with open(os.path.join(tempfile.mkdtemp(),'original_code.py'), 'w') as f: f.write('### Original code (detected as py2): %s\n%s' % (pathname, source)) - with open(os.path.join(temppath,'py2_detection_code.py'), 'w') as f: + with open(os.path.join(tempfile.mkdtemp(),'py2_detection_code.py'), 'w') as f: f.write('### Code after running py3 detection (from %s)\n%s' % (pathname, str(tree)[:-1])) return True else: logger.debug('Detected Python 3 code: {0}'.format(pathname)) - with open(os.path.join(temppath,'original_code.py'), 'w') as f: + with open(os.path.join(tempfile.mkdtemp(),'original_code.py'), 'w') as f: f.write('### Original code (detected as py3): %s\n%s' % (pathname, source)) try: - os.remove(os.path.join(temppath,'futurize_code.py')) + os.remove(os.path.join(tempfile.mkdtemp(),'futurize_code.py')) except OSError: pass return False @@ -396,8 +396,7 @@ def load_module(self, fullname): if detect_python2(source, self.pathname): source = self.transform(source) - temppath = os.environ["TMP"] if os.environ["TMP"] is not None else '/tmp' - with open(os.path.join(temppath,'futurized_code.py'), 'w') as f: + with open(os.path.join(tempfile.mkdtemp(),'futurized_code.py'), 'w') as f: f.write('### Futurized code (from %s)\n%s' % (self.pathname, source))