Skip to content

Commit e97fd25

Browse files
authored
chore: Use Python assignement expressions (#292)
1 parent 1a214a8 commit e97fd25

File tree

17 files changed

+35
-71
lines changed

17 files changed

+35
-71
lines changed

pylib/gyp/MSVSSettings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ def _ValidateExclusionSetting(setting, settings, error_msg, stderr=sys.stderr):
396396
# This may be unrecognized because it's an exclusion list. If the
397397
# setting name has the _excluded suffix, then check the root name.
398398
unrecognized = True
399-
m = re.match(_EXCLUDED_SUFFIX_RE, setting)
400-
if m:
399+
if m := re.match(_EXCLUDED_SUFFIX_RE, setting):
401400
root_setting = m.group(1)
402401
unrecognized = root_setting not in settings
403402

pylib/gyp/MSVSVersion.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ def SelectVisualStudioVersion(version="auto", allow_fallback=True):
552552
"2019": ("16.0",),
553553
"2022": ("17.0",),
554554
}
555-
override_path = os.environ.get("GYP_MSVS_OVERRIDE_PATH")
556-
if override_path:
555+
if override_path := os.environ.get("GYP_MSVS_OVERRIDE_PATH"):
557556
msvs_version = os.environ.get("GYP_MSVS_VERSION")
558557
if not msvs_version:
559558
raise ValueError(

pylib/gyp/generator/android.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,7 @@ def WriteTarget(
900900
if self.type != "none":
901901
self.WriteTargetFlags(spec, configs, link_deps)
902902

903-
settings = spec.get("aosp_build_settings", {})
904-
if settings:
903+
if settings := spec.get("aosp_build_settings", {}):
905904
self.WriteLn("### Set directly by aosp_build_settings.")
906905
for k, v in settings.items():
907906
if isinstance(v, list):

pylib/gyp/generator/cmake.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,8 +810,7 @@ def WriteTarget(
810810
# link directories to targets defined after it is called.
811811
# As a result, link_directories must come before the target definition.
812812
# CMake unfortunately has no means of removing entries from LINK_DIRECTORIES.
813-
library_dirs = config.get("library_dirs")
814-
if library_dirs is not None:
813+
if (library_dirs := config.get("library_dirs")) is not None:
815814
output.write("link_directories(")
816815
for library_dir in library_dirs:
817816
output.write(" ")
@@ -1295,8 +1294,7 @@ def CallGenerateOutputForConfig(arglist):
12951294

12961295

12971296
def GenerateOutput(target_list, target_dicts, data, params):
1298-
user_config = params.get("generator_flags", {}).get("config", None)
1299-
if user_config:
1297+
if user_config := params.get("generator_flags", {}).get("config", None):
13001298
GenerateOutputForConfig(target_list, target_dicts, data, params, user_config)
13011299
else:
13021300
config_names = target_dicts[target_list[0]]["configurations"]

pylib/gyp/generator/eclipse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ def GenerateOutput(target_list, target_dicts, data, params):
451451
if params["options"].generator_output:
452452
raise NotImplementedError("--generator_output not implemented for eclipse")
453453

454-
user_config = params.get("generator_flags", {}).get("config", None)
455-
if user_config:
454+
if user_config := params.get("generator_flags", {}).get("config", None):
456455
GenerateOutputForConfig(target_list, target_dicts, data, params, user_config)
457456
else:
458457
config_names = target_dicts[target_list[0]]["configurations"]

pylib/gyp/generator/make.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,8 +1465,7 @@ def WriteSources(
14651465
order_only=True,
14661466
)
14671467

1468-
pchdeps = precompiled_header.GetObjDependencies(compilable, objs)
1469-
if pchdeps:
1468+
if pchdeps := precompiled_header.GetObjDependencies(compilable, objs):
14701469
self.WriteLn("# Dependencies from obj files to their precompiled headers")
14711470
for source, obj, gch in pchdeps:
14721471
self.WriteLn(f"{obj}: {gch}")
@@ -1600,8 +1599,7 @@ def ComputeOutputBasename(self, spec):
16001599

16011600
target_prefix = spec.get("product_prefix", target_prefix)
16021601
target = spec.get("product_name", target)
1603-
product_ext = spec.get("product_extension")
1604-
if product_ext:
1602+
if product_ext := spec.get("product_extension"):
16051603
target_ext = "." + product_ext
16061604

16071605
return target_prefix + target + target_ext

pylib/gyp/generator/msvs.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,7 @@ def _GetOutputTargetExt(spec):
13641364
Returns:
13651365
A string with the extension, or None
13661366
"""
1367-
target_extension = spec.get("product_extension")
1368-
if target_extension:
1367+
if target_extension := spec.get("product_extension"):
13691368
return "." + target_extension
13701369
return None
13711370

@@ -3166,8 +3165,7 @@ def _GetMSBuildAttributes(spec, config, build_file):
31663165
"windows_driver": "Link",
31673166
"static_library": "Lib",
31683167
}
3169-
msbuild_tool = msbuild_tool_map.get(spec["type"])
3170-
if msbuild_tool:
3168+
if msbuild_tool := msbuild_tool_map.get(spec["type"]):
31713169
msbuild_settings = config["finalized_msbuild_settings"]
31723170
out_file = msbuild_settings[msbuild_tool].get("OutputFile")
31733171
if out_file:
@@ -3184,8 +3182,7 @@ def _GetMSBuildConfigurationGlobalProperties(spec, configurations, build_file):
31843182
# there are actions.
31853183
# TODO(jeanluc) Handle the equivalent of setting 'CYGWIN=nontsec'.
31863184
new_paths = []
3187-
cygwin_dirs = spec.get("msvs_cygwin_dirs", ["."])[0]
3188-
if cygwin_dirs:
3185+
if cygwin_dirs := spec.get("msvs_cygwin_dirs", ["."])[0]:
31893186
cyg_path = "$(MSBuildProjectDirectory)\\%s\\bin\\" % _FixPath(cygwin_dirs)
31903187
new_paths.append(cyg_path)
31913188
# TODO(jeanluc) Change the convention to have both a cygwin_dir and a
@@ -3370,7 +3367,6 @@ def _FinalizeMSBuildSettings(spec, configuration):
33703367
prebuild = configuration.get("msvs_prebuild")
33713368
postbuild = configuration.get("msvs_postbuild")
33723369
def_file = _GetModuleDefinition(spec)
3373-
precompiled_header = configuration.get("msvs_precompiled_header")
33743370

33753371
# Add the information to the appropriate tool
33763372
# TODO(jeanluc) We could optimize and generate these settings only if
@@ -3408,7 +3404,7 @@ def _FinalizeMSBuildSettings(spec, configuration):
34083404
msbuild_settings, "ClCompile", "DisableSpecificWarnings", disabled_warnings
34093405
)
34103406
# Turn on precompiled headers if appropriate.
3411-
if precompiled_header:
3407+
if precompiled_header := configuration.get("msvs_precompiled_header"):
34123408
# While MSVC works with just file name eg. "v8_pch.h", ClangCL requires
34133409
# the full path eg. "tools/msvs/pch/v8_pch.h" to find the file.
34143410
# P.S. Only ClangCL defines msbuild_toolset, for MSVC it is None.

pylib/gyp/generator/ninja.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,15 @@ def ExpandSpecial(self, path, product_dir=None):
263263
dir.
264264
"""
265265

266-
PRODUCT_DIR = "$!PRODUCT_DIR"
267-
if PRODUCT_DIR in path:
266+
if (PRODUCT_DIR := "$!PRODUCT_DIR") in path:
268267
if product_dir:
269268
path = path.replace(PRODUCT_DIR, product_dir)
270269
else:
271270
path = path.replace(PRODUCT_DIR + "/", "")
272271
path = path.replace(PRODUCT_DIR + "\\", "")
273272
path = path.replace(PRODUCT_DIR, ".")
274273

275-
INTERMEDIATE_DIR = "$!INTERMEDIATE_DIR"
276-
if INTERMEDIATE_DIR in path:
274+
if (INTERMEDIATE_DIR := "$!INTERMEDIATE_DIR") in path:
277275
int_dir = self.GypPathToUniqueOutput("gen")
278276
# GypPathToUniqueOutput generates a path relative to the product dir,
279277
# so insert product_dir in front if it is provided.
@@ -2075,16 +2073,14 @@ def OpenOutput(path, mode="w"):
20752073

20762074

20772075
def CommandWithWrapper(cmd, wrappers, prog):
2078-
wrapper = wrappers.get(cmd, "")
2079-
if wrapper:
2076+
if wrapper := wrappers.get(cmd, ""):
20802077
return wrapper + " " + prog
20812078
return prog
20822079

20832080

20842081
def GetDefaultConcurrentLinks():
20852082
"""Returns a best-guess for a number of concurrent links."""
2086-
pool_size = int(os.environ.get("GYP_LINK_CONCURRENCY") or 0)
2087-
if pool_size:
2083+
if pool_size := int(os.environ.get("GYP_LINK_CONCURRENCY") or 0):
20882084
return pool_size
20892085

20902086
if sys.platform in ("win32", "cygwin"):
@@ -2305,8 +2301,7 @@ def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name
23052301
key_prefix = re.sub(r"\.HOST$", ".host", key_prefix)
23062302
wrappers[key_prefix] = os.path.join(build_to_root, value)
23072303

2308-
mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None)
2309-
if mac_toolchain_dir:
2304+
if mac_toolchain_dir := generator_flags.get("mac_toolchain_dir", None):
23102305
wrappers["LINK"] = "export DEVELOPER_DIR='%s' &&" % mac_toolchain_dir
23112306

23122307
if flavor == "win":

pylib/gyp/mac_tool.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
def main(args):
2727
executor = MacTool()
28-
exit_code = executor.Dispatch(args)
29-
if exit_code is not None:
28+
if (exit_code := executor.Dispatch(args)) is not None:
3029
sys.exit(exit_code)
3130

3231

pylib/gyp/msvs_emulation.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ def GetExtension(self):
247247
the target type.
248248
"""
249249
ext = self.spec.get("product_extension", None)
250-
if ext:
251-
return ext
252-
return gyp.MSVSUtil.TARGET_TYPE_EXT.get(self.spec["type"], "")
250+
return ext or gyp.MSVSUtil.TARGET_TYPE_EXT.get(self.spec["type"], "")
253251

254252
def GetVSMacroEnv(self, base_to_build=None, config=None):
255253
"""Get a dict of variables mapping internal VS macro names to their gyp
@@ -625,8 +623,7 @@ def GetDefFile(self, gyp_to_build_path):
625623
def _GetDefFileAsLdflags(self, ldflags, gyp_to_build_path):
626624
""".def files get implicitly converted to a ModuleDefinitionFile for the
627625
linker in the VS generator. Emulate that behaviour here."""
628-
def_file = self.GetDefFile(gyp_to_build_path)
629-
if def_file:
626+
if def_file := self.GetDefFile(gyp_to_build_path):
630627
ldflags.append('/DEF:"%s"' % def_file)
631628

632629
def GetPGDName(self, config, expand_special):
@@ -674,14 +671,11 @@ def GetLdflags(
674671
)
675672
ld("DelayLoadDLLs", prefix="/DELAYLOAD:")
676673
ld("TreatLinkerWarningAsErrors", prefix="/WX", map={"true": "", "false": ":NO"})
677-
out = self.GetOutputName(config, expand_special)
678-
if out:
674+
if out := self.GetOutputName(config, expand_special):
679675
ldflags.append("/OUT:" + out)
680-
pdb = self.GetPDBName(config, expand_special, output_name + ".pdb")
681-
if pdb:
676+
if pdb := self.GetPDBName(config, expand_special, output_name + ".pdb"):
682677
ldflags.append("/PDB:" + pdb)
683-
pgd = self.GetPGDName(config, expand_special)
684-
if pgd:
678+
if pgd := self.GetPGDName(config, expand_special):
685679
ldflags.append("/PGD:" + pgd)
686680
map_file = self.GetMapFileName(config, expand_special)
687681
ld("GenerateMapFile", map={"true": "/MAP:" + map_file if map_file else "/MAP"})

0 commit comments

Comments
 (0)