Skip to content

Commit

Permalink
Script to automatically copy and bump package version in the storage … (
Browse files Browse the repository at this point in the history
#4595) (#4799)

Co-authored-by: Juan Álvarez <[email protected]>
  • Loading branch information
axw and jalvz authored Feb 18, 2021
1 parent 7c78cd8 commit 7f7e071
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
7 changes: 3 additions & 4 deletions apmpackage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
5. Upload to the snapshot registry
- When everything works and `apmpackage/apm/` changes have been merged to `master`, copy the new package to
`package-storage/packages` in the `package-storage` repo, `snapshot` branch.
Do *NOT* override any existing packages. Instead, bump the qualifier version. Eg: rename `apm/0.1.0-dev.1` to `apm/0.1.0-dev.2`
- Bump the qualifier in the contents too: `find . -type f -print0 | xargs -0 sed -i "" "s/0.1.0-dev.1/0.1.0-dev.2/g"`
This step can be done in a separate commit to facilitate review.

Do *NOT* override any existing packages. Instead, bump the qualifier version (eg: `0.1.0-dev.1` to `0.1.0-dev.2`)
both in the folder name and the content (`manifest.yml` and `default.json` pipelines)
- You can `cd script && python copy-package.py` for this.

#### Create a new package version

Expand Down
91 changes: 91 additions & 0 deletions script/copy_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from functools import cmp_to_key

import argparse
import os
import shutil
import subprocess
import sys


def semver_sorter(a, b):
a_list = a.split("-")
b_list = b.split("-")
version_cmp = trivial_cmp(a_list[0], b_list[0])
if version_cmp != 0:
return version_cmp
if len(a_list) == 1:
return 1
if len(b_list) == 1:
return -1
return trivial_cmp(a_list[1], b_list[1])


def trivial_cmp(a, b):
if a > b:
return 1
elif b > a:
return -1
return 0


def bump(v):
tokens = v.split(".")
tokens[-1] = str(int(tokens[-1]) + 1)
return ".".join(tokens)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dst', help='directory of the package-storage repo', default="../../package-storage")
parser.add_argument('--final', action='store_true')
parser.add_argument('-v', '--version', help='version of the package to copy, defaults to last one')
parser.add_argument('--dry', action='store_true', help='dont copy data')
args = parser.parse_args()

src = "../apmpackage/apm/"
original_version = args.version
if not args.version:
# default to last version
versions = [os.path.basename(f) for f in os.listdir(src)]
versions.sort()
original_version = versions[-1]

src = os.path.join(src, original_version)
dst = os.path.join(args.dst, "packages/apm/")

# find and sort published versions
published_versions = [os.path.basename(f) for f in os.listdir(dst)]
published_versions.sort(key=cmp_to_key(semver_sorter))
published_versions.reverse()

# resolve the next version
# only patch and dev might be automatically bumped
next_version = original_version
for published in published_versions:
if original_version == published:
raise Exception("Version already published")
if original_version in published:
if not args.final:
# development version released, bump it
# eg. 0.1.0-dev.3 -> 0.1.0-dev.4
next_version = bump(published)
break

if next_version == original_version and not args.final:
# version never released, create the first development version out of it
next_version = next_version + "-dev.1"

dst = os.path.join(dst, next_version)
print("from " + src + " to " + dst)

if args.dry:
sys.exit(0)

# copy over the package and replace version in manifest and pipeline names
shutil.copytree(src, dst)
cmd = 'find {0} -type f -print0 | xargs -0 sed -i "" "s/{1}/{2}/g"'.format(dst, original_version, next_version)

out = subprocess.check_output(cmd, shell=True)
if out:
print(out)
print("Done")

0 comments on commit 7f7e071

Please sign in to comment.