Created
February 23, 2017 20:00
-
-
Save gambtho/b7bbe317f6e22eb89372f617f986a977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import asyncio | |
from git import Repo | |
import github3 | |
import json | |
import os | |
import time | |
import subprocess | |
with open('<<file with data to search for>>') as json_data: | |
toggles = json.load(json_data) | |
gh = github3.login(os.environ['GIT_USER'], os.environ['GIT_PASS']) | |
org = gh.organization('<<GIT ORG NAME>>') | |
async def find_toggles(repo): | |
toggles_found = [] | |
for toggle in toggles: | |
found = await findfiles(repo, toggle) | |
if found: | |
toggles_found.append(toggle) | |
print("{} - toggles found: {}".format(repo, len(toggles_found))) | |
print('---------------------------------------') | |
return toggles_found | |
def build_file(repo, toggles): | |
array = [] | |
for toggle in toggles: | |
array.append({"toggle_name": toggle, "description": "", "toggle_type": "legacy"}) | |
return {"item": { "user": repo, "toggles": array}} | |
async def clone_and_search(repo): | |
start_time = time.time() | |
repo_loc = "/tmp/" + repo.full_name | |
try: | |
Repo.clone_from(repo.clone_url, repo_loc) | |
except: | |
print("did not download - {}".format(repo.full_name)) | |
branch_name = "toggle_registration" | |
toggles = await find_toggles(repo_loc) | |
if len(toggles) > 0: | |
cloned_repo = Repo(repo_loc) | |
cloned_repo.git.checkout('HEAD', b=branch_name) | |
file_path = '{}/toggles.json'.format(repo_loc) | |
with open('{}'.format(file_path), 'w') as outfile: | |
json.dump(build_file(repo.full_name, toggles), outfile) | |
cloned_repo.index.add([file_path]) | |
cloned_repo.index.commit("Added toggle registration file") | |
cloned_repo.git.push("--set-upstream", "origin", branch_name) | |
repo.create_pull("Pull request for toggle registration file", "master", branch_name) | |
print("--- {} - {} seconds ---".format(repo.full_name, (time.time() - start_time))) | |
def main(): | |
start_time = time.time() | |
loop = asyncio.get_event_loop() | |
repos = org.iter_repos() | |
data = map(clone_and_search, repos) | |
loop.run_until_complete(asyncio.wait(data)) | |
print("--- %s seconds ---" % (time.time() - start_time)) | |
async def findfiles(path, toggle): | |
ack = subprocess.Popen(['ag', toggle, "--scala", "--java", "--jsp", "--perl", "--silent", "-i", "-w", "-c", "--max-count", "1", path], stdout=subprocess.PIPE,) | |
wc = subprocess.Popen(['wc', "-l"], stdin=ack.stdout, stdout=subprocess.PIPE,) | |
output = wc.communicate()[0] | |
return int(output.strip()) > 0 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment