示例#1
0
def svn_last_changed_revision(project_path='.'):

    """
    The last Subversion revision in which a particular project was changed.

    :param str project_path:
        A file path to a project.

    :rtype: :obj:`int`

    :raise IncompatibleVcsError:
        Raised if the VCS of the project at ``project_path`` is not
        Subversion or git-svn.

    """

    SUPPORTED_SYSTEMS = ('svn', 'git-svn')

    vcs = guess_vcs(project_path)

    if vcs not in SUPPORTED_SYSTEMS:
        raise IncompatibleVcsError(vcs, SUPPORTED_SYSTEMS)

    if vcs == 'svn':
        svn_cmdargs = ('svn',)
    elif vcs == 'git-svn':
        svn_cmdargs = ('git', 'svn')
    else:
        assert False

    cmdargs = svn_cmdargs + ('info',)
    svn_proc = _subprocess.Popen(cmdargs, stdout=_subprocess.PIPE)
    grep_proc = _subprocess.Popen(('grep', 'Last Changed Rev'),
                                  stdin=svn_proc.stdout,
                                  stdout=_subprocess.PIPE)
    sed_proc = _subprocess.Popen(('sed', 's/[^0-9]//g'),
                                 stdin=grep_proc.stdout,
                                 stdout=_subprocess.PIPE)
    proc_output = sed_proc.communicate()[0]
    revision_str = proc_output.strip()

    # handle missing "Last Changed Rev"
    #     this is known to happen with git-svn before the project's second
    #     commit
    if revision_str == '':
        cmdargs = svn_cmdargs + ('log', '--incremental', '--limit', '1')
        svn_proc = _subprocess.Popen(cmdargs, stdout=_subprocess.PIPE)
        proc_output = svn_proc.communicate()[0]
        revision_str = proc_output.split()[1][1:]

    try:
        revision = int(revision_str)
    except ValueError:
        raise Error('invalid Subversion revision {!r} from `{}`'
                     .format(revision_str,
                             ' '.join(_shquote(arg) for arg in cmdargs)))

    return revision
示例#2
0
    def _start(self, fork=False):

        pidfilepath = None
        with open(_os.path.join(self.configloc, 'cn=config.ldif'), 'r') \
                 as config_ldif:
            for line in config_ldif.readlines():
                match = _re.match(r'olcPidFile:\s+(?P<pidfile>.*)\n', line)
                if match:
                    pidfilepath = match.group('pidfile')
                    break

        slapd_args = \
            ('slapd',
             '-h',
             ' '.join(self.uris),
             '-F',
             self.configloc,
             '-d',
             '239' if _logging.getLogger().isEnabledFor(_logging.DEBUG)
                   else '32768')

        if fork:
            slapd_proc = _subprocess.Popen(slapd_args, stdout=_subprocess.PIPE,
                                           stderr=_subprocess.STDOUT,
                                           close_fds=True)
            self._slapd_proc = slapd_proc
            slapd_output = ''
            slapd_started = False
            while not slapd_started:
                slapd_proc.poll()
                if slapd_proc.returncode is not None:
                    slapd_output += slapd_proc.stdout.read()
                    raise RuntimeError('cannot start OpenLDAP service via'
                                        ' {!r}: slapd returned exit code {}'
                                        ' with output\n{}'
                                        .format(' '.join(_shquote(arg)
                                                         for arg
                                                         in slapd_args),
                                                slapd_proc.returncode,
                                                slapd_output))

                current_poll_period_starttime = _time()
                while _time() - current_poll_period_starttime \
                      < self._START_POLL_PERIOD:
                    line = slapd_proc.stdout.readline()
                    slapd_output += line
                    _logging.debug('slapd: ' + line.rstrip('\n'))

                    if 'slapd starting' in line:
                        slapd_started = True
                        slapd_proc.stdout.close()
                        break

                # CAVEAT: sleep even after seeing ``slapd starting`` because on
                #     some systems that message is emitted slightly before
                #     slapd can actually accept connections
                _sleep(self._START_POLL_PERIOD)

            if pidfilepath:
                with open(pidfilepath, 'r') as pidfile:
                    pid = pidfile.read().strip()
            else:
                pid = slapd_proc.pid

            self._set_status('running', pid=pid)

        else:
            _os.execvp(slapd_args[0], slapd_args)