Skip to content

Commit

Permalink
Switch to f-strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Jan 15, 2021
1 parent 190ddce commit d1ab989
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 84 deletions.
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ release = clean --all sdist bdist_wheel upload

test = pytest

[wheel]
universal = 1

6 changes: 3 additions & 3 deletions webscaff/commands/run/certbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def get_certificate(ctx):
webroot = ctx.paths.remote.project.state.certbot

if email:
email = '--email %s' % email
email = f'--email {email}'

command = (
'certbot --agree-tos --no-eff-email %s certonly --webroot -d %s -w %s' %
(email, domain, webroot))
f'certbot --agree-tos --no-eff-email {email} certonly '
f'--webroot -d {domain} -w {webroot}')

ctx.sudo(command)

Expand Down
6 changes: 3 additions & 3 deletions webscaff/commands/run/dj.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def manage(ctx, cmd):

project_name = ctx.project.name
for command in cmd:
ctx.sudo('%s %s' % (project_name, command), pty=True, user=project_name)
ctx.sudo(f'{project_name} {command}', pty=True, user=project_name)


def rollout(ctx):
Expand All @@ -46,9 +46,9 @@ def create_superuser(ctx):
email = ctx.project.email or ''
if email:
username = email.partition('@')[0]
command += ' --email %s --username %s' % (email, username)
command += f' --email {email} --username {username}'

echo('\nCreating Django superuser %s ...' % ('[%s]' % username) if username else '')
echo('\nCreating Django superuser %s ...' % f'[{username}]' if username else '')

manage(ctx, command)

Expand Down
6 changes: 3 additions & 3 deletions webscaff/commands/run/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create_environ_file(ctx, type_marker='production'):
def symlink_home(ctx):
"""Create a directory with project-related symlinks in user's home."""

home_linkroot = '~/%s' % ctx.project.name
home_linkroot = f'~/{ctx.project.name}'

sys_fs.mkdir(ctx, home_linkroot)

Expand All @@ -30,7 +30,7 @@ def symlink_home(ctx):
]

for dir_, linkname in dir_map:
ctx.sudo(get_symlink_command(dir_, '%s/%s' % (home_linkroot, linkname)))
ctx.sudo(get_symlink_command(dir_, f'{home_linkroot}/{linkname}'))


def upload_configs(ctx):
Expand All @@ -42,7 +42,7 @@ def upload_configs(ctx):
rsync(ctx, source, ctx.paths.remote.configs, delete=True)

else:
echo('No configuration files uploaded. Expected: %s' % source)
echo(f'No configuration files uploaded. Expected: {source}')


def create_dir(ctx, path):
Expand Down
2 changes: 1 addition & 1 deletion webscaff/commands/run/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

def stream(ctx):
"""Outputs project service log."""
ctx.sudo('journalctl -fu %s' % ctx.project.name)
ctx.sudo(f'journalctl -fu {ctx.project.name}')
7 changes: 4 additions & 3 deletions webscaff/commands/run/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def psql(ctx, command=None):
command = command or ''

if command:
command = ' -%s "%s"' % ('f' if '/' in command else 'c', command)
opt = 'f' if '/' in command else 'c'
command = f' -{opt} "{command}"'

ctx.sudo('psql%s' % command, user=ctx.project.user)
ctx.sudo(f'psql{command}', user=ctx.project.user)


@task
Expand Down Expand Up @@ -92,4 +93,4 @@ def reindex(ctx, table):
:param str table: Table name
"""
psql(ctx, 'REINDEX TABLE %s' % table)
psql(ctx, f'REINDEX TABLE {table}')
10 changes: 5 additions & 5 deletions webscaff/commands/run/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ def bootstrap(ctx):
"""Creates a systemd service file and to enable it."""

project_name = ctx.project.name
service_file = Path(ctx.paths.remote.configs) / ('%s.service' % project_name)
service_file = Path(ctx.paths.remote.configs) / f'{project_name}.service'

ctx.sudo('systemctl enable --now %s' % service_file)
ctx.sudo(f'systemctl enable --now {service_file}')


def restart(ctx):
"""Restarts project service."""
ctx.sudo('systemctl restart %s' % ctx.project.name)
ctx.sudo(f'systemctl restart {ctx.project.name}')


def status(ctx):
"""Returns project service status."""
ctx.sudo('systemctl status %s' % ctx.project.name)
ctx.sudo(f'systemctl status {ctx.project.name}')


@task
def stop(ctx):
"""Stops project service."""
ctx.sudo('systemctl stop %s' % ctx.project.name)
ctx.sudo(f'systemctl stop {ctx.project.name}')
13 changes: 5 additions & 8 deletions webscaff/commands/run/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ def backup(ctx):
makedirs(path_dump_local, exist_ok=True)

path_dumps = Path(ctx.paths.remote.project.state.dumps)
path_dump = path_dumps / ('%s-%s_dump' % (
datetime.now().strftime('%Y-%m-%dT%H%M'),
ctx.project.name
))
path_dump = path_dumps / f"{datetime.now().strftime('%Y-%m-%dT%H%M')}-{ctx.project.name}_dump"

# Create a subdirectory in dumps.
sys_fs.mkdir(ctx, path_dump)
Expand Down Expand Up @@ -137,9 +134,9 @@ def restore(ctx, backup):
path_dumps = Path(ctx.paths.remote.project.state.dumps)
path_remote_archive = path_dumps / backup.name

echo('Uploading %s ...' % backup)
echo(f'Uploading {backup} ...')

ctx.put('%s' % backup, '%s' % path_remote_archive)
ctx.put(f'{backup}', f'{path_remote_archive}')
path_remote = sys_fs.gzip_extract(ctx, path_remote_archive)

try:
Expand All @@ -164,11 +161,11 @@ def py(ctx, filepath):
path_temp = Path(ctx.paths.remote.temp)
path_remote_script = path_temp / str(uuid4())

ctx.put('%s' % script, '%s' % path_remote_script)
ctx.put(f'{script}', f'{path_remote_script}')

try:
with venv.venv_context(ctx):
ctx.run('python %s' % path_remote_script, warn=True)
ctx.run(f'python {path_remote_script}', warn=True)

finally:
sys_fs.rm(ctx, path_remote_script)
12 changes: 6 additions & 6 deletions webscaff/commands/run/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def symlink_entypoint(ctx):
"""Create a system-wide symlink to a project entrypoint."""
project_name = ctx.project.name
ctx.sudo(get_symlink_command(
'%s/%s' % (ctx.paths.remote.project.venv.bin, project_name),
'/usr/bin/%s' % project_name))
f'{ctx.paths.remote.project.venv.bin}/{project_name}',
f'/usr/bin/{project_name}'))


@contextmanager
def venv_context(ctx):
"""Temporarily switches into virtual environment."""

with ctx.prefix('. %s/activate' % ctx.paths.remote.project.venv.bin):
with ctx.prefix(f'. {ctx.paths.remote.project.venv.bin}/activate'):
yield


Expand All @@ -60,13 +60,13 @@ def install(ctx, package='', update=False, from_req=False, editable=False):
editable and flags.append('-e')

if from_req:
package = '-r %s' % (Path(ctx.paths.remote.project.home) / PIP_REQUIREMENTS_FILENAME)
package = f'-r {Path(ctx.paths.remote.project.home) / PIP_REQUIREMENTS_FILENAME}'

if not isinstance(package, list):
package = [package]

with venv_context(ctx):
ctx.run('pip3 install %s %s' % (' '.join(flags), ' '.join(package)))
ctx.run(f"pip3 install {' '.join(flags)} {' '.join(package)}")


@task
Expand All @@ -90,4 +90,4 @@ def install_vcs(ctx, package, vcs_path):
* git://github.com/idlesign/uwsgiconf@master
"""
install(ctx, 'git+%s#egg=%s' % (vcs_path, package), editable=True)
install(ctx, f'git+{vcs_path}#egg={package}', editable=True)
2 changes: 1 addition & 1 deletion webscaff/commands/sys/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def install(ctx, packages):

update(ctx)

ctx.sudo('apt install -y %s' % ' '.join(packages), env={'DEBIAN_FRONTEND': 'noninteractive'})
ctx.sudo(f"apt install -y {' '.join(packages)}", env={'DEBIAN_FRONTEND': 'noninteractive'})


def bootstrap(ctx):
Expand Down
40 changes: 18 additions & 22 deletions webscaff/commands/sys/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def make_tmp_file(ctx, contents):
:rtype: str
"""
fpath = '/tmp/wscf_%s' % uuid4()
fpath = f'/tmp/wscf_{uuid4()}'

append_to_file(ctx, fpath, contents)

Expand All @@ -51,22 +51,22 @@ def make_tmp_file(ctx, contents):

def mkdir(ctx, path):
"""Creates a directory."""
ctx.sudo('mkdir -p %s' % path)
ctx.sudo(f'mkdir -p {path}')


def chmod(ctx, path, mode):
"""Change fs object permissions."""
ctx.sudo('chmod %s %s' % (mode, path))
ctx.sudo(f'chmod {mode} {path}')


def chown(ctx, path, user, group):
"""Sets owner for path contents recursively."""
ctx.sudo('chown -R %s:%s %s' % (user, group, path))
ctx.sudo(f'chown -R {user}:{group} {path}')


def touch(ctx, fpath):
"""Creates a file or updates modified date if already exists."""
ctx.run('touch %s' % fpath)
ctx.run(f'touch {fpath}')


def setfacl(ctx, path, acl, modify=False):
Expand All @@ -82,17 +82,13 @@ def setfacl(ctx, path, acl, modify=False):
"""
mode = 'm' if modify else ' --set'

ctx.sudo('setfacl -R%(mode)s "%(acl)s" %(path)s' % {
'mode': mode,
'acl': acl,
'path': path
})
ctx.sudo(f'setfacl -R{mode} "{acl}" {path}')


def rm(ctx, target, force=True):
"""Removes target file or directory recursively."""
ctx.sudo('rm -r%s %s' % ('f' if force else '', target))
opt = 'f' if force else ''
ctx.sudo(f'rm -r{opt} {target}')


def gzip_extract(ctx, archive, target_dir=None, do_sudo=False):
Expand All @@ -102,10 +98,10 @@ def gzip_extract(ctx, archive, target_dir=None, do_sudo=False):

mkdir(ctx, target_dir)

echo('Extract into %s ...' % target_dir)
echo(f'Extract into {target_dir} ...')

method = ctx.sudo if do_sudo else ctx.run
method('tar -xzf %s -C %s' % (archive, target_dir))
method(f'tar -xzf {archive} -C {target_dir}')

return target_dir

Expand All @@ -117,11 +113,11 @@ def gzip_dir(ctx, src, target_fname, do_sudo=False):
arch_ext = '.tar.gz'

if arch_ext not in target_fname:
target_fname = '%s%s' % (target_fname, arch_ext)
target_fname = f'{target_fname}{arch_ext}'

echo('Creating %s ...' % target_fname)
echo(f'Creating {target_fname} ...')

command = 'tar -czf %s *' % target_fname
command = f'tar -czf {target_fname} *'
command = cd_sudo(src, command)
method = ctx.sudo if do_sudo else ctx.run

Expand All @@ -134,25 +130,25 @@ def gzip_dir(ctx, src, target_fname, do_sudo=False):
def tail(ctx, filepath):
"""Tails a file to output."""
# todo maybe use a more powerful tail from orchestra
ctx.sudo('tail -f %s' % filepath)
ctx.sudo(f'tail -f {filepath}')


def swap_init(ctx):
"""Creates a swap file."""
swap_file = '/swapfile'
ctx.sudo('dd if=/dev/zero of=%s bs=1024 count=524288' % swap_file)
ctx.sudo(f'dd if=/dev/zero of={swap_file} bs=1024 count=524288')
chmod(ctx, swap_file, 600)
ctx.sudo('mkswap %s' % swap_file)
ctx.sudo(f'mkswap {swap_file}')
swap_on(ctx)


def swap_on(ctx):
"""Turns on swap."""
swap_file = '/swapfile'
ctx.sudo('swapon %s' % swap_file)
ctx.sudo(f'swapon {swap_file}')


def swap_off(ctx):
"""Turns off swap."""
swap_file = '/swapfile'
ctx.sudo('swapoff %s' % swap_file)
ctx.sudo(f'swapoff {swap_file}')
2 changes: 1 addition & 1 deletion webscaff/commands/sys/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def clone(ctx, path_base, repo_url, dir_target):
ctx.run('ssh -T git@github.com', warn=True)

with ctx.cd(path_base):
ctx.run('git clone -v %s %s' % (repo_url, dir_target))
ctx.run(f'git clone -v {repo_url} {dir_target}')


def pull(ctx, path_base):
Expand Down

0 comments on commit d1ab989

Please sign in to comment.