def write(self, version: str): with open(self.path, 'r') as fp: config = json.load(fp, object_pairs_hook=OrderedDict) config['version'] = version fs.write_file(self.path, json.dumps(config, indent=2) + '\n')
def init(quick: bool, blank: bool, force: bool): """ Create an empty pelconf.yaml from template """ config_file = 'pelconf.yaml' prompt = "-- <35>{} <32>already exists. Wipe it?<0>".format(config_file) if not force and exists(config_file) and not click.confirm( shell.fmt(prompt)): log.info("Canceled") return ctx = dict(blank=blank) if not blank: form = InitForm().run(quick=quick) ctx.update(form.values) config_content = templates.Engine().render_file('pelconf.yaml', ctx) log.info('Writing <35>{}'.format(config_file)) fs.write_file(config_file, config_content) if context.get('verbose') > 0: print( f"{'- ' * 40}\n{shell.highlight(config_content, 'yaml')}{'- ' * 40}" )
def add_hooks(pre_commit: str, pre_push: str): """ Add git hooks for commit and push to run linting and tests. """ # Detect virtualenv the hooks should use # Detect virtualenv virtual_env = conf.get_env('VIRTUAL_ENV') if virtual_env is None: log.err("You are not inside a virtualenv") confirm_msg = ( "Are you sure you want to use global python installation " "to run your git hooks? [y/N] " ) click.prompt(confirm_msg, default='') if not click.confirm(confirm_msg): log.info("Cancelling") return load_venv = '' else: load_venv = 'source "{}/bin/activate"'.format(virtual_env) commit_hook = conf.proj_path('.git/hooks/pre-commit') push_hook = conf.proj_path('.git/hooks/pre-push') # Write pre-commit hook log.info("Adding pre-commit hook <33>{}", commit_hook) fs.write_file(commit_hook, util.remove_indent(''' #!/bin/bash PATH="/opt/local/libexec/gnubin:$PATH" {load_venv} {command} '''.format(load_venv=load_venv, command=pre_commit))) # Write pre-push hook log.info("Adding pre-push hook: <33>{}", push_hook) fs.write_file(push_hook, util.remove_indent(''' #!/bin/bash PATH="/opt/local/libexec/gnubin:$PATH" {load_venv} peltak test --allow-empty {command} '''.format(load_venv=load_venv, command=pre_push))) log.info("Making hooks executable") if not context.get('pretend', False): os.chmod(conf.proj_path('.git/hooks/pre-commit'), 0o755) os.chmod(conf.proj_path('.git/hooks/pre-push'), 0o755)
def write(self, version: str): """ Write the project version to .py file. This will regex search in the file for a ``__version__ = VERSION_STRING`` and substitute the version string for the new version. """ with open(self.path) as fp: content = fp.read() ver_statement = "__version__ = '{}'".format(version) new_content = RE_PY_VERSION.sub(ver_statement, content) fs.write_file(self.path, new_content)
def gen_pypirc(username: Optional[str] = None, password: Optional[str] = None): """ Generate ~/.pypirc with the given credentials. Useful for CI builds. Can also get credentials through env variables ``PYPI_USER`` and ``PYPI_PASS``. Args: username (str): pypi username. If not given it will try to take it from the `` PYPI_USER`` env variable. password (str): pypi password. If not given it will try to take it from the `` PYPI_PASS`` env variable. """ path = join(conf.get_env('HOME'), '.pypirc') username = username or conf.get_env('PYPI_USER', None) password = password or conf.get_env('PYPI_PASS', None) if username is None or password is None: log.err("You must provide $PYPI_USER and $PYPI_PASS") sys.exit(1) log.info("Generating <94>{}".format(path)) fs.write_file( path, util.remove_indent(''' [distutils] index-servers = pypi [pypi] repository: https://upload.pypi.org/legacy/ username: {username} password: {password} '''.format(username=username, password=password)))
def write(self, version: str): config = util.toml_load(self.path) config['tool']['poetry']['version'] = version fs.write_file(self.path, util.toml_dump(config))
def write(self, version: str): fs.write_file(self.path, version)