1- # -*- coding: utf-8 -*-
1+ # /// script
2+ # dependencies = ["requests"]
3+ # ///
4+
25"""
36Command line executable allowing to update NinjaUrls.cmake, documentation
47and tests given a Ninja version.
58"""
9+ from __future__ import annotations
610
711import argparse
812import contextlib
1216import tempfile
1317import textwrap
1418
15- try :
16- from requests import request
17- except ImportError :
18- raise SystemExit (
19- "requests not available: "
20- "consider installing it running 'pip install requests'"
21- )
19+ from requests import request
2220
2321ROOT_DIR = os .path .join (os .path .dirname (__file__ ), ".." )
2422
@@ -31,7 +29,7 @@ def _log(txt, verbose=True):
3129 print (txt )
3230 yield
3331 if verbose :
34- print ("%s - done" % txt )
32+ print (f" { txt } - done" )
3533
3634
3735def _download_file (download_url , filename ):
@@ -67,7 +65,7 @@ def _hash_sum(filepath, algorithm="sha256", block_size=2 ** 20):
6765
6866def _download_and_compute_sha256 (url , filename ):
6967 filepath = os .path .join (tempfile .gettempdir (), filename )
70- with _log ("Downloading %s" % url ):
68+ with _log (f "Downloading { url } " ):
7169 _download_file (url , filepath )
7270 sha256 = _hash_sum (filepath , algorithm = "sha256" )
7371 return url , sha256
@@ -77,7 +75,7 @@ def get_ninja_archive_urls_and_sha256s(upstream_repository, version, verbose=Fal
7775 tag_name = f"v{ version } "
7876 files_base_url = f"https://github.com/{ upstream_repository } /archive/{ tag_name } "
7977
80- with _log ("Collecting URLs and SHA256s from '%s'" % files_base_url ):
78+ with _log (f "Collecting URLs and SHA256s from '{ files_base_url } '" ):
8179
8280 # Get SHA256s and URLs
8381 urls = {
@@ -87,7 +85,7 @@ def get_ninja_archive_urls_and_sha256s(upstream_repository, version, verbose=Fal
8785
8886 if verbose :
8987 for identifier , (url , sha256 ) in urls .items ():
90- print ("[{}]\n {}\n {}\n " . format ( identifier , url , sha256 ) )
88+ print (f "[{ identifier } ]\n { url } \n { sha256 } \n " )
9189
9290 return urls
9391
@@ -97,8 +95,8 @@ def generate_cmake_variables(urls_and_sha256s):
9795
9896 # Get SHA256s and URLs
9997 for var_prefix , urls_and_sha256s_values in urls_and_sha256s .items ():
100- template_inputs ["%s_url" % var_prefix ] = urls_and_sha256s_values [0 ]
101- template_inputs ["%s_sha256" % var_prefix ] = urls_and_sha256s_values [1 ]
98+ template_inputs [f" { var_prefix } _url" ] = urls_and_sha256s_values [0 ]
99+ template_inputs [f" { var_prefix } _sha256" ] = urls_and_sha256s_values [1 ]
102100
103101 return textwrap .dedent (
104102 """
@@ -118,16 +116,16 @@ def update_cmake_urls_script(upstream_repository, version):
118116 cmake_urls_filename = "NinjaUrls.cmake"
119117 cmake_urls_filepath = os .path .join (ROOT_DIR , cmake_urls_filename )
120118
121- msg = "Updating '{}' with Ninja version {}" . format ( cmake_urls_filename , version )
119+ msg = f "Updating '{ cmake_urls_filename } ' with Ninja version { version } "
122120 with _log (msg ), open (cmake_urls_filepath , "w" ) as cmake_file :
123121 cmake_file .write (content )
124122
125123
126124def _update_file (filepath , regex , replacement , verbose = True ):
127- msg = "Updating %s" % os .path .relpath (filepath , ROOT_DIR )
125+ msg = f "Updating { os .path .relpath (filepath , ROOT_DIR )} "
128126 with _log (msg , verbose = verbose ):
129127 pattern = re .compile (regex )
130- with open (filepath , "r" ) as doc_file :
128+ with open (filepath ) as doc_file :
131129 lines = doc_file .readlines ()
132130 updated_content = []
133131 for line in lines :
@@ -138,7 +136,7 @@ def _update_file(filepath, regex, replacement, verbose=True):
138136
139137def update_docs (upstream_repository , version ):
140138 pattern = re .compile (r"ninja \d+.\d+.\d+(\.[\w\-]+)*" )
141- replacement = "ninja %s" % version
139+ replacement = f "ninja { version } "
142140 _update_file (
143141 os .path .join (ROOT_DIR , "README.rst" ),
144142 pattern , replacement )
@@ -171,7 +169,7 @@ def update_tests(version):
171169 version = "." .join (parts )
172170
173171 pattern = re .compile (r'expected_version = "\d+.\d+.\d+(\.[\w\-]+)*"' )
174- replacement = 'expected_version = "%s"' % version
172+ replacement = f 'expected_version = "{ version } "'
175173 _update_file (os .path .join (
176174 ROOT_DIR , "tests/test_ninja.py" ), pattern , replacement )
177175
0 commit comments