Ejemplo n.º 1
0
    def __call__(self, filename, version_label, separator):

        v = Version(lhs=version_label, separator=separator)
        if self._overwrite or not os.path.isfile(filename):
            f, v = v.write(filename)

        elif os.path.isfile(filename):
            answer = input(f"Overwrite file '{filename}' (Y/N [N]: ")
            if len(answer) > 0 and answer.strip().lower() == 'y':
                f, v = v.write(filename)
            else:
                f = filename
                v = None

        return f, v
Ejemplo n.º 2
0
 def __call__(self, filename, label, separator, bump_field):
     if not os.path.isfile(filename):
         print(
             f'No such file: \'{filename}\' cannot bump {bump_field} version'
         )
         return filename, None
     v = Version.find(filename, label, separator)
     if v:
         print(f"Bumping '{bump_field}' value from {v.field(bump_field)} ",
               end="")
         v.bump(bump_field)
         print(f"to {v.field(bump_field)} in '{filename}'")
         Version.update(filename, v, label, separator)
         print(f"new version: {v}")
     else:
         print(f"Couldn't bump value in {filename}")
     return filename, v
Ejemplo n.º 3
0
    def test_find(self):
        # looking for VERSION = '0.0.1-alpha' in test_data

        try:
            with open("test_data", "w") as file:
                file.write("""
# Package meta-data.
NAME = 'semvermanager'
DESCRIPTION = "semvermamager implements a restricted subset of the SEMVER standard"

EMAIL = '*****@*****.**'
AUTHOR = 'Joe Drumgoole'
REQUIRES_PYTHON = '>=3.6.0'
VERSION = '0.0.1-alpha'
URL = 'https://github.com/jdrumgoole/semvermanager'
            """)
            v = Version.find("test_data")
            self.assertEqual(v, Version(0, 0, 1, "alpha"))
        finally:
            os.unlink("test_data")
Ejemplo n.º 4
0
    def test_file_update(self):

        v = Version(0, 4, 2, "")
        temp_filename = temp.tempfile()

        try:
            v.write(temp_filename)
            v2 = Version(0, 4, 3, "")
            Version.update(temp_filename, v2)
            v3 = Version().read(temp_filename)
            self.assertEqual(v2, v3)
            self.assertNotEqual(v, v3)
        finally:
            if os.path.isfile(temp_filename):
                os.unlink(temp_filename)
Ejemplo n.º 5
0
    def test_version(self):
        v = Version(0, 1, 0, "beta")
        self.assertEqual(v.major, 0)
        self.assertEqual(v.minor, 1)
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.tag, "beta")

        v = Version(1, 0, 0, "beta")
        self.assertEqual(v.major, 1)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.tag, "beta")

        v = Version(0, 0, 1, "beta")
        self.assertEqual(v.major, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.patch, 1)
        self.assertEqual(v.tag, "beta")

        v = Version(0, 0, 1, "alpha")
        self.assertEqual(v.major, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.patch, 1)
        self.assertEqual(v.tag, "alpha")

        v = Version(0, 0, 1, "beta")
        self.assertEqual(v.major, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.patch, 1)
        self.assertEqual(v.tag, "beta")

        v = Version(0, 0, 1, "")
        self.assertEqual(v.major, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.patch, 1)
        self.assertEqual(v.tag, "")

        self.assertRaises(VersionError, Version, 0, 0, 1, "test")
Ejemplo n.º 6
0
def main(args=None):
    if not args:
        args = sys.argv

    parser = argparse.ArgumentParser()

    parser.add_argument(
        "--version",
        help="Specify a version in the form major.minor.patch-tag")

    parser.add_argument("--make",
                        default=False,
                        action="store_true",
                        help="Make a new version file")

    parser.add_argument("--bump",
                        choices=Version.FIELDS,
                        help="Bump a version field")

    parser.add_argument(
        "--getversion",
        default=False,
        action="store_true",
        help="Report the current version in the specified file")

    parser.add_argument("--bareversion",
                        default=False,
                        action="store_true",
                        help="Return the unquoted version strin with VERSION=")

    parser.add_argument(
        "--overwrite",
        default=False,
        action="store_true",
        help="overwrite files without checking [default: %(default)s]")

    parser.add_argument("--update",
                        default=False,
                        action="store_true",
                        help="Update multiple version strings in file")

    parser.add_argument(
        "--label",
        default="VERSION",
        help=
        "field used to determine which line is the version line [default: %(default)s]"
    )

    parser.add_argument(
        "--separator",
        default="=",
        help=
        "Character used to separate the version label from the version [default: %(default)s]"
    )

    parser.add_argument("filenames",
                        nargs='*',
                        help="Files to use as version file")

    args = parser.parse_args(args)

    if args.version:
        version = Version.parse_version("VERSION=" + args.version,
                                        lhs=args.label)

    if args.make:
        cmd_runner = OperationRunner(MakeCommand(args.overwrite))
        if not args.filenames:
            args.filenames = ["VERSION"]  # make a default file
        for f, v in cmd_runner(args.filenames, args.label, args.separator):
            if v:
                print(f"Created version {v} in '{f}'")
            else:
                print(f"Failed to create version file '{f}'")

    if args.getversion:
        if os.path.isfile(args.filename):
            v = Version.find(args.filename)
            print(v)
        else:
            print(f"No such version file: '{args.filename}'")

    if args.bareversion:
        if os.path.isfile(args.filename):
            v = Version.find(args.filename, args.label)
            print(v.bare_version())
        else:
            print(f"No such version file: '{args.filename}'")

    if args.bump:
        if args.bump in Version.FIELDS:
            cmd_runner = OperationRunner(BumpCommand())

            for filename, v in cmd_runner(args.filenames, args.label,
                                          args.separator, args.bump):
                if v:
                    print(f"Processed version {v} in file : '{filename}'")
                else:
                    print(f"Couldn't process '{filename}'")

            # if not os.path.isfile(args.filename):
            #     print(f"No such file:'{args.filename}' can't bump {args.bump} version")
            #     sys.exit(1)
            # v = Version.find(args.filename, args.versionlabel)
            # print(f"Bumping '{args.bump}' value from {v.field(args.bump)} ", end="")
            # v.bump(args.bump)
            # print(f"to {v.field(args.bump)} in '{args.filename}'")
            # Version.update(args.filename, v, args.versionlabel)
            # print(f"new version: {v}")
        else:
            print(
                f"{args.bump} is not a valid version field, choose one of {Version.FIELDS}"
            )
            sys.exit(1)

    if args.update:
        print(f"Updating '{args.filename}' with version '{version}'")
        Version.update(filename=args.filename, version=version, lhs=args.label)
Ejemplo n.º 7
0
    def test_bump(self):

        tags = Version.TAGS
        v = Version(0, 0, 1, "alpha")
        v.bump_tag()
        self.assertEqual(v.tag, tags[1])
        v.bump_tag()
        self.assertEqual(v.tag, tags[2])
        v.bump_tag()
        self.assertEqual(v.tag, tags[0])

        v = Version(0, 0, 1, "alpha")
        self.assertEqual(v.tag, "alpha")
        self.assertEqual(v.tag_version, 0)
        v.bump_tag_version()
        self.assertEqual(v.tag, "alpha")
        self.assertEqual(v.tag_version, 1)
        self.assertEqual(v, Version(0, 0, 1, "alpha", 1))

        v.bump_patch()
        self.assertEqual(v.patch, 2)
        v.bump_patch()
        self.assertEqual(v.patch, 3)

        v.bump_minor()
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.minor, 1)
        self.assertEqual(v.major, 0)

        v.bump_minor()
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.minor, 2)
        self.assertEqual(v.major, 0)

        v.bump_major()
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.major, 1)

        v.bump_major()
        self.assertEqual(v.patch, 0)
        self.assertEqual(v.minor, 0)
        self.assertEqual(v.major, 2)
Ejemplo n.º 8
0
 def test_init(self):
     v = Version()
     self.assertEqual(v.major, 0)
     self.assertEqual(v.minor, 0)
     self.assertEqual(v.patch, 0)
     self.assertEqual(v.tag, "alpha")
Ejemplo n.º 9
0
    def test_parse_version(self):

        v = Version.parse_version("0.0.0-alpha")
        self.assertEqual(v, Version(0, 0, 0, "alpha"))

        v = Version.parse_version("VERSION=0.0.0-alpha")
        self.assertEqual(v, Version(0, 0, 0, "alpha"))

        v = Version.parse_version("  VERSION  =  0.0.0-alpha  ")
        self.assertEqual(v, Version(0, 0, 0, "alpha"))

        v = Version.parse_version("  0.0.0-alpha  ")
        self.assertEqual(v, Version(0, 0, 0, "alpha"))

        # VERSION = '0.0.1-alpha'

        v = Version.parse_version("VERSION = '0.0.1-alpha'")
        self.assertEqual(v, Version(0, 0, 1, "alpha"))

        v = Version.parse_version("version = '0.0.1-alpha'", lhs="version")
        self.assertEqual(v, Version(0, 0, 1, "alpha"))

        v = Version.parse_version("version : '0.0.1-alpha'",
                                  lhs="version",
                                  separator=":")
        self.assertEqual(
            v,
            Version(0,
                    0,
                    1,
                    "alpha",
                    tag_version=0,
                    lhs="version",
                    separator=":"))

        v = Version.parse_version("version : '0.4.2'",
                                  lhs="version",
                                  separator=":")
        self.assertEqual(
            v, Version(0,
                       4,
                       2,
                       "",
                       tag_version=0,
                       lhs="version",
                       separator=":"))
Ejemplo n.º 10
0
    def test_file_write_sep(self):

        temp_filename = temp.tempfile()
        try:
            v = Version(0, 4, 1, "beta", separator=":")
            v.write(temp_filename)
            x = Version().read(temp_filename, separator=":")
            self.assertEqual(x, v)
            x.bump_tag()
            x.write(temp_filename)
            w = Version().read(temp_filename, separator=":")
            self.assertEqual(x, w)

            v = Version(0, 3, 1, 'beta',
                        separator="->")  # test single quotes for tag
            v.write(temp_filename)
            x = Version().read(temp_filename, separator="->")
            self.assertEqual(x, v)

        finally:
            if os.path.isfile(temp_filename):
                os.unlink(temp_filename)