Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deprecation Notice to READMEs #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions deprecation_notice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Add Deprecation Notice to README on IBMCOM

## Setup

1. Download [chromedriver](https://sites.google.com/chromium.org/driver/) for Google Chrome/Chromium based browsers
2. Create a virtual environment for Python: `python -m venv venv`
3. Activate the virtual environment: `.\venv\Scripts\activate`
4. Modify the template `README.mustache` with [mustache](https://mustache.github.io/) templating (if required)

## Usage
1. Run the script `python ra_main.py`
2. Enter Docker Hub **Username** as well as **Password** as prompted
3. Enter the Absolute Browser path for Chromium-based browsers like _Edge, Brave_. Enter nothing if using _Google Chrome_
4. Enter Input CSV file path if we need to update specific READMEs, empty if we need to update all
6 changes: 6 additions & 0 deletions deprecation_notice/README.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Deprecation Notice

This repository will no longer be maintained.
{{#image}}
Images will be further maintained on ICR. You can pull the same by using the command: `docker pull {{image}}:<tag>`
{{/image}}
3 changes: 3 additions & 0 deletions deprecation_notice/input_list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,image
test-repo,
alpine_python_hello,icr.io/ppc64le-oss/alpine_python_hello
100 changes: 100 additions & 0 deletions deprecation_notice/ra_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import chevron
import getpass
import csv

REPOSITORY = "ibmcom"
TIMEOUT = 10

class ReadmeAutomation:
def __init__(self, browser) -> None:
options = None
if browser:
options = webdriver.ChromeOptions()
options.binary_location = browser
self.browser = webdriver.Chrome(options=options)

def login_to_dockerhub(self, user, pswd) -> None:
self.browser.get("https://login.docker.com/u/login")
username = self.browser.find_element(By.NAME, "username")
username.clear()
username.send_keys(user)
username.send_keys(Keys.RETURN)
password = self.browser.find_element(By.NAME, "password")
password.clear()
password.send_keys(pswd)
password.send_keys(Keys.RETURN)
WebDriverWait(self.browser, TIMEOUT).until(
EC.presence_of_element_located((By.XPATH, "//span[@data-testid='navBarUsernameDropdown']"))
)

def get_repositories_urls(self) -> list:
self.browser.get(f"https://hub.docker.com/repositories/{REPOSITORY}")
try:
repos = WebDriverWait(self.browser, TIMEOUT).until(
EC.presence_of_all_elements_located((By.XPATH, "//a[@data-testid='repositoryRowLink']"))
)
return [ {
'url': repo.get_attribute("href"),
'image': f'icr.io/ppc64le-oss/{repo.get_attribute("href").split("/")[-1]}'
} for repo in repos
]
except TimeoutException:
print("Failed to get repositories")

def accept_cookie_consent(self) -> None:
try:
cookies = WebDriverWait(self.browser, TIMEOUT).until(
EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))
)
cookies.click()
except TimeoutException:
print("Cookie consent dialogue not found, skipping consent")

def update_readme(self, input_file) -> None:
self.accept_cookie_consent() # Required to avoid obstruction of button visibility
with open('README.mustache', 'r') as template:
if input_file:
with open(input_file) as csv_file:
repos = []
for row in csv.DictReader(csv_file):
repos.append({
'url': f'https://hub.docker.com/repository/docker/{REPOSITORY}/{row["name"]}',
'image': row["image"]
})
else:
repos = self.get_repositories_urls()
for repo in repos:
self.browser.get(repo["url"])
try:
edit = WebDriverWait(self.browser, TIMEOUT).until(
EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='editRepoReadme']"))
)
edit.click()
textarea = self.browser.find_element(By.TAG_NAME, "textarea")
textarea.clear()
readme = chevron.render(template, {
# Add any variables defined inside template here in key:value pairs
# key = variable name in template
# value = data to be replaced in generated README
'image': repo["image"]
})
textarea.send_keys(readme)
update = self.browser.find_element(By.XPATH, "//button[text()='Update']")
update.click()
except TimeoutException:
print(f'Failed to update README for {repo["url"]}')

if __name__ == "__main__":
user = input("Enter Docker Hub Username: ")
pswd = getpass.getpass("Enter Docker Hub Password: ")
browser = input("Chromium-based browser binary path (Empty if browser is Chrome): ")
input_file = input("Input CSV file (Empty if running on whole repository): ")
ra = ReadmeAutomation(browser=browser)
ra.login_to_dockerhub(user=user, pswd=pswd)
ra.update_readme(input_file=input_file)
Binary file added deprecation_notice/requirements.txt
Binary file not shown.