Skip to content

Commit

Permalink
formatting tools code (Azure#18983)
Browse files Browse the repository at this point in the history
  • Loading branch information
seankane-msft authored May 27, 2021
1 parent 9fa8e38 commit c4cbcee
Show file tree
Hide file tree
Showing 68 changed files with 1,517 additions and 1,555 deletions.
70 changes: 30 additions & 40 deletions tools/azure-devtools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,55 @@


CLASSIFIERS = [
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'License :: OSI Approved :: MIT License',
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"License :: OSI Approved :: MIT License",
]


DEPENDENCIES = [
'ConfigArgParse>=0.12.0',
'six>=1.10.0',
'vcrpy==3.0.0'
]
DEPENDENCIES = ["ConfigArgParse>=0.12.0", "six>=1.10.0", "vcrpy==3.0.0"]

with io.open('README.rst', 'r', encoding='utf-8') as f:
with io.open("README.rst", "r", encoding="utf-8") as f:
README = f.read()

setup(
name='azure-devtools',
name="azure-devtools",
version=VERSION,
description='Microsoft Azure Development Tools for SDK',
description="Microsoft Azure Development Tools for SDK",
long_description=README,
license='MIT',
author='Microsoft Corporation',
author_email='ptvshelp@microsoft.com',
url='https://github.com/Azure/azure-python-devtools',
license="MIT",
author="Microsoft Corporation",
author_email="ptvshelp@microsoft.com",
url="https://github.com/Azure/azure-python-devtools",
zip_safe=False,
classifiers=CLASSIFIERS,
packages=[
'azure_devtools',
'azure_devtools.scenario_tests',
'azure_devtools.perfstress_tests',
'azure_devtools.ci_tools',
"azure_devtools",
"azure_devtools.scenario_tests",
"azure_devtools.perfstress_tests",
"azure_devtools.ci_tools",
],
entry_points={
'console_scripts': [
'perfstress = azure_devtools.perfstress_tests:run_perfstress_cmd',
'systemperf = azure_devtools.perfstress_tests:run_system_perfstress_tests_cmd',
"console_scripts": [
"perfstress = azure_devtools.perfstress_tests:run_perfstress_cmd",
"systemperf = azure_devtools.perfstress_tests:run_system_perfstress_tests_cmd",
],
},
extras_require={
'ci_tools':[
"PyGithub>=1.40", # Can Merge PR after 1.36, "requests" and tests after 1.40
"ci_tools": [
"PyGithub>=1.40", # Can Merge PR after 1.36, "requests" and tests after 1.40
"GitPython",
"requests>=2.0"
],
'systemperf':[
"aiohttp>=3.0",
"requests>=2.0",
"tornado==6.0.3"
"pycurl==7.43.0.5"
"httpx==0.11.1"
]
],
"systemperf": ["aiohttp>=3.0", "requests>=2.0", "tornado==6.0.3" "pycurl==7.43.0.5" "httpx==0.11.1"],
},
package_dir={'': 'src'},
package_dir={"": "src"},
install_requires=DEPENDENCIES,
)
53 changes: 27 additions & 26 deletions tools/azure-devtools/src/azure_devtools/ci_tools/bot_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,38 @@ def order(function):
function.bot_order = True
return function

WebhookMetadata = namedtuple(
'WebhookMetadata',
['repo', 'issue', 'text', 'comment']
)

WebhookMetadata = namedtuple("WebhookMetadata", ["repo", "issue", "text", "comment"])


def build_from_issue_comment(gh_token, body):
"""Create a WebhookMetadata from a comment added to an issue.
"""
"""Create a WebhookMetadata from a comment added to an issue."""
if body["action"] in ["created", "edited"]:
github_con = Github(gh_token)
repo = github_con.get_repo(body['repository']['full_name'])
issue = repo.get_issue(body['issue']['number'])
text = body['comment']['body']
repo = github_con.get_repo(body["repository"]["full_name"])
issue = repo.get_issue(body["issue"]["number"])
text = body["comment"]["body"]
try:
comment = issue.get_comment(body['comment']['id'])
comment = issue.get_comment(body["comment"]["id"])
except UnknownObjectException:
# If the comment has already disapeared, skip the command
return None
return WebhookMetadata(repo, issue, text, comment)
return None


def build_from_issues(gh_token, body):
"""Create a WebhookMetadata from an opening issue text.
"""
"""Create a WebhookMetadata from an opening issue text."""
if body["action"] in ["opened", "edited"]:
github_con = Github(gh_token)
repo = github_con.get_repo(body['repository']['full_name'])
issue = repo.get_issue(body['issue']['number'])
text = body['issue']['body']
repo = github_con.get_repo(body["repository"]["full_name"])
issue = repo.get_issue(body["issue"]["number"])
text = body["issue"]["body"]
comment = issue # It's where we update the comment: in the issue itself
return WebhookMetadata(repo, issue, text, comment)
return None


@lru_cache()
def robot_name_from_env_variable():
github_con = Github(os.environ["GH_TOKEN"])
Expand All @@ -63,29 +62,31 @@ def __init__(self, handler, robot_name=None, gh_token=None):
self.robot_name = robot_name or robot_name_from_env_variable()

def _is_myself(self, body):
return body['sender']['login'].lower() == self.robot_name.lower()
return body["sender"]["login"].lower() == self.robot_name.lower()

def issue_comment(self, body):
if self._is_myself(body):
return {'message': 'I don\'t talk to myself, I\'m not schizo'}
return {"message": "I don't talk to myself, I'm not schizo"}
webhook_data = build_from_issue_comment(self.gh_token, body)
return self.manage_comment(webhook_data)

def issues(self, body):
if self._is_myself(body):
return {'message': 'I don\'t talk to myself, I\'m not schizo'}
return {"message": "I don't talk to myself, I'm not schizo"}
webhook_data = build_from_issues(self.gh_token, body)
return self.manage_comment(webhook_data)

def orders(self):
"""Return method tagged "order" in the handler.
"""
return [order_cmd for order_cmd in dir(self.handler)
if getattr(getattr(self.handler, order_cmd), "bot_order", False)]
"""Return method tagged "order" in the handler."""
return [
order_cmd
for order_cmd in dir(self.handler)
if getattr(getattr(self.handler, order_cmd), "bot_order", False)
]

def manage_comment(self, webhook_data):
if webhook_data is None:
return {'message': 'Nothing for me'}
return {"message": "Nothing for me"}
# Is someone talking to me:
message = re.search("@{} (.*)".format(self.robot_name), webhook_data.text, re.I)
response = None
Expand All @@ -97,7 +98,7 @@ def manage_comment(self, webhook_data):
response = self.help_order()
elif orderstr in self.orders():
try: # Reaction is fun, but it's preview not prod.
# Be careful, don't fail the command if we can't thumbs up...
# Be careful, don't fail the command if we can't thumbs up...
webhook_data.comment.create_reaction("+1")
except GithubException:
pass
Expand All @@ -110,8 +111,8 @@ def manage_comment(self, webhook_data):
response += self.help_order()
if response:
webhook_data.issue.create_comment(response)
return {'message': response}
return {'message': 'Nothing for me or exception'}
return {"message": response}
return {"message": "Nothing for me or exception"}

def help_order(self):
orders = ["This is what I can do:"]
Expand Down
28 changes: 16 additions & 12 deletions tools/azure-devtools/src/azure_devtools/ci_tools/git_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

_LOGGER = logging.getLogger(__name__)


def checkout_and_create_branch(repo, name):
"""Checkout branch. Create it if necessary"""
local_branch = repo.branches[name] if name in repo.branches else None
Expand All @@ -20,24 +21,24 @@ def checkout_and_create_branch(repo, name):
local_branch = repo.create_head(name)
local_branch.checkout()


def checkout_create_push_branch(repo, name):
"""Checkout this branch. Create it if necessary, and push it to origin.
"""
"""Checkout this branch. Create it if necessary, and push it to origin."""
try:
repo.git.checkout(name)
_LOGGER.info("Checkout %s success", name)
except GitCommandError:
_LOGGER.info("Checkout %s was impossible (branch does not exist). Creating it and push it.", name)
checkout_and_create_branch(repo, name)
repo.git.push('origin', name, set_upstream=True)
repo.git.push("origin", name, set_upstream=True)


def do_commit(repo, message_template, branch_name, hexsha):
"Do a commit if modified/untracked files"
repo.git.add(repo.working_tree_dir)

if not repo.git.diff(staged=True):
_LOGGER.warning('No modified files in this Autorest run')
_LOGGER.warning("No modified files in this Autorest run")
return False

checkout_and_create_branch(repo, branch_name)
Expand All @@ -46,6 +47,7 @@ def do_commit(repo, message_template, branch_name, hexsha):
_LOGGER.info("Commit done: %s", msg)
return commit.hexsha


def get_repo_hexsha(git_folder):
"""Get the SHA1 of the current repo"""
repo = Repo(str(git_folder))
Expand All @@ -57,6 +59,7 @@ def get_repo_hexsha(git_folder):
_LOGGER.info("Found REST API repo SHA1: %s", hexsha)
return hexsha


def checkout_with_fetch(git_folder, refspec, repository="origin"):
"""Fetch the refspec, and checkout FETCH_HEAD.
Beware that you will ne in detached head mode.
Expand All @@ -67,6 +70,7 @@ def checkout_with_fetch(git_folder, refspec, repository="origin"):
repo.git.checkout("FETCH_HEAD")
_LOGGER.info("Fetch and checkout success for %s", refspec)


def clone_to_path(https_authenticated_url, folder, branch_or_commit=None):
"""Clone the given URL to the folder.
Expand All @@ -82,23 +86,23 @@ def clone_to_path(https_authenticated_url, folder, branch_or_commit=None):

_LOGGER.info("Clone success")


def get_files_in_commit(git_folder, commit_id="HEAD"):
"""List of files in HEAD commit.
"""
"""List of files in HEAD commit."""
repo = Repo(str(git_folder))
output = repo.git.diff("--name-only", commit_id+"^", commit_id)
output = repo.git.diff("--name-only", commit_id + "^", commit_id)
return output.splitlines()


def get_diff_file_list(git_folder):
"""List of unstaged files.
"""
"""List of unstaged files."""
repo = Repo(str(git_folder))
output = repo.git.diff("--name-only")
return output.splitlines()



def get_add_diff_file_list(git_folder):
"""List of new files.
"""
"""List of new files."""
repo = Repo(str(git_folder))
repo.git.add("sdk")
output = repo.git.diff("HEAD", "--name-only")
Expand Down
Loading

0 comments on commit c4cbcee

Please sign in to comment.