Beispiel #1
0
def request_get(url):
    """
    :param str url: URL to query
    :return str: Response body
    """
    try:
        LOG.debug("GET %s", url)
        request = Request(url)  # nosec
        response = urlopen(request).read()  # nosec
        return response and runez.decode(response).strip()

    except Exception as e:
        code = getattr(e, "code", None)
        if isinstance(code, int) and 400 <= code < 500:
            return None

        try:
            # Some old python installations have trouble with SSL (OSX for example), try curl
            data = runez.run("curl", "-s", url, dryrun=False, fatal=False)
            return data and runez.decode(data).strip()

        except Exception as e:
            LOG.debug("GET %s failed: %s", url, e, exc_info=e)

    return None
Beispiel #2
0
    def get_entry_points(self):
        """
        :return dict|None: Determined entry points for associated pypi package
        """
        if not os.path.isdir(self.build_folder):
            return None

        scripts = {}
        for fname in os.listdir(self.build_folder):
            if fname.endswith(".whl") and self.package_spec.version_part(fname):
                wheel_path = os.path.join(self.build_folder, fname)
                try:
                    with zipfile.ZipFile(wheel_path, "r") as wheel:
                        for wname in wheel.namelist():
                            wdir, wbase = os.path.split(wname)
                            if wbase == "entry_points.txt":
                                with wheel.open(wname) as fh:
                                    eps = runez.file.ini_to_dict(fh, default={})
                                    scripts.update(eps.get("console_scripts"))

                            elif wdir.endswith("/scripts") and "_completer" not in wbase:
                                with wheel.open(wname) as fh:
                                    first_line = runez.decode(fh.readline())
                                    if "python" in first_line:
                                        scripts[wbase] = wname

                except Exception as e:
                    LOG.error("Can't read wheel %s: %s", wheel_path, e, exc_info=e)

        return scripts
Beispiel #3
0
    def run_git_command(self, *args):
        """
        :param args: Execute git command with provided args
        :return str, GitRunReport: Output from git command + report on eventual error
        """
        cmd, pretty_args = self._git_command(args)
        LOG.debug("Running: %s", pretty_args)
        proc = subprocess.Popen(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)  # nosec
        output, error = proc.communicate()
        output = runez.decode(output)
        error = runez.decode(error)
        if proc.returncode == 0:
            return output, GitRunReport()

        if not error:
            return output, GitRunReport(problem="git exited with code %s" %
                                        proc.returncode)

        return output, GitRunReport(problem=shortened_message(error))
Beispiel #4
0
def test_decode():
    assert runez.decode(None) is None
    assert runez.decode(" something ") == " something "
    assert runez.decode(" something ", strip=True) == "something"

    # len() depends on whether python was built with UCS-2 or UCS-4, we don't care here, just want to check decode() works OK with unicode
    assert len(runez.decode(" lucky leaf Рўў is lucky ­Ъўђ ")) in (25, 26)
    assert len(runez.decode(" lucky leaf Рўў is lucky ­Ъўђ ",
                            strip=True)) in (23, 24)

    assert runez.decode(b" something ") == " something "
    assert runez.decode(b" something ", strip=True) == "something"