def read_format_write(src, dst): with src.open('r') as f: code = f.read() code = blacken_code(code) with dst.open('w') as f: f.write(code)
def test_blacken_code(self): input = '''\ x = { 'a':37,'b':42, 'c':927} '''.strip() expected = 'x = {"a": 37, "b": 42, "c": 927}'.strip() actual = blacken_code(input).strip() self.assertEqual(actual, expected)
def read_format_write(src, dst): with src.open("r") as f: code = f.read() try: code = blacken_code(code) except Exception as e: # let's not deal with blacken issues... print(e) with dst.open("w") as f: f.write(code)
def test_blacken_code(): input = '''\ x = { 'a':37,'b':42, 'c':927} '''.strip() expected = 'x = {"a": 37, "b": 42, "c": 927}'.strip() actual = blacken_code(input).strip() assert actual == expected
def test_blacken_code_nothing_changed(self): input = '1\n' expected = '1\n' actual = blacken_code(input) self.assertEqual(actual, expected)
def test_blacken_code_nothing_changed(): input = '1\n' expected = '1\n' actual = blacken_code(input) assert actual == expected
def create_pull_request_for_code_content(code_content): with stacklog(app.logger.info, 'Checking for valid code'): if not is_valid_python(code_content): return { 'result': False, 'url': None, 'message': 'Submitted code is not valid Python code', } with tempfile.TemporaryDirectory() as dirname: dirname = str(pathlib.Path(dirname).resolve()) # clone directory to dir with stacklog(app.logger.info, 'Cloning repo'): repo = git.Repo.clone_from(REPO_URL, to_path=dirname) with work_in(dirname): # configure repo with stacklog(app.logger.info, 'Configuring repo'): set_config_variables( repo, { 'user.name': 'Demo1', 'user.email': '*****@*****.**', }) repo.remote().set_url(REPO_URL) # create a new branch with stacklog(app.logger.info, 'Creating new branch and checking it out'): feature_name, branch_name = make_feature_and_branch_name() repo.create_head(branch_name) repo.heads[branch_name].checkout() # start new feature with stacklog(app.logger.info, 'Starting new feature'): extra_context = { 'username': USERNAME.replace('-', '_'), 'featurename': feature_name, } changes = ballet.templating.start_new_feature( no_input=True, extra_context=extra_context) changed_files = [ str(pathlib.Path(name).relative_to(dirname)) for (name, kind) in changes if kind == 'file' ] new_feature_path = get_new_feature_path(changes) # add code content to path with stacklog(app.logger.info, 'Adding code content'): with open(new_feature_path, 'w') as f: blackened_code_content = blacken_code(code_content) f.write(blackened_code_content) # commit new code with stacklog(app.logger.info, 'Committing new feature'): repo.index.add(changed_files) repo.index.commit('Add new feature') # push to branch with stacklog(app.logger.info, 'Pushing to remote'): refspec = f'refs/heads/{branch_name}:refs/heads/{branch_name}' if not is_debug(): repo.remote().push(refspec=refspec) # create pull request with stacklog(app.logger.info, 'Creating pull request'): github = Github(PASSWORD) grepo = github.get_repo(UPSTREAM_REPO_SPEC) title = 'Propose new feature' body = dedent(f'''\ Propose new feature: {feature_name} Submitted by user: {USERNAME} -- Pull request automatically created by ballet-submit-server ''') base = 'master' head = f'{USERNAME}:{branch_name}' maintainer_can_modify = True app.logger.debug( f'About to create pull: title={title}, body={body}, base={base}, head={head}' ) if not is_debug(): pr = grepo.create_pull( title=title, body=body, base=base, head=head, maintainer_can_modify=maintainer_can_modify) return { 'result': True, 'url': pr.html_url, 'message': None, } if is_debug(): return { 'result': True, 'url': 'http://some/testing/url', 'message': None, }
def write_feature_content(content, handle): content = get_feature_imports() + content content = blacken_code(content) handle.write(content)
def write_code_content(self, new_feature_path: str, code_content: str): with open(new_feature_path, 'w') as f: blackened_code_content = blacken_code(code_content) f.write(blackened_code_content)