Esempio n. 1
0
def find_py_source(path, ignore=True):
    """Find the source file for a given bytecode file.

    If ignore is True, errors are swallowed and None is returned"""
    if not ignore:
        return source_from_cache(path)
    else:
        try:
            return source_from_cache(path)
        except (NotImplementedError, ValueError):
            return None
Esempio n. 2
0
    def filename_and_mtime(self, module):
        if not hasattr(module, '__file__') or module.__file__ is None:
            return None, None

        if getattr(module, '__name__', None) == '__main__':
            # we cannot reload(__main__)
            return None, None

        filename = module.__file__
        path, ext = os.path.splitext(filename)

        if ext.lower() == '.py':
            py_filename = filename
        else:
            try:
                py_filename = source_from_cache(filename)
            except ValueError:
                return None, None

        try:
            pymtime = os.stat(py_filename).st_mtime
        except OSError:
            return None, None

        return py_filename, pymtime
Esempio n. 3
0
    def filename_and_mtime(self, module):
        if not hasattr(module, '__file__') or module.__file__ is None:
            return None, None

        if getattr(module, '__name__',
                   None) in [None, '__mp_main__', '__main__']:
            # we cannot reload(__main__) or reload(__mp_main__)
            return None, None

        filename = module.__file__
        path, ext = os.path.splitext(filename)

        if ext.lower() == '.py':
            py_filename = filename
        else:
            try:
                py_filename = source_from_cache(filename)
            except ValueError:
                return None, None

        try:
            pymtime = os.stat(py_filename).st_mtime
        except OSError:
            return None, None

        return py_filename, pymtime
Esempio n. 4
0
 def test_source_from_cache(self):
     # Given the path to a PEP 3147 defined .pyc file, return the path to
     # its source.  This tests the good path.
     path = os.path.join('foo', 'bar', 'baz', '__pycache__',
                         'qux.{}.pyc'.format(self.tag))
     expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
     self.assertEqual(util.source_from_cache(path), expect)
Esempio n. 5
0
 def test_source_from_cache(self):
     # Given the path to a PEP 3147 defined .pyc file, return the path to
     # its source.  This tests the good path.
     path = os.path.join('foo', 'bar', 'baz', '__pycache__',
                         'qux.{}.pyc'.format(self.tag))
     expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
     self.assertEqual(util.source_from_cache(path), expect)
Esempio n. 6
0
def source_from_cache(path):
    """Given the path to a .pyc. file, return the path to its .py file.

    The .pyc file does not need to exist; this simply returns the path to
    the .py file calculated to correspond to the .pyc file.  If path does
    not conform to PEP 3147 format, ValueError will be raised. If
    sys.implementation.cache_tag is None then NotImplementedError is raised.

    """
    return util.source_from_cache(path)
Esempio n. 7
0
def source_from_cache(path):
    """**DEPRECATED**

    Given the path to a .pyc. file, return the path to its .py file.

    The .pyc file does not need to exist; this simply returns the path to
    the .py file calculated to correspond to the .pyc file.  If path does
    not conform to PEP 3147 format, ValueError will be raised. If
    sys.implementation.cache_tag is None then NotImplementedError is raised.

    """
    return util.source_from_cache(path)
Esempio n. 8
0
 def clean(self):
     """Delete all .pyc files in the tree that have no matching .py file."""
     _log.debug("Cleaning orphaned *.pyc files from: %s" %
                self.search_directory)
     filenames = self.filesystem.files_under(self.search_directory)
     for filename in filenames:
         if filename.endswith(".pyc"):
             try:
                 orphan = source_from_cache(filename) not in filenames
             except ValueError:
                 orphan = True
             if orphan:
                 _log.info("Deleting orphan *.pyc file: %s" % filename)
                 self.filesystem.remove(filename)
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
    zip_filename = zip_basename + os.extsep + 'zip'
    zip_name = os.path.join(zip_dir, zip_filename)
    zip_file = zipfile.ZipFile(zip_name, 'w')
    if name_in_zip is None:
        parts = script_name.split(os.sep)
        if len(parts) >= 2 and parts[-2] == '__pycache__':
            legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
            name_in_zip = os.path.basename(legacy_pyc)
            script_name = legacy_pyc
        else:
            name_in_zip = os.path.basename(script_name)
    zip_file.write(script_name, name_in_zip)
    zip_file.close()
    return zip_name, os.path.join(zip_name, name_in_zip)
Esempio n. 10
0
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
    zip_filename = zip_basename+os.extsep+'zip'
    zip_name = os.path.join(zip_dir, zip_filename)
    with zipfile.ZipFile(zip_name, 'w') as zip_file:
        if name_in_zip is None:
            parts = script_name.split(os.sep)
            if len(parts) >= 2 and parts[-2] == '__pycache__':
                legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
                name_in_zip = os.path.basename(legacy_pyc)
                script_name = legacy_pyc
            else:
                name_in_zip = os.path.basename(script_name)
        zip_file.write(script_name, name_in_zip)
    #if test.support.verbose:
    #    with zipfile.ZipFile(zip_name, 'r') as zip_file:
    #        print 'Contents of %r:' % zip_name
    #        zip_file.printdir()
    return zip_name, os.path.join(zip_name, name_in_zip)
Esempio n. 11
0
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
    zip_filename = zip_basename+os.extsep+'zip'
    zip_name = os.path.join(zip_dir, zip_filename)
    with zipfile.ZipFile(zip_name, 'w') as zip_file:
        if name_in_zip is None:
            parts = script_name.split(os.sep)
            if len(parts) >= 2 and parts[-2] == '__pycache__':
                legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
                name_in_zip = os.path.basename(legacy_pyc)
                script_name = legacy_pyc
            else:
                name_in_zip = os.path.basename(script_name)
        zip_file.write(script_name, name_in_zip)
    #if test.support.verbose:
    #    with zipfile.ZipFile(zip_name, 'r') as zip_file:
    #        print 'Contents of %r:' % zip_name
    #        zip_file.printdir()
    return zip_name, os.path.join(zip_name, name_in_zip)
Esempio n. 12
0
 def test_source_from_cache_no_cache_tag(self):
     # If sys.implementation.cache_tag is None, raise NotImplementedError.
     path = os.path.join('blah', '__pycache__', 'whatever.pyc')
     with support.swap_attr(sys.implementation, 'cache_tag', None):
         with self.assertRaises(NotImplementedError):
             util.source_from_cache(path)
Esempio n. 13
0
 def test_source_from_cache_no_cache_tag(self):
     # If sys.implementation.cache_tag is None, raise NotImplementedError.
     path = os.path.join('blah', '__pycache__', 'whatever.pyc')
     with support.swap_attr(sys.implementation, 'cache_tag', None):
         with self.assertRaises(NotImplementedError):
             util.source_from_cache(path)