Skip to content

Commit

Permalink
Fix denpendence
Browse files Browse the repository at this point in the history
  • Loading branch information
youngfish42 committed Jan 12, 2024
1 parent ac7e651 commit 2394320
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions add_dependent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import re
import re
import requests
import time

# Path to the local markdown file
file_path = 'README.md'

# # Regular expression to match URLs enclosed in parentheses
url_pattern = re.compile(r'\(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\)')
# # Regular expression to match Github project URLs
# github_url_pattern = re.compile(r'^https://github.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/?$')
github_url_pattern = re.compile(r'^http[s]?://github.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/?$')
# Regular expression to match Github project URLs with optional additional paths
github_url_pattern = re.compile(r'^http[s]?://github.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(/.*)?/?$')
# github_url_pattern = re.compile(r'^http[s]?://github.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(/.*)?/?$')


github_links = set() # Use a set to automatically remove duplicates
Expand All @@ -23,11 +25,43 @@
# Filter out Github project URLs and add them to the set
github_links.update([url for url in urls if re.match(github_url_pattern, url)])

# Sort the links
github_links = sorted(github_links)

# Save the github_links into file
with open('github_links.txt', 'w') as f:
for link in github_links:
f.write(link + '\n')

# Get the commit hashes for each project
commit_hashes = {}
headers = {"Authorization": "Input Your key"} # replace "your_token" with your Github token
for link in github_links:
try:
# Remove http:// or https:// from the URL
link = link.replace('http://', '').replace('https://', '')
owner, repo = link.split("/")[1:3]
url = f"https://api.github.com/repos/{owner}/{repo}/commits/main"
response = requests.get(url,headers=headers)
response.raise_for_status()
commit_hashes[link] = response.json()["sha"]
print(f"Got commit hash for {link}: {commit_hashes[link]}")
except requests.exceptions.HTTPError as e:
if e.response.status_code == 422:
print(f"'main' branch not found for {link}, trying 'master' branch")
try:
url = f"https://api.github.com/repos/{owner}/{repo}/commits/master"
response = requests.get(url,headers=headers)
response.raise_for_status()
commit_hashes[link] = response.json()["sha"]
except Exception as e:
print(f"Error fetching commit info for {link}: {e}")
else:
print(f"Error fetching commit info for {link}: {e}")
except Exception as e:
print(f"Error getting commit hash for {link}: {e}")
time.sleep(1) # Wait 1 second to avoid rate limiting


# Start the go.mod file with the module line
go_mod = "module github.com/youngfish42/Awesome-FL\n\n" # replace "mymodule" with your module name
Expand All @@ -36,10 +70,8 @@
# Start the require block
go_mod += "require (\n"
# Add the dependencies
for link in github_links:
# Remove http:// or https:// from the URL
link = link.replace('http://', '').replace('https://', '')
go_mod += f" {link} v0.0.0 // indirect\n" # replace "v0.0.0" with the actual version if known
for link,hash in commit_hashes.items():
go_mod += f" {link} {hash} // indirect\n" #
# End the require block
go_mod += ")\n"

Expand Down

0 comments on commit 2394320

Please sign in to comment.