Beispiel #1
0
def test_regression_scenarios_current():

    value = "__version__ = '''0.0.0'''"
    result, _ = find_in_line(value)
    assert result == "0.0.0", result

    value = '__version__ = """0.0.0"""'
    result, _ = find_in_line(value)
    assert result == "0.0.0", result
Beispiel #2
0
def test_that_is_a_tuple():
    # single quotes
    result, _ = find_in_line("__version__ = (1, 2, 3)")
    assert result == "1.2.3", result

    result, _ = find_in_line("__version__ = (1, 2,)")
    assert result == "1.2", result

    result, _ = find_in_line("__version__=(1, 2,)")
    assert result == "1.2", result

    result, _ = find_in_line("__version__=(1, 2,) # comments")
    assert result == "1.2", result
Beispiel #3
0
    def find_dunder_version_in_file(self, full_path: str) -> Dict[str, str]:
        """
        Find __version__ in a source file
        """
        versions = {}

        with self.file_opener.open_this(full_path, "r") as infile:
            for line in infile:
                version, _ = dunder_version.find_in_line(line)
                if version:
                    versions[full_path] = version
                version, _ = dunder_version.find_in_line(line)
        return versions
Beispiel #4
0
def test_that_is_a_int_or_float_b():
    # single quotes
    result, _ = find_in_line("__version__ = 1.2")
    assert result == "1.2", result

    result, _ = find_in_line("__version__ = 1")
    assert result == "1", result

    result, _ = find_in_line("__version__=2")
    assert result == "2", result

    result, _ = find_in_line("__version__=2 # comments")
    assert result == "2", result

    result, _ = find_in_line("__version__=2 # __version__ = 3")
    assert result == "2", result
Beispiel #5
0
    def read_setup_py(self) -> Dict[str, str]:
        """
        Extract from setup.py's setup() arg list.
        :return:
        """
        found: Dict[str, str] = {}
        setup_py = os.path.join("setup.py")

        if not os.path.isfile(setup_py):
            if self.strict:
                logger.debug(str(os.getcwd()))
                logger.debug(str(os.listdir(os.getcwd())))
                # return ""
                # raise JiggleVersionException(
                #     "Can't find setup.py : {0}, path :{1}".format(setup_py, self.SRC)
                # )
                return found
            return found

        with self.file_opener.open_this(setup_py, "r") as file_handle:
            self.setup_py_source = file_handle.read()

        if "use_scm_version=True" in ifnull(self.setup_py_source, ""):
            die(
                -1,
                "setup.py has use_scm_version=True in it- this means we expect no file to have a version string. "
                "Nothing to change",
            )
            return {}

        with self.file_opener.open_this(setup_py, "r") as infile:
            for line in infile:
                version = kwarg_version.find_in_line(line)
                if version:
                    found[setup_py] = version
                    continue
                version, _ = dunder_version.find_in_line(line)
                if version:
                    found[setup_py] = version
                    continue

        # stress testing
        # if "version" in self.setup_py_source and not found:
        #     raise TypeError(self.setup_py_source)

        return found
    def jiggle_source_code(self) -> int:
        """
        Updates version of central package
        """
        changed = 0
        for file_name in self.file_inventory.source_files:
            to_write = []
            # self.create_missing(file_name, file_name)
            if not os.path.isfile(file_name):
                continue

            all_source = self.file_opener.read_this(file_name)
            if "__version_info__" in all_source:
                logger.warning("We have __version_info__ to sync up.")
                # raise TypeError()

            with self.file_opener.open_this(file_name, "r") as infile:
                for line in infile:
                    leading_white = self.leading_whitespace(line)
                    version, version_token = dunder_version.find_in_line(line)
                    if version:
                        simplified_line = dunder_version.simplify_line(
                            line, keep_comma=True)
                        if simplified_line.strip(" \t\n").endswith(","):
                            comma = ","
                        else:
                            comma = ""

                        if simplified_line.strip(" \t\n").startswith(","):
                            start_comma = ","
                        else:
                            start_comma = ""

                        to_write.append(
                            f"{start_comma}{leading_white}{version_token} "
                            f'= "{str(self.version_to_write())}"{comma}{self.signature_txt}\n'
                        )
                    else:
                        to_write.append(line)

            with self.file_opener.open_this(file_name, "r") as file_handle:
                check(file_handle.read(), "".join(to_write))
            with open(file_name, "w", encoding="utf-8") as outfile:
                outfile.writelines(to_write)
                changed += 1
        return changed
Beispiel #7
0
def test_regression_scenarios_old():
    value = "__version__ = '%(version)s%(tag)s%(build)s' % {"
    result, _ = find_in_line(value)
    assert result is None, result

    result, _ = find_in_line(
        '__version__ = ".".join(map(str, version_info[:3])) + ("-"+version_info[3] if'
    )
    assert result is None, result

    result, _ = find_in_line("VERSION = (1, 1, 0, 'rc1')")
    assert result == "1.1.0.rc1", result

    # passing
    result, _ = find_in_line("VERSION = (1, 0, 1)")
    assert result == "1.0.1", result
    result, _ = find_in_line(
        "    version = re.search(r'^__version__\s*=\s*['\"]([^'\"]*)['\"]',")
    assert result is None, result
    result, _ = find_in_line("version = re.search(r'^__version__\s*=\s*[")
    assert result is None, result
Beispiel #8
0
def test_common_scenarios():
    # single quotes
    result, _ = find_in_line("__version__ = '1.2.3'")
    assert result == "1.2.3", result

    # double quotes
    result, _ = find_in_line('__version__ = "1.2.3"')
    assert result == "1.2.3", result

    # whitespace
    result, _ = find_in_line(' __version__    =    "1.2.3"    ')
    assert result == "1.2.3", result

    # tabs
    result, _ = find_in_line(' \t __version__ \t   =    "1.2.3"  \t  ')
    assert result == "1.2.3", result

    # no whitespace
    result, _ = find_in_line('__version__="1.2.3"')
    assert result == "1.2.3", result

    # comments
    result, _ = find_in_line('__version__="1.2.3" # this is my version')
    assert result == "1.2.3", result
    def jiggle_setup_py(self) -> int:
        """
        Edit a version = "1.2.3" or version="1.2.3",
        :return:
        """
        changed = 0
        setup_py = os.path.join(self.SRC, "setup.py")
        if not os.path.isfile(setup_py):
            return changed
        lines_to_write = []
        need_rewrite = False

        with open(setup_py, "rb") as file_handle:
            encoding = chardet.detect(file_handle.read())
        # logger.warning("guessing encoding " + str(encoding))
        with self.file_opener.open_this(setup_py, "r") as infile:
            for line in infile:
                leading_white = self.leading_whitespace(line)

                simplified_line = dunder_version.simplify_line(line,
                                                               keep_comma=True)

                # determine if we have a version=
                version = kwarg_version.find_in_line(line)
                # setup() function args
                if version:
                    if simplified_line.strip(" \t\n").startswith(","):
                        start_comma = ","
                    else:
                        start_comma = ""

                    comma = ""
                    if simplified_line.strip(" \t\n").endswith(","):
                        comma = ","
                    # could happen, say on last.
                    # if not comma and not start_comma:
                    #     print(simplified_line)
                    #     raise TypeError("$$$$$")
                    source = f'{leading_white}{start_comma}version = "{str(self.version_to_write())}"{comma}{self.signature_txt}\n'
                    need_rewrite = True
                    lines_to_write.append(source)
                    continue

                # code that isn't the setup() function args
                version, version_token = dunder_version.find_in_line(line)
                if version:
                    if simplified_line.strip(" \t\n").startswith(","):
                        start_comma = ","
                    else:
                        start_comma = ""

                    # the other simplify removes ","
                    if simplified_line.strip(" \t\n").endswith(","):
                        comma = ","
                    else:
                        comma = ""

                    lines_to_write.append(
                        f"{leading_white}{start_comma}{version_token} ="
                        f' "{str(self.version_to_write())}"{comma}{self.signature_txt}\n'
                    )
                    need_rewrite = True
                    continue

                lines_to_write.append(line)

        if need_rewrite:
            with self.file_opener.open_this(setup_py, "r") as file_handle_r:
                source_string = str(file_handle_r.read())
                check(source_string, "".join(lines_to_write))
            with open(setup_py, "w", encoding=encoding["encoding"]) as outfile:
                outfile.writelines(lines_to_write)
                outfile.close()
                changed += 1
        return changed