Skip to content

Commit

Permalink
Convert contrib/node and contrib/go to use f-strings. (pantsbuild…
Browse files Browse the repository at this point in the history
…#8699)

Co-Authored-By: Eric Arellano <[email protected]>
  • Loading branch information
2 people authored and jsirois committed Nov 24, 2019
1 parent fbe99a1 commit 1d7e589
Show file tree
Hide file tree
Showing 28 changed files with 70 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fetch_archive(self, archive_url, strip_level, dest):
try:
archiver = archiver_for_path(archive_url)
except ValueError:
raise FetchError("Don't know how to unpack archive at url {}".format(archive_url))
raise FetchError(f"Don't know how to unpack archive at url {archive_url}")

with self._fetch(archive_url) as archive:
if strip_level == 0:
Expand Down Expand Up @@ -68,10 +68,10 @@ def _fetch(self, url):
@contextmanager
def _download(self, url):
# TODO(jsirois): Wrap with workunits, progress meters, checksums.
logger.info('Downloading {}...'.format(url))
logger.info(f'Downloading {url}...')
with closing(self._session().get(url, stream=True)) as res:
if res.status_code != requests.codes.ok:
raise FetchError('Failed to download {} ({} error)'.format(url, res.status_code))
raise FetchError(f'Failed to download {url} ({res.status_code} error)')
with temporary_file() as archive_fp:
# NB: Archives might be very large so we play it safe and buffer them to disk instead of
# memory before unpacking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class FetchError(Exception):

def add_message_prefix(self, prefix):
# Note: Assumes that this object was created with a single string argument.
self.args = ('{}{}'.format(prefix, self.args[0]), )
self.args = (f'{prefix}{self.args[0]}', )
8 changes: 4 additions & 4 deletions contrib/go/src/python/pants/contrib/go/subsystems/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def root(self):
if imported_repo:
return imported_repo.import_prefix
else:
raise FetchError('No <meta name="go-import"> tag found at {}'.format(self.import_path))
raise FetchError(f'No <meta name="go-import"> tag found at {self.import_path}')

def fetch(self, dest, rev=None):
imported_repo = self._meta_tag_reader.get_imported_repo(self.import_path)
Expand All @@ -86,9 +86,9 @@ def fetch(self, dest, rev=None):
'at {}'.format(self.import_path))
if imported_repo.vcs != 'git':
# TODO: Support other vcs systems as needed.
raise FetchError("Don't know how to fetch for vcs type {}.".format(imported_repo.vcs))
raise FetchError(f"Don't know how to fetch for vcs type {imported_repo.vcs}.")
# TODO: Do this in a workunit (see https://github.com/pantsbuild/pants/issues/3502).
logger.info('Cloning {} into {}'.format(imported_repo.url, dest))
logger.info(f'Cloning {imported_repo.url} into {dest}')
repo = Git.clone(imported_repo.url, dest)
if rev:
repo.set_state(rev)
Expand Down Expand Up @@ -124,7 +124,7 @@ def fetch(self, dest, rev=None):
self._fetch(archive_url, self._url_info.strip_level, dest)
except FetchError as e:
# Modify the message to add more information, then reraise with the original traceback.
e.add_message_prefix('Error while fetching import {}: '.format(self.import_path))
e.add_message_prefix(f'Error while fetching import {self.import_path}: ')
raise

def _fetch(self, archive_url, strip_level, dest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_imported_repo(self, import_path):
# See https://github.com/pantsbuild/pants/issues/3503.
session.mount("http://",
requests.adapters.HTTPAdapter(max_retries=self.get_options().retries))
page_data = session.get('http://{import_path}?go-get=1'.format(import_path=import_path))
page_data = session.get(f'http://{import_path}?go-get=1')
except requests.ConnectionError:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def select(self, context):
)

if result != 0:
raise SubsystemError('{} failed with exit code {}'.format(go_cmd, result))
raise SubsystemError(f'{go_cmd} failed with exit code {result}')

logger.info('Selected {} binary bootstrapped to: {}'.format(self.options_scope, tool_path))
logger.info(f'Selected {self.options_scope} binary bootstrapped to: {tool_path}')
return tool_path
4 changes: 2 additions & 2 deletions contrib/go/src/python/pants/contrib/go/targets/go_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def normalize_package_path(cls, package_path):
:raises: `ValueError` if the package path cannot be normalized.
"""
if package_path.startswith(os.pardir + os.sep):
raise ValueError('Relative package paths are not allowed. Given: {!r}'.format(package_path))
raise ValueError(f'Relative package paths are not allowed. Given: {package_path!r}')
if os.path.isabs(package_path):
raise ValueError('Absolute package paths are not allowed. Given: {!r}'.format(package_path))
raise ValueError(f'Absolute package paths are not allowed. Given: {package_path!r}')
return '' if not package_path or package_path == os.curdir else package_path.lstrip('/')

@property
Expand Down
9 changes: 4 additions & 5 deletions contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _generate_missing(self, gopath, local_address, import_listing, visited):
except AddressLookupError:
if not self._generate_remotes:
raise self.NewRemoteEncounteredButRemotesNotAllowedError(
'Cannot generate dependency for remote import path {}'.format(import_path))
f'Cannot generate dependency for remote import path {import_path}')
self._build_graph.inject_synthetic_target(address=address,
target_type=GoRemoteLibrary,
pkg=remote_pkg_path)
Expand Down Expand Up @@ -219,7 +219,7 @@ def log(self, logger):
:type logger: A :class:`logging.Logger` compatible object.
"""
log = logger.info if self.local or self.rev else logger.warn
log('\t{}'.format(self))
log(f'\t{self}')

@property
def failed(self):
Expand All @@ -231,9 +231,8 @@ def failed(self):

def __str__(self):
import_paths = ' '.join(sorted(self.import_paths))
rev = '' if self.local else ' {}'.format(self.rev or 'FLOATING')
return ('{build_file_path} ({import_paths}){rev}'
.format(build_file_path=self.build_file_path, import_paths=import_paths, rev=rev))
rev = '' if self.local else f" {self.rev or 'FLOATING'}"
return (f'{self.build_file_path} ({import_paths}){rev}')

class FloatingRemoteError(TaskError):
"""Indicates Go remote libraries exist or were generated that don't specify a `rev`."""
Expand Down
8 changes: 4 additions & 4 deletions contrib/go/src/python/pants/contrib/go/tasks/go_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ def _go_install(self, target, gopath, build_flags):
result, go_cmd = self.go_dist.execute_go_cmd(
'install', gopath=gopath, args=args,
workunit_factory=self.context.new_workunit,
workunit_name='install {}'.format(target.import_path),
workunit_name=f'install {target.import_path}',
workunit_labels=[WorkUnitLabel.COMPILER])
if result != 0:
raise TaskError('{} failed with exit code {}'.format(go_cmd, result))
raise TaskError(f'{go_cmd} failed with exit code {result}')

def _sync_binary_dep_links(self, target, gopath, lib_binary_map):
"""Syncs symlinks under gopath to the library binaries of target's transitive dependencies.
Expand Down Expand Up @@ -149,8 +149,8 @@ def _get_cross_compiling_subdir_and_extension(self, gopath):
host_arch = self.go_dist.create_go_cmd('env', gopath=gopath, args=["GOARCH"]).check_output().decode().strip()
target_arch = self.go_dist.create_go_cmd('env', gopath=gopath, args=["GOHOSTARCH"]).check_output().decode().strip()

host_pair = "{}_{}".format(host_goos, host_arch)
target_pair = "{}_{}".format(target_goos, target_arch)
host_pair = f"{host_goos}_{host_arch}"
target_pair = f"{target_goos}_{target_arch}"

ext = ".exe" if target_goos == "windows" else ""

Expand Down
6 changes: 3 additions & 3 deletions contrib/go/src/python/pants/contrib/go/tasks/go_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _fetch_pkg(self, gopath, pkg, rev):
# Only fetch each remote root once.
if not os.path.exists(root_dir):
with temporary_dir() as tmp_fetch_root:
with self.context.new_workunit('fetch {}'.format(pkg)):
with self.context.new_workunit(f'fetch {pkg}'):
fetcher.fetch(dest=tmp_fetch_root, rev=rev)
safe_mkdir(root_dir)
for path in os.listdir(tmp_fetch_root):
Expand Down Expand Up @@ -108,7 +108,7 @@ def _map_fetched_remote_source(self, go_remote_lib, gopath, all_known_remote_lib
with safe_concurrent_creation(remote_import_paths_cache) as safe_path:
with open(safe_path, 'w') as fp:
for path in remote_import_paths:
fp.write('{}\n'.format(path))
fp.write(f'{path}\n')

for remote_import_path in remote_import_paths:
remote_root = import_root_map.get(remote_import_path)
Expand Down Expand Up @@ -277,4 +277,4 @@ def _write_import_root_map_file(path, import_root_map):
with safe_concurrent_creation(path) as safe_path:
with open(safe_path, 'w') as fp:
for import_path, root in sorted(import_root_map.items()):
fp.write('{}\t{}\n'.format(import_path, root))
fp.write(f'{import_path}\t{root}\n')
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def go_fmt_invalid_targets(self, flags):
try:
output = subprocess.check_output(args).decode()
except subprocess.CalledProcessError as e:
raise TaskError('{} failed with exit code {}'.format(' '.join(args), e.returncode),
raise TaskError(f"{' '.join(args)} failed with exit code {e.returncode}",
exit_code=e.returncode)
yield output
else:
Expand Down
4 changes: 2 additions & 2 deletions contrib/go/src/python/pants/contrib/go/tasks/go_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def execute_with_go_env(self, go_path, import_paths, args, **kwargs):
process = subprocess.Popen(cmd, shell=True, env=env, **kwargs)
result = process.wait()
if result != 0:
raise TaskError('{} failed with exit code {}'.format(cmd, result), exit_code=result)
raise TaskError(f'{cmd} failed with exit code {result}', exit_code=result)


class GoGo(GoInteropTask):
Expand All @@ -78,4 +78,4 @@ def execute_with_go_env(self, go_path, import_paths, args, **kwargs):
go_cmd = self.go_dist.create_go_cmd(gopath=go_path, cmd=cmd, args=args)
result = go_cmd.spawn(**kwargs).wait()
if result != 0:
raise TaskError('{} failed with exit code {}'.format(go_cmd, result), exit_code=result)
raise TaskError(f'{go_cmd} failed with exit code {result}', exit_code=result)
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ def execute_codegen(self, target, target_workdir):

bases = OrderedSet(tgt.target_base for tgt in target.closure() if self.is_gentarget(tgt))
for base in bases:
target_cmd.append('-I={}'.format(os.path.join(get_buildroot(), base)))
target_cmd.append(f'-I={os.path.join(get_buildroot(), base)}')

outdir = os.path.join(target_workdir, 'src', 'go')
safe_mkdir(outdir)
protoc_plugins = self.get_options().protoc_plugins + list(target.protoc_plugins)
if protoc_plugins:
go_out = 'plugins={}:{}'.format('+'.join(protoc_plugins), outdir)
go_out = f"plugins={'+'.join(protoc_plugins)}:{outdir}"
else:
go_out = outdir
target_cmd.append('--go_out={}'.format(go_out))
target_cmd.append(f'--go_out={go_out}')

all_sources = list(target.sources_relative_to_buildroot())
for source in all_sources:
Expand All @@ -92,7 +92,7 @@ def execute_codegen(self, target, target_workdir):
stdout=workunit.output('stdout'),
stderr=workunit.output('stderr'))
if result != 0:
raise TaskError('{} ... exited non-zero ({})'.format(self._protoc, result))
raise TaskError(f'{self._protoc} ... exited non-zero ({result})')

@property
def _copy_target_attributes(self):
Expand Down
3 changes: 1 addition & 2 deletions contrib/go/src/python/pants/contrib/go/tasks/go_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ def execute(self):
# TODO(cgibb): Wrap with workunit and stdout/stderr plumbing.
res = Xargs.subprocess([binary_path]).execute(self.get_passthru_args())
if res != 0:
raise TaskError('{bin} exited non-zero ({res})'
.format(bin=os.path.basename(binary_path), res=res))
raise TaskError(f'{os.path.basename(binary_path)} exited non-zero ({res})')
5 changes: 2 additions & 3 deletions contrib/go/src/python/pants/contrib/go/tasks/go_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def goos_goarch(self):
:rtype: string
"""
return '{goos}_{goarch}'.format(goos=self._lookup_go_env_var('GOOS'),
goarch=self._lookup_go_env_var('GOARCH'))
return f"{self._lookup_go_env_var('GOOS')}_{self._lookup_go_env_var('GOARCH')}"

def _lookup_go_env_var(self, var):
return self.go_dist.create_go_cmd('env', args=[var]).check_output().decode().strip()
Expand Down Expand Up @@ -143,7 +142,7 @@ def list_imports(self, pkg, gopath=None):
of `pkg`.
"""
go_cmd = self._go_dist.create_go_cmd('list', args=['-json', pkg], gopath=gopath)
with self._workunit_factory('list {}'.format(pkg), cmd=str(go_cmd),
with self._workunit_factory(f'list {pkg}', cmd=str(go_cmd),
labels=[WorkUnitLabel.TOOL]) as workunit:
# TODO(John Sirois): It would be nice to be able to tee the stdout to the workunit to we have
# a capture of the json available for inspection in the server console.
Expand Down
4 changes: 2 additions & 2 deletions contrib/go/src/python/pants/contrib/go/tasks/go_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _maybe_workdir(self):
return get_buildroot()

def run_tests(self, fail_fast, test_targets, args_by_target):
self.context.log.debug('test_targets: {}'.format(test_targets))
self.context.log.debug(f'test_targets: {test_targets}')

with self.chroot(test_targets, self._maybe_workdir) as chroot:
cmdline_args = self._build_and_test_flags + [
Expand All @@ -111,7 +111,7 @@ def run_tests(self, fail_fast, test_targets, args_by_target):
)
go_cmd = self.go_dist.create_go_cmd('test', gopath=gopath, args=cmdline_args)

self.context.log.debug('go_cmd: {}'.format(go_cmd))
self.context.log.debug(f'go_cmd: {go_cmd}')

workunit_labels = [WorkUnitLabel.TOOL, WorkUnitLabel.TEST]
with self.context.new_workunit(
Expand Down
8 changes: 4 additions & 4 deletions contrib/go/src/python/pants/contrib/go/tasks/go_thrift_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ def _validate_supports_more_than_one_source(self):
@memoized_property
def _thrift_cmd(self):
cmd = [self._thrift_binary]
thrift_import = 'thrift_import={}'.format(self.get_options().thrift_import)
thrift_import = f'thrift_import={self.get_options().thrift_import}'
if thrift_import is None:
raise TaskError('Option thrift_import in scope {} must be set.'.format(self.options_scope))
raise TaskError(f'Option thrift_import in scope {self.options_scope} must be set.')
gen_options = self.get_options().gen_options
if gen_options:
gen_options += ',' + thrift_import
else:
gen_options = thrift_import
cmd.extend(('--gen', 'go:{}'.format(gen_options)))
cmd.extend(('--gen', f'go:{gen_options}'))

if self.get_options().strict:
cmd.append('-strict')
Expand Down Expand Up @@ -149,7 +149,7 @@ def _generate_thrift(self, target, target_workdir):
stdout=workunit.output('stdout'),
stderr=workunit.output('stderr'))
if result != 0:
raise TaskError('{} ... exited non-zero ({})'.format(self._thrift_binary, result))
raise TaskError(f'{self._thrift_binary} ... exited non-zero ({result})')

gen_dir = os.path.join(target_workdir, 'gen-go')
src_dir = os.path.join(target_workdir, 'src')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_globs_cgo(self):
self.create_file('src/go/src/foo/jake.hh')
self.create_file('src/go/src/foo/jake.hpp')
self.create_file('src/go/src/foo/jake.hxx')
self.add_to_build_file('src/go/src/foo', '{}()\n'.format(self.target_type.alias()))
self.add_to_build_file('src/go/src/foo', f'{self.target_type.alias()}()\n')
target = self.target('src/go/src/foo')

self.assertEqual(sorted(['foo/jake.go',
Expand All @@ -111,7 +111,7 @@ def test_globs_resources(self):
# We should grab all of these though.
self.create_file('src/go/src/foo/jake.go')
self.create_file('src/go/src/foo/jake.png')
self.add_to_build_file('src/go/src/foo', '{}()'.format(self.target_type.alias()))
self.add_to_build_file('src/go/src/foo', f'{self.target_type.alias()}()')
target = self.target('src/go/src/foo')

self.assertEqual(sorted(['foo/jake.go',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_execute(self):
with temporary_dir() as bin_source_dir:
def create_binary(name):
target = self.make_target(name, GoBinary)
executable = os.path.join(bin_source_dir, '{}.exe'.format(name))
executable = os.path.join(bin_source_dir, f'{name}.exe')
touch(executable)
return target, executable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_resolve_and_inject_implicit_already_exists(self):

def _create_package(self, dirpath, name, deps):
"""Creates a Go package inside dirpath named 'name' importing deps."""
imports = ['import "localzip/{}"'.format(d) for d in deps]
imports = [f'import "localzip/{d}"' for d in deps]
f = os.path.join(dirpath, '{name}/{name}.go'.format(name=name))
self.create_file(f, contents=
"""package {name}
Expand All @@ -84,7 +84,7 @@ def _create_zip(self, src, dest, name):
shutil.make_archive(os.path.join(dest, name), 'zip', root_dir=src)

def _create_remote_lib(self, name):
self.make_target(spec='3rdparty/go/localzip/{name}'.format(name=name),
self.make_target(spec=f'3rdparty/go/localzip/{name}',
target_type=GoRemoteLibrary,
pkg=name)

Expand Down Expand Up @@ -113,7 +113,7 @@ def _assert_dependency_graph(self, root_target, dep_map):
if root_target.name not in dep_map:
return

expected_spec_paths = {'3rdparty/go/localzip/{}'.format(name)
expected_spec_paths = {f'3rdparty/go/localzip/{name}'
for name in dep_map[root_target.name]}
actual_spec_paths = {dep.address.spec_path for dep in root_target.dependencies}
self.assertEqual(actual_spec_paths, expected_spec_paths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def run(self, **kwargs):
:rtype: :class:`subprocess.Popen`
"""
env, kwargs = self._prepare_env(kwargs)
logger.debug('Running command {}'.format(self.cmd))
logger.debug(f'Running command {self.cmd}')
return subprocess.Popen(self.cmd, env=env, **kwargs)

def check_output(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _get_add_package_args(self, package, type_option, version_option):
PackageInstallationTypeOption.NO_SAVE: None,
}.get(type_option)
if package_type_option is None:
LOG.warning('{} does not support {} packages, ignored.'.format(self.name, type_option))
LOG.warning(f'{self.name} does not support {type_option} packages, ignored.')
elif package_type_option: # Skip over '' entries
return_args.append(package_type_option)
package_version_option = {
Expand All @@ -185,7 +185,7 @@ def _get_add_package_args(self, package, type_option, version_option):
}.get(version_option)
if package_version_option is None:
LOG.warning(
'{} does not support install with {} version, ignored'.format(self.name, version_option))
f'{self.name} does not support install with {version_option} version, ignored')
elif package_version_option: # Skip over '' entries
return_args.append(package_version_option)
return return_args
Expand All @@ -208,7 +208,7 @@ def _get_installation_args(self, install_optional, production_only, force, froze
if force:
return_args.append('--force')
if frozen_lockfile:
LOG.warning('{} does not support frozen lockfile option. Ignored.'.format(self.name))
LOG.warning(f'{self.name} does not support frozen lockfile option. Ignored.')
return return_args

def _get_add_package_args(self, package, type_option, version_option):
Expand All @@ -222,7 +222,7 @@ def _get_add_package_args(self, package, type_option, version_option):
PackageInstallationTypeOption.NO_SAVE: '--no-save',
}.get(type_option)
if package_type_option is None:
LOG.warning('{} does not support {} packages, ignored.'.format(self.name, type_option))
LOG.warning(f'{self.name} does not support {type_option} packages, ignored.')
elif package_type_option: # Skip over '' entries
return_args.append(package_type_option)
package_version_option = {
Expand All @@ -231,7 +231,7 @@ def _get_add_package_args(self, package, type_option, version_option):
}.get(version_option)
if package_version_option is None:
LOG.warning(
'{} does not support install with {} version, ignored.'.format(self.name, version_option))
f'{self.name} does not support install with {version_option} version, ignored.')
elif package_version_option: # Skip over '' entries
return_args.append(package_version_option)
return return_args
Expand Down
Loading

0 comments on commit 1d7e589

Please sign in to comment.