[READ-ONLY] Mirror of https://github.com/mrgnw/scripts.
1#!/usr/bin/env python3
2
3# uses script git-latest-tag
4
5import urllib.request
6import ssl
7
8
9import xml.etree.ElementTree as ET
10import sys
11from pathlib import Path
12
13
14jetbrains_path = Path.home() / 'Library/Application Support/JetBrains/'
15
16
17def parse_args():
18 """
19 possible args:
20 - duckdb-jetbrains # no args: run the shell script `git-latest-tag duckb/duckdb` to get the latest version
21 - duckdb-jetbrains {version} # 1 arg: it's the version e.g. 0.8.1
22 - duckdb-jetbrains {path_to_xml} {version}
23
24 If it's just one arg, path_to_xml is "$HOME/Library/Application Support/JetBrains/GoLand2023.1/jdbc-drivers/jdbc-drivers.xml"
25 use pathlib to get the home directory
26 """
27
28 jetbrains_path = Path.home() / 'Library/Application Support/JetBrains/'
29 # TODO: lazy approach, can be improved
30 xml_path = [x for x in jetbrains_path.glob('**/jdbc-drivers.xml')][0]
31 print(f'{xml_path}\n')
32
33 if len(sys.argv) == 1:
34 # Run the shell script `git-latest-tag duckb/duckdb` to get the latest version
35 from subprocess import run, PIPE
36
37 version = run(['git-latest-tag', 'duckdb/duckdb'], stdout=PIPE, encoding='utf-8').stdout.strip()
38 if len(sys.argv) == 2:
39 version = sys.argv[1]
40 if len(sys.argv) == 3:
41 xml_path = sys.argv[1]
42 version = sys.argv[2]
43
44 return xml_path, version
45
46
47def add_version_to_xml(xml_path, new_version):
48 # Load the XML tree and get the root
49 tree = ET.parse(xml_path)
50 root = tree.getroot()
51
52 # Find the DuckDB artifact
53 duckdb = None
54 for artifact in root.findall('artifact'):
55 if artifact.get('name') == 'DuckDB':
56 duckdb = artifact
57 break
58
59 if not duckdb:
60 print('DuckDB section not found (no artifact for DuckDB).')
61 return False
62 # don't insert if version is already there
63 for version in duckdb.findall('version'):
64 if version.get('version') == new_version:
65 print(f'Version {new_version} already exists in xml')
66 return False
67
68 # Create the version XML as string and parse it
69 version_xml = f"""
70 <version version="{new_version}">
71 <item type="maven" url="org.duckdb:duckdb_jdbc:{new_version}"/>
72 <item type="license" url="https://download.jetbrains.com/idea/jdbc-drivers/DuckDB/LICENSE.txt"/>
73 </version>
74 """
75 version_element = ET.fromstring(version_xml.strip())
76 duckdb.append(version_element)
77 if duckdb[-1].tail is None:
78 duckdb[-1].tail = '\n'
79 else:
80 duckdb[-1].tail = duckdb[-1].tail.rstrip() + '\n'
81 tree.write(xml_path)
82 return True
83
84
85def download_jar(version, ide='GoLand'):
86 # curl -O https://repo.maven.apache.org/maven2/org/duckdb/duckdb_jdbc/0.9.1/duckdb_jdbc-0.9.1.jar
87 jar_tree = f'org/duckdb/duckdb_jdbc/{version}'
88 file_name = f'duckdb_jdbc-{version}.jar'
89 url = f'https://repo.maven.apache.org/maven2/{jar_tree}/{file_name}'
90 print(url)
91
92 # get latest version of ide withing the jetbrains path
93 ide_path = sorted([x for x in jetbrains_path.glob(f'**/{ide}*')], reverse=True)[0]
94
95 # download jar and save it to the same directory as the xml file
96 jar_dir = Path(ide_path / 'jdbc-drivers/Duckdb' / version / jar_tree)
97 jar_dir.mkdir(parents=True, exist_ok=True)
98
99 jar_filepath = jar_dir / file_name
100 with open(jar_filepath, 'wb') as f:
101 ssl_context = ssl.create_default_context()
102 ssl_context.check_hostname = False
103 ssl_context.verify_mode = ssl.CERT_NONE
104 print('⬇️ Downloading…')
105 with urllib.request.urlopen(url, context=ssl_context) as response, open(
106 jar_filepath, 'wb'
107 ) as out_file:
108 data = response.read() # Read the entire content of the file
109 out_file.write(data)
110
111 # print the path to the jar file and its filesize in MB
112 print(f'✅ {jar_filepath.stat().st_size / 1024**2:.2f} MB\t{jar_filepath}\n')
113
114
115if __name__ == '__main__':
116 xml_path, version = parse_args()
117
118 if add_version_to_xml(xml_path, version) is True:
119 print(f'+ {version}')
120
121 download_jar(version)