Skip to content

Commit

Permalink
Issue 13413 write script to sync version to spring boot dependencies (#…
Browse files Browse the repository at this point in the history
…13748)

* Get all properties from pom file.

* Save properties in a dict.

* Get dependency vertion from dependencyManagement.

* Implement function: update_version_for_external_dependencies.

* Remove debug log.

* Not add new line in the result file.

* Change 2 file path to global variable.

* Add version parameter in spring_boot_dependencies_file.

* Remove debug log.

* Add comment to explain when should we run this script.

* Fix typo in file name.

Co-authored-by: Rujun Chen <[email protected]>
  • Loading branch information
Rujun Chen and rujche authored Aug 3, 2020
1 parent 75114f7 commit 6bee112
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ org.springframework.boot:spring-boot-starter-test;2.3.0.RELEASE
org.springframework.boot:spring-boot-starter-validation;2.3.0.RELEASE
org.springframework.boot:spring-boot-starter-web;2.3.0.RELEASE
org.springframework.boot:spring-boot-starter;2.3.0.RELEASE
# After you update spring-boot's version, please run this script: /sdk/spring/scripts/sync_version_from_spring-boot-dependencies.py
org.springframework.boot:spring-boot;2.3.0.RELEASE
org.springframework.boot:spring-boot-starter-parent;2.3.0.RELEASE
org.springframework.data:spring-data-commons;2.3.0.RELEASE
Expand Down
90 changes: 90 additions & 0 deletions sdk/spring/scripts/sync_version_from_spring-boot-dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


import fileinput
import os
import requests
import time
import xml.etree.ElementTree as ET


EXTERNAL_DEPENDENCIES_FILE = 'eng/versioning/external_dependencies.txt'
SPRING_BOOT_DEPENDENCIES_FILE = 'https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/{}/spring-boot-dependencies-{}.pom'


def main():
start_time = time.time()
change_to_root_dir()
print('Current working directory = {}.'.format(os.getcwd()))
spring_boot_version = get_spring_boot_version()
print('spring_boot_version = {}.'.format(spring_boot_version))
dependencyDict = get_spring_boot_dependencies(spring_boot_version)
update_version_for_external_dependencies(dependencyDict)

elapsed_time = time.time() - start_time
print('elapsed_time = {}'.format(elapsed_time))


def change_to_root_dir():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir('../../..')


def get_spring_boot_version():
file1 = open(EXTERNAL_DEPENDENCIES_FILE, 'r')
Lines = file1.readlines()
count = 0
for line in Lines:
if line.startswith('org.springframework.boot:spring-boot;'):
return line.split(';', 1)[1].strip()
raise Exception("Can not get spring boot version.")


def get_spring_boot_dependencies(spring_boot_version):
r = requests.get(SPRING_BOOT_DEPENDENCIES_FILE.format(spring_boot_version, spring_boot_version))
projectElement = ET.fromstring(r.text)
nameSpace = {'maven': 'http://maven.apache.org/POM/4.0.0'}
# get properties
properties = projectElement.find('maven:properties', nameSpace)
propertyDict = {}
for property in properties:
key = property.tag.split('}', 1)[1]
value = property.text
propertyDict[key] = value
# get dependencies
dependencyDict = {}
dependencyElements = projectElement.findall('./maven:dependencyManagement/maven:dependencies/maven:dependency', nameSpace)
for dependencyElement in dependencyElements:
groupId = dependencyElement.find("./maven:groupId", nameSpace).text.strip(' ')
artifactId = dependencyElement.find("./maven:artifactId", nameSpace).text.strip(' ')
version = dependencyElement.find("./maven:version", nameSpace).text.strip(' ${}')
key = groupId + ':' + artifactId
value = propertyDict[version]
dependencyDict[key] = value
return dependencyDict


def update_version_for_external_dependencies(dependencyDict):
file_line_count = sum(1 for line in open(EXTERNAL_DEPENDENCIES_FILE))
for line in fileinput.input(EXTERNAL_DEPENDENCIES_FILE, inplace=True):
line = line.strip()
endValue = '' if fileinput.filelineno() == file_line_count else '\n'
if line.startswith('#') or not line:
print(line, end = endValue)
else:
keyValue = line.split(';', 1)
key = keyValue[0]
value = keyValue[1]
if key in dependencyDict:
value = dependencyDict[key]
print('{};{}'.format(key, value), end = endValue)


def print_dict(dict):
for key, value in dict.items():
print('key = {}, value = {}.'.format(key, value))


if __name__ == '__main__':
main()

0 comments on commit 6bee112

Please sign in to comment.