Beispiel #1
0
    def check_extname(self, fname, version=None):
        """Return extension file name if file can be renamed."""

        version = Version(version or self.version)

        if '/' in fname:
            fdir, fname = fname.rsplit('/', 1)  # in case full path was passed
        else:
            fdir = ''

        info = EXTFILE_RE.search(fname)
        if not info:
            return
        info = info.groupdict()
        if info['ver'] and (not version or version.minor is None):
            # get version from soabi if version is not set of only major
            # version number is set
            version = Version("%s.%s" % (info['ver'][0], info['ver'][1]))

        if info['stableabi']:
            # files with stable ABI in name don't need changes
            return
        if info['debug'] and self.debug is False:
            # do not change Python 2.X extensions already marked as debug
            # (the other way arround is acceptable)
            return
        if info['soabi'] and info['multiarch']:
            # already tagged, nothing we can do here
            return

        try:
            soabi, multiarch = self._get_config(version)[:2]
        except Exception:
            log.debug('cannot get soabi/multiarch', exc_info=True)
            return

        if info['soabi'] and soabi and info['soabi'] != soabi:
            return

        tmp_soabi = info['soabi'] or soabi
        tmp_multiarch = info['multiarch'] or multiarch

        result = info['name']
        if result.endswith('module') and (self.impl == 'cpython3' and version >> '3.2'
                                          or self.impl == 'cpython2' and version == '2.7'):
            result = result[:-6]

        if tmp_soabi:
            result = "{}.{}".format(result, tmp_soabi)
            if tmp_multiarch and not (self.impl == 'cpython3' and version << '3.3') and tmp_multiarch not in soabi:
                result = "{}-{}".format(result, tmp_multiarch)
        elif self.impl == 'cpython2' and version == '2.7' and tmp_multiarch:
            result = "{}.{}".format(result, tmp_multiarch)

        if self.debug and self.impl == 'cpython2':
            result += '_d'
        result += '.so'
        if fname == result:
            return
        return join(fdir, result)
Beispiel #2
0
    def cache_file(self, fpath, version=None):
        """Given path to a .py file, return path to its .pyc/.pyo file.

        This function is inspired by Python 3.2's imp.cache_from_source.

        :param fpath: path to file name
        :param version: Python version

        >>> i = Interpreter('python')
        >>> i.cache_file('foo.py', Version('3.1'))
        'foo.pyc'
        >>> i.cache_file('bar/foo.py', '3.4')
        'bar/__pycache__/foo.cpython-34.pyc'
        """
        version = Version(version or self.version)
        last_char = 'o' if '-O' in self.options else 'c'
        if version <= Version('3.1'):
            return fpath + last_char

        fdir, fname = split(fpath)
        if not fname.endswith('.py'):
            fname += '.py'
        return join(
            fdir, '__pycache__',
            "%s.%s.py%s" % (fname[:-3], self.magic_tag(version), last_char))
Beispiel #3
0
    def magic_tag(self, version=None):
        """Return Python magic tag (used in __pycache__ dir to tag files).

        >>> i = Interpreter('python')
        >>> i.magic_tag(version='3.4')
        'cpython-34'
        """
        version = Version(version or self.version)
        if self.impl.startswith('cpython') and version << Version('3.2'):
            return ''
        return self._execute('import imp; print(imp.get_tag())', version)
Beispiel #4
0
    def __init__(self, value=None, path=None, name=None, version=None,
                 debug=None, impl=None, options=None):
        params = locals()
        del params['self']
        del params['value']

        if isinstance(value, Interpreter):
            for key in params.keys():
                if params[key] is None:
                    params[key] = getattr(value, key)
        elif value:
            if value.replace('.', '').isdigit() and not version:
                # version string
                params['version'] = Version(value)
            else:
                # shebang or other string
                for key, val in self.parse(value).items():
                    # prefer values passed to constructor over shebang ones:
                    if params[key] is None:
                        params[key] = val

        for key, val in params.items():
            if val is not None:
                setattr(self, key, val)
            elif key == 'version':
                setattr(self, key, val)
Beispiel #5
0
 def __setattr__(self, name, value):
     if name == 'name':
         if value not in ('python', 'pypy', ''):
             raise ValueError("interpreter not supported: %s" % value)
         if value == 'python':
             if self.version:
                 if self.version.major == 3:
                     self.__dict__['impl'] = 'cpython3'
                 else:
                     self.__dict__['impl'] = 'cpython2'
         elif value == 'pypy':
             self.__dict__['impl'] = 'pypy'
     elif name == 'version' and value is not None:
         value = Version(value)
         if not self.impl and self.name == 'python':
             if value.major == 3:
                 self.impl = 'cpython3'
             else:
                 self.impl = 'cpython2'
     if name in ('path', 'name', 'impl', 'options') and value is None:
         pass
     elif name == 'debug':
         self.__dict__[name] = bool(value)
     else:
         self.__dict__[name] = value
Beispiel #6
0
 def magic_number(self, version=None):
     """Return magic number."""
     version = Version(version or self.version)
     if self.impl == 'cpython2':
         return ''
     result = self._execute('import imp; print(imp.get_magic())', version)
     return eval(result)
Beispiel #7
0
 def parse_public_version(self, path):
     """Return version assigned to site-packages path."""
     match = PUBLIC_DIR_RE[self.impl].match(path)
     if match:
         vers = match.groups(0)
         if vers and vers[0]:
             return Version(vers)
         # PyPy is not versioned
         return default(self.impl)
 def parse_public_dir(self, path):
     """Return version assigned to site-packages path
     or True is it's unversioned public dir."""
     match = PUBLIC_DIR_RE[self.impl].match(path)
     if match:
         vers = match.groups(0)
         if vers and vers[0]:
             return Version(vers)
         return True
Beispiel #9
0
 def multiarch(self, version=None):
     """Return multiarch tag."""
     version = Version(version or self.version)
     try:
         soabi, multiarch = self._get_config(version)[:2]
     except Exception:
         log.debug('cannot get multiarch', exc_info=True)
         # interpreter without multiarch support
         return ''
     return multiarch
Beispiel #10
0
 def soabi(self, version=None):
     """Return SOABI flag (used to in .so files)."""
     version = Version(version or self.version)
     # NOTE: it's not the same as magic_tag
     try:
         soabi, multiarch = self._get_config(version)[:2]
     except Exception:
         log.debug('cannot get soabi', exc_info=True)
         # interpreter without soabi support
         return ''
     return soabi
Beispiel #11
0
def so2pyver(fpath):
    """Return libpython version file is linked to or None.

    :rtype: tuple
    :returns: Python version
    """

    cmd = "readelf -Wd '%s'" % fpath
    process = Popen(cmd, stdout=PIPE, shell=True)
    match = SHAREDLIB_RE.search(str(process.stdout.read(), encoding='utf-8'))
    if match:
        return Version(match.groups()[0])
Beispiel #12
0
 def _vstr(self, version=None, consider_default_ver=False):
     if self.impl == 'pypy':
         # TODO: will Debian support more than one PyPy version?
         return self.name
     version = version or self.version or ''
     if consider_default_ver and (not version or version == self.default_version):
         version = '3' if self.impl == 'cpython3' else ''
     elif isinstance(version, Version) and version == Version(major=2):
         version = ''  # do not promote /usr/bin/python2
     if self.debug:
         return 'python{}-dbg'.format(version)
     return self.name + str(version)
Beispiel #13
0
    def sitedir(self, package=None, version=None, gdb=False):
        """Return path to site-packages directory.

        Note that returned path is not the final location of .py files

        >>> i = Interpreter('python')
        >>> i.sitedir(version='3.1')
        '/usr/lib/python3/dist-packages/'
        >>> i.sitedir(version='2.5')
        '/usr/lib/python2.5/site-packages/'
        >>> i.sitedir(version=Version('2.7'))
        '/usr/lib/python2.7/dist-packages/'
        >>> i.sitedir(version='3.1', gdb=True, package='python3-foo')
        'debian/python3-foo/usr/lib/debug/usr/lib/python3/dist-packages/'
        >>> i.sitedir(version=Version('3.2'))
        '/usr/lib/python3/dist-packages/'
        """
        try:
            version = Version(version or self.version)
        except Exception as err:
            raise ValueError("cannot find valid version: %s" % err)
        if self.impl == 'pypy':
            path = '/usr/lib/pypy/dist-packages/'
        elif version << Version('2.6'):
            path = "/usr/lib/python%s/site-packages/" % version
        elif version << Version('3.0'):
            path = "/usr/lib/python%s/dist-packages/" % version
        else:
            path = '/usr/lib/python3/dist-packages/'

        if gdb:
            path = "/usr/lib/debug%s" % path
        if package:
            path = "debian/%s%s" % (package, path)

        return path
Beispiel #14
0
 def _get_config(self, version=None):
     version = Version(version or self.version)
     # sysconfig module is available since Python 3.2
     # (also backported to Python 2.7)
     if self.impl == 'pypy' or self.impl.startswith('cpython') and (
             version >> '2.6' and version << '3' or version >> '3.1'
             or version == '3'):
         cmd = 'import sysconfig as s;'
     else:
         cmd = 'from distutils import sysconfig as s;'
     cmd += 'print("__SEP__".join(i or "" ' \
            'for i in s.get_config_vars('\
            '"SOABI", "MULTIARCH", "INCLUDEPY", "LIBPL", "LDLIBRARY")))'
     conf_vars = self._execute(cmd, version).split('__SEP__')
     try:
         conf_vars[1] = os.environ['DEB_HOST_MULTIARCH']
     except KeyError:
         pass
     return conf_vars
Beispiel #15
0
    def _execute(self, command, version=None, cache=True):
        version = Version(version or self.version)
        command = "{} -c '{}'".format(self._vstr(version), command.replace("'", "\'"))
        if cache and command in self.__class__._cache:
            return self.__class__._cache[command]

        output = execute(command)
        if output['returncode'] != 0:
            log.debug(output['stderr'])
            raise Exception('{} failed with status code {}'.format(command, output['returncode']))

        result = output['stdout'].splitlines()

        if len(result) == 1:
            result = result[0]

        if cache:
            self.__class__._cache[command] = result

        return result
Beispiel #16
0
    def old_sitedirs(self, package=None, version=None, gdb=False):
        """Return deprecated paths to site-packages directories."""
        try:
            version = Version(version or self.version)
        except Exception as err:
            raise ValueError("cannot find valid version: %s" % err)
        result = []
        for item in OLD_SITE_DIRS.get(self.impl, []):
            if isinstance(item, str):
                result.append(item.format(version))
            else:
                res = item(version)
                if res is not None:
                    result.append(res)

        if gdb:
            result = ['/usr/lib/debug{}'.format(i) for i in result]
            if self.impl.startswith('cpython'):
                result.append('/usr/lib/debug/usr/lib/pyshared/python{}'.format(version))
        if package:
            result = ['debian/{}{}'.format(package, i) for i in result]

        return result
Beispiel #17
0
 def stableabi(self, version=None):
     version = Version(version or self.version)
     # stable ABI was introduced in Python 3.3
     if self.impl == 'cpython3' and version >> Version('3.2'):
         return 'abi{}'.format(version.major)
Beispiel #18
0
def guess_dependency(impl, req, version=None):
    log.debug('trying to find dependency for %s (python=%s)', req, version)
    if isinstance(version, str):
        version = Version(version)

    # some upstreams have weird ideas for distribution name...
    name, rest = re.compile('([^!><= \(\)\[]+)(.*)').match(req).groups()
    # TODO: check stdlib and dist-packaged for name.py and name.so files
    req = safe_name(name) + rest

    data = load(impl)
    req_d = REQUIRES_RE.match(req)
    if not req_d:
        log.info('please ask dh_python3 author to fix REQUIRES_RE '
                 'or your upstream author to fix requires.txt')
        raise Exception('requirement is not valid: %s' % req)
    req_d = req_d.groupdict()
    name = req_d['name']
    details = data.get(name.lower())
    if details:
        for item in details:
            if version and version not in item.get('versions', version):
                # rule doesn't match version, try next one
                continue

            if not item['dependency']:
                return  # this requirement should be ignored
            if item['dependency'].endswith(')'):
                # no need to translate versions if version is hardcoded in
                # Debian dependency
                return item['dependency']
            if req_d['version'] and (item['standard'] or item['rules']) and\
                    req_d['operator'] not in (None, '=='):
                v = _translate(req_d['version'], item['rules'],
                               item['standard'])
                return "%s (%s %s)" % (item['dependency'], req_d['operator'],
                                       v)
            else:
                return item['dependency']

    # search for Egg metadata file or directory (using dpkg -S)
    query = PYDIST_DPKG_SEARCH_TPLS[impl].format(ci_regexp(safe_name(name)))

    log.debug("invoking dpkg -S %s", query)
    process = Popen("/usr/bin/dpkg -S %s" % query,
                    shell=True,
                    stdout=PIPE,
                    stderr=PIPE)
    stdout, stderr = process.communicate()
    if process.returncode == 0:
        result = set()
        stdout = str(stdout, 'utf-8')
        for line in stdout.split('\n'):
            if not line.strip():
                continue
            result.add(line.split(':')[0])
        if len(result) > 1:
            log.error('more than one package name found for %s dist', name)
        else:
            return result.pop()
    else:
        log.debug('dpkg -S did not find package for %s: %s', name, stderr)

    pname = sensible_pname(impl, name)
    log.info(
        'Cannot find package that provides %s. '
        'Please add package that provides it to Build-Depends or '
        'add "%s %s-fixme" line to %s or add proper '
        ' dependency to Depends by hand and ignore this info.', name,
        safe_name(name), pname, PYDIST_OVERRIDES_FNAMES[impl])