Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit b23ad39

Browse files
committed
service: dev: Add version bump command
Signed-off-by: John Andersen <[email protected]>
1 parent 81b7aa1 commit b23ad39

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

dffml/service/dev.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import sys
3+
import ast
34
import json
45
import pydoc
56
import asyncio
7+
import pathlib
68
import getpass
79
import importlib
810
import contextlib
@@ -434,6 +436,127 @@ async def run(self):
434436
raise RuntimeError
435437

436438

439+
class BumpMain(CMD):
440+
"""
441+
Bump the version number of the main package within the dependency list of
442+
each plugin.
443+
"""
444+
445+
async def run(self):
446+
main_package = is_develop("dffml")
447+
if not main_package:
448+
raise NotImplementedError(
449+
"Need to reinstall the main package in development mode."
450+
)
451+
# TODO Implement this in Python
452+
proc = await asyncio.create_subprocess_exec(
453+
"bash", str(pathlib.Path(main_package, "scripts", "bump_deps.sh"))
454+
)
455+
await proc.wait()
456+
if proc.returncode != 0:
457+
raise RuntimeError
458+
459+
460+
class BumpPackages(CMD):
461+
"""
462+
Bump all the versions of all the packages and increment the version number
463+
given.
464+
"""
465+
466+
arg_skip = Arg(
467+
"-skip",
468+
help="Do not increment versions in these packages",
469+
nargs="+",
470+
default=[],
471+
required=False,
472+
)
473+
arg_only = Arg(
474+
"-only",
475+
help="Only increment versions in these packages",
476+
nargs="+",
477+
default=[],
478+
required=False,
479+
)
480+
arg_version = Arg("version", help="Version to increment by")
481+
482+
@staticmethod
483+
def bump_version(original, increment):
484+
# Split on .
485+
# map: int: Convert to an int
486+
# zip: Create three instances of (original[i], increment[i])
487+
# map: sum: Add each pair together
488+
# map: str: Convert back to strings
489+
return ".".join(
490+
map(
491+
str,
492+
map(
493+
sum,
494+
zip(
495+
map(int, original.split(".")),
496+
map(int, increment.split(".")),
497+
),
498+
),
499+
)
500+
)
501+
502+
async def run(self):
503+
main_package = is_develop("dffml")
504+
if not main_package:
505+
raise NotImplementedError(
506+
"Need to reinstall the main package in development mode."
507+
)
508+
main_package = pathlib.Path(main_package)
509+
skel = main_package / "dffml" / "skel"
510+
# Update all the version files
511+
for version_file in main_package.rglob("**/version.py"):
512+
# Ignore skel
513+
if skel in version_file.parents:
514+
self.logger.debug(
515+
"Skipping skel verison file %s", version_file
516+
)
517+
continue
518+
# If we're only supposed to increment versions of some packages,
519+
# check we're in the right package, skip if not.
520+
setup_filepath = version_file.parent.parent / "setup.py"
521+
with chdir(setup_filepath.parent):
522+
name = SetupPyKWArg.get_kwargs(setup_filepath)["name"]
523+
if self.only and name not in self.only:
524+
self.logger.debug(
525+
"Verison file not in only %s", version_file
526+
)
527+
continue
528+
elif name in self.skip:
529+
self.logger.debug("Skipping verison file %s", version_file)
530+
continue
531+
# Read the file
532+
filetext = version_file.read_text()
533+
# Find the version as a string
534+
modified_lines = []
535+
for line in filetext.split("\n"):
536+
# Look for the line containing the version string
537+
if line.startswith("VERSION"):
538+
# Parse the version string
539+
version = ast.literal_eval(line.split("=")[-1].strip())
540+
# Increment the version string
541+
version = self.bump_version(version, self.version)
542+
# Modify the line to use the new version string
543+
line = f'VERSION = "{version}"'
544+
# Append line to list of lines to write back
545+
modified_lines.append(line)
546+
# Write back the file using the modified lines
547+
filetext = version_file.write_text("\n".join(modified_lines))
548+
self.logger.debug("Updated verison file %s", version_file)
549+
550+
551+
class Bump(CMD):
552+
"""
553+
Bump the the main package in the versions plugins, or any or all libraries.
554+
"""
555+
556+
main = BumpMain
557+
packages = BumpPackages
558+
559+
437560
class Develop(CMD):
438561
"""
439562
Development utilities for hacking on DFFML itself
@@ -447,3 +570,4 @@ class Develop(CMD):
447570
install = Install
448571
release = Release
449572
setuppy = SetupPy
573+
bump = Bump

docs/cli.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,27 @@ argument.
201201
202202
$ dffml service dev setuppy kwarg name model/tensorflow/setup.py
203203
dffml-model-tensorflow
204+
205+
bump
206+
++++
207+
208+
Utilities for bumping version numbers.
209+
210+
main
211+
____
212+
213+
Update the version of DFFML used by all of the plugins.
214+
215+
.. code-block:: console
216+
217+
dffml service dev bump main
218+
219+
packages
220+
________
221+
222+
Update the version number of a package or all packages. Increments the version
223+
of each packages by the version string given.
224+
225+
.. code-block:: console
226+
227+
dffml service dev bump packages -log debug -skip dffml -- 0.0.1

tests/service/test_dev.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
from dffml.version import VERSION
1515
from dffml.df.types import DataFlow
16-
from dffml.service.dev import Develop, RepoDirtyError, Export, Run
16+
from dffml.service.dev import (
17+
Develop,
18+
RepoDirtyError,
19+
Export,
20+
Run,
21+
BumpPackages,
22+
)
1723
from dffml.util.os import chdir
1824
from dffml.util.skel import Skel
1925
from dffml.util.packaging import is_develop
@@ -229,6 +235,11 @@ async def test_okay(self):
229235
)
230236

231237

238+
class TestBumpPackages(AsyncTestCase):
239+
async def test_bump_version(self):
240+
self.assertEqual(BumpPackages.bump_version("1.2.3", "5.6.7"), "6.8.10")
241+
242+
232243
class TestExport(AsyncTestCase):
233244
async def test_run(self):
234245
stdout = io.BytesIO()

0 commit comments

Comments
 (0)