Пример #1
0
def req(key, subject, password):
    p_in = pipe()
    p_out = pipe()

    w_tmp, w_fn = mkstemp()

    write(w_tmp, key)
    close(w_tmp)

    subject = ''.join(['/%s=%s' % (k, v) for k, v in subject.items() if v])

    spawn(['openssl', 'req', '-new', '-key', w_fn, '-passin', 'stdin',
	   '-subj', subject], stdin = p_in, stdout = p_out)

    r_in, w_in = p_in
    r_out, w_out = p_out

    close(r_in)
    close(w_out)

    write(w_in, password)
    close(w_in)

    f_out = fdopen(r_out, 'r')
    csr = f_out.readlines()
    f_out.close()

    remove(w_fn)

    return ''.join(csr)
Пример #2
0
def genrsa(password):
    p_in = pipe()
    p_out = pipe()

    cmd = ['openssl', 'genrsa']

    if password:
	cmd += ['-des3', '-passout', 'stdin']

    cmd += ['1024']

    spawn(cmd, stdin = p_in, stdout = p_out)

    # r_in, w_in file descriptors to new process's standard input
    # likewise r_out, w_out for new process's standard output
    r_in, w_in = p_in
    r_out, w_out = p_out

    close(r_in)
    close(w_out)

    if password:
	write(w_in, password)
    close(w_in)

    f_out = fdopen(r_out, 'r')
    key = f_out.readlines()
    f_out.close()

    return ''.join(key)
Пример #3
0
    def get_definitions(self):
        """Call racer and get back the definition data
        """

        matches = []
        args = shlex.split(
            '{0} -i tab-text find-definition {1} {2} {3} -'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                os.path.dirname(self.filename)
            ), posix=os.name != 'nt'
        )
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ['RUST_SRC_PATH']

        env['RUST_SRC_PATH'] = rust_src_path
        kwargs = {
            'stdin': PIPE, 'stdout': PIPE, 'stderr': PIPE,
            'cwd': os.getcwd(), 'env': env
        }
        try:

            racer = spawn(args, **kwargs)
        except subprocess.CalledProcessError:
            new_env = []
            for elem in env:
                new_env.append(str(elem))
            racer = spawn(args, **kwargs)

        src = self.settings['source']
        if sys.version_info >= (3, 0):
            src = self.settings['source'].encode()

        output, error = racer.communicate(src)
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            error = error.decode('utf8')

        if error != '':
            raise Exception(error)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            _, elem, row, col, path, _, _ = line.split('\t')
            matches.append((path, int(row), int(col)))

        return matches
    def execute(self):
        """Execute the linting process
        """

        phpcs = os.path.join(os.path.dirname(__file__), 'phpcs/scripts/phpcs')
        args = [
            'php', phpcs, '--extensions=php,inc,lib,js,css', '--report=json',
            '--standard={0}'.format(
                self.settings.get('phpcs_standard', 'PRS2')
            )
        ]
        args.append('--severity={0}'.format(
            self.settings.get('phpcs_severity', 1))
        )

        if self.settings.get('phpcs_tabs2spaces', True):
            args.append('--tab-width=4')

        if self.settings.get('phpcs_no_warnings', False):
            args.append('-n')

        for arg in self.settings.get('phpcs_additional_arguments', []):
            args.append(arg)

        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #5
0
    def guru(self):
        """Use Guru to look for the definition of the word under the cursor
        """

        scope = ''
        if self.scope is not None and self.scope != '':
            scope = ' -scope {0}'.format(self.scope)

        args = shlex.split('"{0}"{1} -json -modified {2} "{3}:#{4}"'.format(
            self.binary, scope,
            self.mode, self.path, self.offset)
        )
        print(' '.join(args))
        guru = spawn(
            args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.env
        )
        out, err = guru.communicate(self.modified_buffer)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise GuruError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        try:
            data = json.loads(out)
        except:
            data = json.loads(
                '[' + out.replace('}\n{', '},\n{').replace('\t', '') + ']'
            )

        if type(data) is dict:
            data['tool'] = 'guru'
        return data
Пример #6
0
    def godef(self):
        """Use godef to look for the definition of the word under the cursor
        """

        args = shlex.split('\'{0}\' -json -i -p \'{1}\' {2}{3}'.format(
            self.binary, self.path, '-A' if self.extended else '', self.expr)
        )
        godef = spawn(
            args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.env
        )
        out, err = godef.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise GoDefError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        if not self.extended:
            if out == '{}':
                return {'src': 'builtin', 'line': 0, 'col': 0}

        data = json.loads(out)
        data['tool'] = 'godef'
        return data
Пример #7
0
    def guru(self):
        """Use Guru to look for the definition of the word under the cursor
        """

        scope = ''
        if self.scope is not None and self.scope != '':
            scope = ' -scope {0}'.format(self.scope)

        args = shlex.split('"{0}"{1} -json -modified {2} "{3}:#{4}"'.format(
            self.binary, scope, self.mode, self.path, self.offset))
        print(' '.join(args))
        guru = spawn(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = guru.communicate(self.modified_buffer)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise GuruError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        try:
            data = json.loads(out)
        except:
            data = json.loads('[' +
                              out.replace('}\n{', '},\n{').replace('\t', '') +
                              ']')

        if type(data) is dict:
            data['tool'] = 'guru'
        return data
Пример #8
0
    def rustfmt(self):
        """Run the rustfmt command in a file
        """

        args = shlex.split(
            '{0} --write-mode=plain --config-path {1} {2}'.format(
                self.settings.get('rustfmt_binary_path', 'rustfmt'),
                self.settings.get('config_path'), self.filename),
            posix=os.name != 'nt')

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        header = 'Using rustfmt config file '
        result = '\n'.join(
            [l for l in output.splitlines() if not l.startswith(header)])

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            self.error = err
            raise Exception(err)

        return result
Пример #9
0
    def autocomplete(self):
        """Autocomplete the word under cursor using the given data
        """

        args = shlex.split('\'{0}\' -f json autocomplete \'{1}\' c{2}'.format(
            self.binary, self.path, self.offset))
        print(' '.join(args))
        gocode = spawn(args,
                       stdin=PIPE,
                       stdout=PIPE,
                       stderr=PIPE,
                       env=self.env)
        out, err = gocode.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise AutoCompleteError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        try:
            completions = json.loads(out)
        except Exception as error:
            raise AutoCompleteError(error)

        comps = []
        if len(completions) > 0:
            lguide = self._calculate_lguide(completions[1])
            for elem in completions[1]:
                comps.append(('{0}{1} {2} {3}'.format(
                    elem['name'], ' ' * (lguide - len(elem['name'])),
                    elem['class'], elem['type']), self._snippet(elem)))

        return comps
Пример #10
0
    def execute(self):
        """Execute the linting process
        """

        phpcs = os.path.join(os.path.dirname(__file__), 'phpcs/scripts/phpcs')
        args = [
            'php', phpcs, '--extensions=php,inc,lib,js,css', '--report=json',
            '--standard={0}'.format(
                self.settings.get('phpcs_standard', 'PSR2')
            )
        ]
        args.append('--severity={0}'.format(
            self.settings.get('phpcs_severity', 1))
        )

        if self.settings.get('phpcs_tabs2spaces', True):
            args.append('--tab-width=4')

        if self.settings.get('phpcs_no_warnings', False):
            args.append('-n')

        for arg in self.settings.get('phpcs_additional_arguments', []):
            args.append(arg)

        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #11
0
 def _check_queue(self, db):
     """Look up waiting jobs and start as many as possible."""
     if (self.max_running is not None) and \
        (len(self.running) >= self.max_running):
         return
     c = db.cursor()
     c.execute("""SELECT job_id, script, directory
                  FROM job NATURAL JOIN user
                  WHERE status='waiting'
                  ORDER BY job_id LIMIT 1""")
     row = c.fetchone()
     if row is None:
         return
     job_id, script, path = row[0], str(row[1]), row[2]
     cmd = self.cmd
     args = self.args
     callback = make_callback(self, self.dbname, job_id)
     self.running[job_id] = \
         process.spawn(job_id, cmd, args, script, path, callback)
     c.execute("""UPDATE job
                  SET status='running', start_time=datetime('now')
                  WHERE job_id=?""",
               (job_id,))
     db.commit()
     self._check_queue(db)
Пример #12
0
    def gometalinter(self):
        """Run gometalinter and return back a JSON object with it's results
        """

        args = shlex.split(
            '\'{0}\' {1}'.format(self.binary, self.options)
        )
        print(' '.join(args))
        gometalinter = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = gometalinter.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')

            if 'deadline' not in err:
                raise GometaLinterError(err)
            else:
                logging.info(
                    'Some linters are running out of time with deadline '
                    'errros, please, consider to run just fast linters as '
                    'your system seems to be a bit slow'
                )

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        return self._normalize(json.loads(out))
    def _install_linters(self):
        """Install gometalinter linters
        """

        args = shlex.split('{0} --install'.format(self.binary))
        gometalinter = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        _, err = gometalinter.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise GometaLinterError(err)
Пример #14
0
    def _install_linters(self):
        """Install gometalinter linters
        """

        args = shlex.split('{0} --install'.format(self.binary))
        gometalinter = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        _, err = gometalinter.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise GometaLinterError(err)
    def execute(self):
        """Execute the linting process
        """

        args = [
            'php', '-l', '-n', '-d', 'dislay_errors=On', '-d', 'log_errors=Off'
        ]
        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #16
0
    def completions(self):
        """Call racer and get back a formated list of completions
        """

        completions = []
        args = shlex.split(
            '{0} -i tab-text complete-with-snippet {1} {2} {3}'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                self.filename
            ), posix=os.name != 'nt'
        )
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ.get('RUST_SRC_PATH', '')

        env['RUST_SRC_PATH'] = rust_src_path
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd(), env=env)
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            raise Exception(err)

        lguide = self._calculate_lguide(output)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            try:
                _, completion, snippet, _, _, _, _type, info = line.split('\t')
            except ValueError:
                # racer 1.2.10 added a new `doc` field
                _, completion, snippet, _, _, _, _type, info, doc = \
                    line.split('\t')

            completions.append((
                '{0}{1} {2} {3}'.format(
                    completion, ' ' * (lguide - len(completion)),
                    _type[0].lower(), info
                ), snippet
            ))

        return completions
Пример #17
0
    def execute(self):
        """Execute the linting process
        """

        args = [
            'php', '-l', '-n', '-d', 'display_errors=On', '-d', 'log_errors=Off'
        ]
        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #18
0
    def go_get(self):
        """Go get the code to execute the scoped context
        """

        args = shlex.split('{0} get {1}'.format(
            self.go, self.__go_get_url), posix=os.name != 'nt')
        go = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = go.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, 0):
                err = err.decode('utf8')
            raise GoGetError(err)
        self._bin_found = True
Пример #19
0
    def go_get(self):
        """Go get the code to execute the scoped context
        """

        args = shlex.split('{0} get {1}'.format(self.go, self.__go_get_url),
                           posix=os.name != 'nt')
        go = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = go.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, 0):
                err = err.decode('utf8')
            raise GoGetError(err)
        self._bin_found = True
Пример #20
0
    def completions(self):
        """Call racer and get back a formated list of completions
        """

        completions = []
        args = shlex.split(
            '{0} -i tab-text complete-with-snippet {1} {2} {3}'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                self.filename),
            posix=os.name != 'nt')
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ.get('RUST_SRC_PATH', '')

        env['RUST_SRC_PATH'] = rust_src_path
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd(), env=env)
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            raise Exception(err)

        lguide = self._calculate_lguide(output)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            try:
                _, completion, snippet, _, _, _, _type, info = line.split('\t')
            except ValueError:
                # racer 1.2.10 added a new `doc` field
                _, completion, snippet, _, _, _, _type, info, doc = \
                    line.split('\t')

            completions.append(
                ('{0}{1} {2} {3}'.format(completion,
                                         ' ' * (lguide - len(completion)),
                                         _type[0].lower(), info), snippet))

        return completions
Пример #21
0
    def doc(self):
        """Call racer and get back documentation (only on racer >= 1.2.10
        """

        args = shlex.split(
            '{0} -i tab-text complete-with-snippet {1} {2} {3}'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                self.filename
            ), posix=os.name != 'nt'
        )
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ.get('RUST_SRC_PATH', '')

        env['RUST_SRC_PATH'] = rust_src_path
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd(), env=env)
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            raise Exception(err)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            try:
                # racer 1.2.10 added a new `doc` field
                _, _, _, _, _, _, _, info, doc = line.split('\t')
            except ValueError:
                raise RuntimeError('doc is available in racer >= 1.2.10 only')

            if not doc:
                doc = info

            if doc == "\"\"":
                doc = ""

            return ast.literal_eval(doc)
Пример #22
0
    def execute(self):
        """Execute the linting process
        """

        current_dir = os.getcwd()
        os.chdir(os.path.dirname(self.filename))
        args = shlex.split('{0} -Zparse-only {1}'.format(
            self.settings.get('rustc_binary_path', 'rustc'),
            os.path.basename(self.filename)), posix=os.name != 'nt'
        )
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        _, self.output = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')

        os.chdir(current_dir)
Пример #23
0
    def impl(self):
        """Run the impl command and return back a string with the code
        """

        args = shlex.split('\'{0}\' \'{1}\' {2}'.format(
            self.binary, self.receiver, self.iface))
        impl = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = impl.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise ImplError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        return out
Пример #24
0
    def phpcs_fixer(self):
        """Run the php-cs-fixer command in a file
        """

        phpcs_fixer = os.path.join(
            os.path.dirname(__file__), '../linting/phpcs_fixer/php-cs-fixer')
        args = [
            'php', '-n', phpcs_fixer, 'fix', self.filename, '--level=all',
            '-{}'.format(self.settings.get('phpcs_fixer_verbosity_level'), 'v')
        ] + self.settings.get('phpcs_fixer_additional_arguments', [])

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, error = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')

        return output
Пример #25
0
    def goimports(self):
        """Run the goimports command and return back a string with the code
        """

        args = [self.binary, self.path]
        goimports = spawn(
            args, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=self.env)
        out, err = goimports.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise GoimportsError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        return out
Пример #26
0
    def phpcs_fixer(self):
        """Run the php-cs-fixer command in a file
        """

        phpcs_fixer = os.path.join(os.path.dirname(__file__),
                                   '../linting/phpcs_fixer/php-cs-fixer')
        args = [
            'php', '-n', phpcs_fixer, 'fix', self.filename, '--level=all',
            '-{}'.format(self.settings.get('phpcs_fixer_verbosity_level'), 'v')
        ] + self.settings.get('phpcs_fixer_additional_arguments', [])

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, error = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')

        return output
Пример #27
0
    def execute(self, path=None):
        if "hooks" not in self.settings['FEATURES']:
            return

        if not path:
            path = self.path

        path = normalize_path(path)

        if not os.path.exists(path):
            if self.myopts and "--debug" in self.myopts:
                # behavior mimicked by hook.sh
                self.output.ewarn(
                    'This hook path could not be found; ignored: ' + path)
            return

        if os.path.isdir(path):
            command = [HOOKS_SH_BINARY]
            if self.myopts:
                for myopt in self.myopts:
                    command.extend(['--opt', myopt])
            if self.myaction:
                command.extend(['--action', self.myaction])
            if self.mytargets:
                for mytarget in self.mytargets:
                    command.extend(['--target', mytarget])

            command = [
                BASH_BINARY, '-c',
                'cd "' + path + '" && source "' + PORTAGE_BIN_PATH +
                '/isolated-functions.sh" && source ' + ' '.join(command)
            ]
            if self.myopts and "--verbose" in self.myopts:
                self.output.einfo('Executing hooks directory "' + self.path +
                                  '"...')
            code = spawn(mycommand=command, env=self.settings.environ())
            if code:  # if failure
                # behavior mimicked by hook.sh
                raise PortageException(
                    '!!! Hook directory %s failed with exit code %s' %
                    (self.path, code))

        else:
            raise InvalidLocation('This hook path ought to be a directory: ' +
                                  path)
Пример #28
0
    def impl(self):
        """Run the impl command and return back a string with the code
        """

        args = shlex.split('\'{0}\' \'{1}\' {2}'.format(
            self.binary, self.receiver, self.iface)
        )
        impl = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = impl.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise ImplError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        return out
Пример #29
0
    def _version_regex(self):
        """Execute the right regexp.finditer depenging on rustc version
        """

        r = self.settings.get('rustc_binary_path', 'rustc')
        args = shlex.split('{0} --version'.format(r), posix=os.name != 'nt')
        p = spawn(args, stdout=PIPE, stderr=PIPE)
        out, _ = p.communicate()
        if sys.version_info >= (3,):
            out = out.decode('utf8')

        if 'rustc' not in out:
            return []

        v = tuple(int(i) for i in out.split(' ')[1].split('-')[0].split('.'))
        if v >= (1, 13):
            return self._regexp_1_13.finditer(self.output)

        return self._regexp.finditer(self.output)
    def execute(self):
        """Execute the linting process
        """

        rules = ','.join(self.settings.get(
            'phpmd_ruleset', ['unusedcode', 'naming', 'codesize'])
        )
        phpmd = os.path.join(os.path.dirname(__file__), 'phpmd/phpmd.phar')
        args = [
            'php', '-d date.timezone=UTC', phpmd, self.filename, 'text', rules
        ]

        for arg in self.settings.get('phpmd_additional_arguments', []):
            args.append(arg)

        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #31
0
    def execute(self):
        """Execute the linting process
        """

        rules = ','.join(
            self.settings.get('phpmd_ruleset',
                              ['unusedcode', 'naming', 'codesize']))
        phpmd = os.path.join(os.path.dirname(__file__), 'phpmd/phpmd.phar')
        args = [
            'php', '-d date.timezone=UTC', phpmd, self.filename, 'text', rules
        ]

        for arg in self.settings.get('phpmd_additional_arguments', []):
            args.append(arg)

        args.append(self.filename)
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        self.output, self.error = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #32
0
    def motion(self):
        """Run the motion command and return back json object with the results
        """

        args = shlex.split('"{0}" {1} \'{2}\' {3} -mode {4} {5}{6}{7}'.format(
            self.binary, self.scope, self.path,
            self.offset, self.mode, self.include, self.parse_comments,
            ' -shift 1' if self.mode == 'prev' else ''
        ))
        motion = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = motion.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise MotionError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        return json.loads(out)
Пример #33
0
    def goimports(self):
        """Run the goimports command and return back a string with the code
        """

        args = [self.binary, self.path]
        goimports = spawn(args,
                          stdout=PIPE,
                          stderr=PIPE,
                          stdin=PIPE,
                          env=self.env)
        out, err = goimports.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise GoimportsError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        return out
Пример #34
0
def restore_req(request):
    errors = ''

    if 'file' in request.FILES:
        p = request.FILES['file']

        td = mkdtemp()

        try:
            fn = join(td, p.get('filename', 'console-backup.tar.bz2'))

            f = file(fn, 'w')
            try:

                try:
                    f.write(p['content'])
                finally:
                    f.close()

                p_err = pipe()
                pid = spawn([
                    join(settings.INSTALL_DIR, 'bin', 'mc-restore-console'), fn
                ],
                            stderr=p_err)
                r_err, w_err = p_err
                close(w_err)
                errors = fdopen(r_err).read()

                pid1, status = waitpid(pid, 0)

                if status == 0:
                    return HttpResponseRedirect('.')

            finally:
                remove(fn)

        finally:
            rmdir(td)

    return render_to_response('config/restore.html',
                              default_context(request, errors=errors))
Пример #35
0
    def rustfmt(self):
        """Run the rustfmt command in a file
        """

        args = shlex.split('{0} --write-mode=display {1}'.format(
            self.settings.get('rustfmt_binary_path', 'rustfmt'), self.filename
        ), posix=os.name != 'nt')
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        os.remove(self.filename)

        if err != '':
            self.error = err
            raise Exception(err)

        return '\n'.join(output.splitlines()[2:])
Пример #36
0
    def get_definitions(self):
        """Call racer and get back the definition data
        """

        matches = []
        args = shlex.split(
            '{0} -i tab-text find-definition {1} {2} {3}'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                self.filename
            ), posix=os.name != 'nt'
        )
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ['RUST_SRC_PATH']

        env['RUST_SRC_PATH'] = rust_src_path
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd(), env=env)
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            raise Exception(err)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            _, elem, row, col, path, _, _ = line.split('\t')
            matches.append((path, int(row), int(col)))

        return matches
Пример #37
0
    def execute(self):
        """Execute the linting process
        """

        if numversion < (1, 0, 0):
            args = "--include-ids=y -r n".split(" ")
        else:
            args = "--msg-template={msg_id}:{line}:{column}:{msg} -r n".split(" ")

        if self.rcfile:
            args.append("--rcfile={0}".format(os.path.expanduser(self.rcfile)))

        args.append(self.filename)
        args = [sys.executable, "-m", "pylint.lint"] + args

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        if proc is None:
            return {"E": [], "W": [], "V": []}

        self.output, _ = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode("utf8")
Пример #38
0
def restore_req(request):
    errors = ''

    if 'file' in request.FILES:
	p = request.FILES['file']

	td = mkdtemp()

	try:
	    fn = join(td, p.get('filename', 'console-backup.tar.bz2'))

	    f = file(fn, 'w')
	    try:

		try:
		    f.write(p['content'])
		finally:
		    f.close()

		p_err = pipe()
		pid = spawn([join(settings.INSTALL_DIR, 'bin', 'mc-restore-console'),
			     fn], stderr = p_err)
		r_err, w_err = p_err
		close(w_err)
		errors = fdopen(r_err).read()

		pid1, status = waitpid(pid, 0)

		if status == 0:
		    return HttpResponseRedirect('.')

	    finally:
		remove(fn)

	finally:
	    rmdir(td)

    return render_to_response('config/restore.html',
			      default_context(request, errors=errors))
Пример #39
0
    def rustfmt(self):
        """Run the rustfmt command in a file
        """

        # use the `--config-path` option only when a path is given
        config_path = self.settings.get('config_path')
        config_path_option = ''
        if config_path.strip():
            config_path_option = '--config-path={}'.format(config_path)

        args = shlex.split(
            '{0} --write-mode=plain {1} {2}'.format(
                self.settings.get('rustfmt_binary_path', 'rustfmt'),
                config_path_option,
                self.filename
            ),
            posix=os.name != 'nt')

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, err = proc.communicate()
        if proc.returncode != 0:
            err = output  # rustfmt's err message goes to stdout too
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        header = 'Using rustfmt config file '
        result = '\n'.join(
            [l for l in output.splitlines() if not l.startswith(header)])

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            self.error = err
            raise Exception(err)

        return result
Пример #40
0
    def doc(self):
        """Run the doc command and return back the results as a string
        """

        args = shlex.split('\'{0}\' doc {1}{2}'.format(
            self.binary, '-u ' if self.private else '', self.expr
        ))
        print(' '.join(args))
        godoc = spawn(
            args, stdout=PIPE, stderr=PIPE, env=self.env,
            cwd=os.path.dirname(self.path)
        )
        out, err = godoc.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise DocError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        return out
Пример #41
0
    def doc(self):
        """Run the doc command and return back the results as a string
        """

        args = shlex.split('\'{0}\' doc {1}{2}'.format(
            self.binary, '-u ' if self.private else '', self.expr))
        print(' '.join(args))
        godoc = spawn(args,
                      stdout=PIPE,
                      stderr=PIPE,
                      env=self.env,
                      cwd=os.path.dirname(self.path))
        out, err = godoc.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise DocError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        return out
Пример #42
0
    def execute(self):
        """Execute the linting process
        """

        if numversion < (1, 0, 0):
            args = '--include-ids=y -r n'.split(' ')
        else:
            args = '--msg-template={msg_id}:{line}:{column}:{msg} -r n'.split(
                ' ')

        if self.rcfile:
            args.append('--rcfile={0}'.format(os.path.expanduser(self.rcfile)))

        args.append(self.filename)
        args = [sys.executable, '-m', 'pylint.lint'] + args

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        if proc is None:
            return {'E': [], 'W': [], 'V': []}

        self.output, _ = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #43
0
    def execute(self):
        """Execute the linting process
        """

        if numversion < (1, 0, 0):
            args = '--include-ids=y -r n'.split(' ')
        else:
            args = '--msg-template={msg_id}:{line}:{column}:{msg} -r n'.split(
                ' ')

        if self.rcfile:
            args.append('--rcfile={0}'.format(os.path.expanduser(self.rcfile)))

        args.append(self.filename)
        args = [sys.executable, '-m', 'pylint.lint'] + args

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        if proc is None:
            return {'E': [], 'W': [], 'V': []}

        self.output, _ = proc.communicate()
        if sys.version_info >= (3, 0):
            self.output = self.output.decode('utf8')
Пример #44
0
    def godef(self):
        """Use godef to look for the definition of the word under the cursor
        """

        args = shlex.split('\'{0}\' -json -i -p \'{1}\' {2}{3}'.format(
            self.binary, self.path, '-A' if self.extended else '', self.expr))
        godef = spawn(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = godef.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')
            raise GoDefError(err)

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        if not self.extended:
            if out == '{}':
                return {'src': 'builtin', 'line': 0, 'col': 0}

        data = json.loads(out)
        data['tool'] = 'godef'
        return data
Пример #45
0
    def rustfmt(self):
        """Run the rustfmt command in a file
        """

        args = shlex.split('{0} --write-mode=display {1}'.format(
            self.settings.get('rustfmt_binary_path', 'rustfmt'),
            self.filename),
                           posix=os.name != 'nt')
        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, err = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            self.error = err
            raise Exception(err)

        return '\n'.join(output.splitlines()[2:])
Пример #46
0
    def autocomplete(self):
        """Autocomplete the word under cursor using the given data
        """

        args = shlex.split('\'{0}\' -f json autocomplete \'{1}\' c{2}'.format(
            self.binary, self.path, self.offset)
        )
        print(' '.join(args))
        gocode = spawn(
            args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.env
        )
        out, err = gocode.communicate(self.code)
        if err is not None and len(err) > 0:
            if sys.version_info >= (3,):
                err = err.decode('utf8')
            raise AutoCompleteError(err)

        if sys.version_info >= (3,):
            out = out.decode('utf8')

        try:
            completions = json.loads(out)
        except Exception as error:
            raise AutoCompleteError(error)

        comps = []
        if len(completions) > 0:
            lguide = self._calculate_lguide(completions[1])
            for elem in completions[1]:
                comps.append((
                    '{0}{1} {2} {3}'.format(
                        elem['name'], ' ' * (lguide - len(elem['name'])),
                        elem['class'], elem['type']
                    ), self._snippet(elem)
                ))

        return comps
    def gometalinter(self):
        """Run gometalinter and return back a JSON object with it's results
        """

        args = shlex.split('\'{0}\' {1}'.format(self.binary, self.options))
        print(' '.join(args))
        gometalinter = spawn(args, stdout=PIPE, stderr=PIPE, env=self.env)
        out, err = gometalinter.communicate()
        if err is not None and len(err) > 0:
            if sys.version_info >= (3, ):
                err = err.decode('utf8')

            if 'deadline' not in err:
                raise GometaLinterError(err)
            else:
                logging.info(
                    'Some linters are running out of time with deadline '
                    'errros, please, consider to run just fast linters as '
                    'your system seems to be a bit slow')

        if sys.version_info >= (3, ):
            out = out.decode('utf8')

        return self._normalize(json.loads(out))
Пример #48
0
    def php_copy_and_paste_detector(self):
        """Run the phpcpd command in a file or directory
        """

        phpcpd = os.path.join(os.path.dirname(__file__), "../linting/phpcpd/phpcpd.phar")
        args = ["php", "-n", "-d", "memory_limit=-1", phpcpd, self.filename]
        configured_verbosity = self.settings.get("phpcpd_verbosity_level", 0)
        if configured_verbosity > 0:
            verbosity_lvl = "-{}".format("v" * configured_verbosity)
            args.append(verbosity_lvl)

        args += [
            "--min-lines",
            str(self.settings.get("phpcpd_min_lines", 5)),
            "--min-tokens",
            str(self.settings.get("phpcpd_min_tokens", 70)),
        ] + self.settings.get("phpcpd_additional_arguments", [])

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, error = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode("utf8")

        return output if not self.lint else self.to_lint_fmt(output)
Пример #49
0
	def execute (self, path=None):
		if "hooks" not in self.settings['FEATURES']:
			return
		
		if not path:
			path = self.path
		
		path = normalize_path(path)
		
		if not os.path.exists(path):
			if self.myopts and "--debug" in self.myopts:
				# behavior mimicked by hook.sh
				self.output.ewarn('This hook path could not be found; ignored: ' + path)
			return
		
		if os.path.isdir(path):
			command=[HOOKS_SH_BINARY]
			if self.myopts:
				for myopt in self.myopts:
					command.extend(['--opt', myopt])
			if self.myaction:
				command.extend(['--action', self.myaction])
			if self.mytargets:
				for mytarget in self.mytargets:
					command.extend(['--target', mytarget])
			
			command=[BASH_BINARY, '-c', 'cd "'+path+'" && source "' + PORTAGE_BIN_PATH + '/isolated-functions.sh" && source ' + ' '.join(command)]
			if self.myopts and "--verbose" in self.myopts:
				self.output.einfo('Executing hooks directory "' + self.path + '"...')
			code = spawn(mycommand=command, env=self.settings.environ())
			if code: # if failure
				# behavior mimicked by hook.sh
				raise PortageException('!!! Hook directory %s failed with exit code %s' % (self.path, code))
		
		else:
			raise InvalidLocation('This hook path ought to be a directory: ' + path)
Пример #50
0
    def rustfmt(self):
        """Run the rustfmt command in a file
        """

        # use the `--config-path` option only when a path is given
        config_path = self.settings.get('config_path')
        config_path_option = ''
        if config_path.strip():
            config_path_option = '--config-path={}'.format(config_path)

        args = shlex.split('{0} --write-mode=plain {1} {2}'.format(
            self.settings.get('rustfmt_binary_path', 'rustfmt'),
            config_path_option, self.filename),
                           posix=os.name != 'nt')

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, err = proc.communicate()
        if proc.returncode != 0:
            err = output  # rustfmt's err message goes to stdout too
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            err = err.decode('utf8')

        header = 'Using rustfmt config file '
        result = '\n'.join(
            [l for l in output.splitlines() if not l.startswith(header)])

        # delete temporary file
        if os.path.exists(self.filename):
            os.remove(self.filename)

        if err != '':
            self.error = err
            raise Exception(err)

        return result
Пример #51
0
    def php_copy_and_paste_detector(self):
        """Run the phpcpd command in a file or directory
        """

        phpcpd = os.path.join(os.path.dirname(__file__),
                              '../linting/phpcpd/phpcpd.phar')
        args = ['php', '-n', '-d', 'memory_limit=-1', phpcpd, self.filename]
        configured_verbosity = self.settings.get('phpcpd_verbosity_level', 0)
        if configured_verbosity > 0:
            verbosity_lvl = '-{}'.format('v' * configured_verbosity)
            args.append(verbosity_lvl)

        args += [
            '--min-lines',
            str(self.settings.get('phpcpd_min_lines', 5)), '--min-tokens',
            str(self.settings.get('phpcpd_min_tokens', 70))
        ] + self.settings.get('phpcpd_additional_arguments', [])

        proc = spawn(args, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
        output, error = proc.communicate()
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')

        return output if not self.lint else self.to_lint_fmt(output)
Пример #52
0
    def doc(self):
        """Call racer and get back documentation (only on racer >= 1.2.10)
        """

        args = shlex.split(
            '{0} -i tab-text complete-with-snippet {1} {2} {3} -'.format(
                self.settings.get('racer_binary_path', 'racer'),
                self.settings.get('row', 0) + 1,  # ST3 counts rows from 0
                self.settings.get('col', 0),
                self.filename
            ), posix=os.name != 'nt'
        )
        env = os.environ.copy()
        rust_src_path = self.settings.get('rust_src_path')
        if rust_src_path is None or rust_src_path == '':
            rust_src_path = os.environ.get('RUST_SRC_PATH', '')

        env['RUST_SRC_PATH'] = rust_src_path
        kwargs = {
            'stdin': PIPE, 'stdout': PIPE, 'stderr': PIPE,
            'cwd': os.getcwd(), 'env': env
        }
        try:

            racer = spawn(args, **kwargs)
        except subprocess.CalledProcessError:
            new_env = []
            for elem in env:
                new_env.append(str(elem))
            racer = spawn(args, **kwargs)

        src = self.settings['source']
        src = self.settings['source'].encode('utf8')

        output, error = racer.communicate(src)
        if sys.version_info >= (3, 0):
            output = output.decode('utf8')
            error = error.decode('utf8')

        if error != '':
            raise Exception(error)

        for line in output.splitlines():
            if not line.startswith('MATCH'):
                continue

            try:
                # racer 1.2.10 added a new `doc` field
                _, _, _, _, _, _, _, info, doc = line.split('\t')
            except ValueError:
                raise RuntimeError('doc is available in racer >= 1.2.10 only')

            if not doc:
                doc = info

            if doc == "\"\"":
                doc = ""

            if doc == '':
                return doc

            return ast.literal_eval(doc)