Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03caee2

Browse files
committed
added python script used by github actions
1 parent 8bb054e commit 03caee2

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

‎.github/scripts/Update_Database.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from github import Github
2+
import json
3+
import sys
4+
import re
5+
6+
# Regex Patterns
7+
category = r"- \[x\] (.+)"
8+
name = r"Title: (.+)"
9+
path = r"Folder: (.+)"
10+
requirments_path = r"Requirments: (.+)"
11+
entry = r"Script: (.+)"
12+
arguments = r"Arguments: (.+)"
13+
contributor = r"Contributor: (.+)"
14+
description = r"Description: (.+)"
15+
16+
17+
def add_script(category, name, path, entry, arguments, requirments_path, contributor, description, pa_token):
18+
""" Add a Contributor script to database """
19+
new_data = {category: {name: [path, entry, arguments, requirments_path, contributor, description]}}
20+
data_store = read_data()
21+
22+
try:
23+
# If category doesn't exist try will fail and except will ask to add a new category with the project
24+
if data_store[category]: # Check for existing category or a new one
25+
data_store[category].update(new_data[category]) # Add script
26+
except:
27+
data_store.update(new_data) # Add new category
28+
29+
# <----- This part is to avoid a single/double quotes error when trying to update the database with PyGithub ----->
30+
with open("./Master Script/datastore.json", "w") as file:
31+
json.dump(data_store, file)
32+
print("Script added to database, pushing changes to repo...")
33+
34+
with open("./Master Script/datastore.json", "r") as file:
35+
data_store = file.readlines()[0]
36+
37+
# <----- Github Login & Database Update ----->
38+
git = Github(pa_token)
39+
user_object = git.get_user()
40+
git_username = user_object.login
41+
print("[+] PyGithub Login Success!")
42+
repo = git.get_repo("avinashkranjan/Amazing-Python-Scripts")
43+
datastore_file = repo.get_contents("./Master Script/datastore.json")
44+
repo.update_file(datastore_file.path, "Updated datastore.json", data_store, datastore_file.sha, branch="main")
45+
repo.update_file("./datastore.json", "Updated datastore.json", data_store, datastore_file.sha, branch="gh-pages")
46+
print("[+] Database Updated")
47+
48+
49+
def read_data():
50+
""" Loads datastore.json """
51+
with open("./Master Script/datastore.json", "r") as file:
52+
data = json.load(file)
53+
return data
54+
55+
56+
def extract_from_pr_body(pr_body, pa_token):
57+
""" Manipulates the provided PR body and extracts the required information """
58+
pr_body = pr_body.split("\n")
59+
for element in pr_body:
60+
pr_body[pr_body.index(element)] = element.rstrip("\r")
61+
pr_body = pr_body[pr_body.index("## Project Metadata"):]
62+
category_list = []
63+
for text in pr_body:
64+
# <----- Validate Category ----->
65+
cat = re.match(category, text)
66+
if cat is not None:
67+
category_list.append(cat[1])
68+
# <----- Validate Title ----->
69+
if re.match(name, text) is not None:
70+
title = re.match(name, text)[1]
71+
# <----- Validate Folder ----->
72+
if re.match(path, text) is not None:
73+
folder = re.match(path, text)[1]
74+
# <----- Validate requirments.txt ----->
75+
if re.match(requirments_path, text) is not None:
76+
requirements = re.match(requirments_path, text)[1]
77+
# <----- Validate Script.py ----->
78+
if re.match(entry, text) is not None:
79+
script = re.match(entry, text)[1]
80+
# <----- Validate Arguments ----->
81+
if re.match(arguments, text) is not None:
82+
argument = re.match(arguments, text)[1]
83+
# <----- Validate Contribute ----->
84+
if re.match(contributor, text) is not None:
85+
user = re.match(contributor, text)[1]
86+
# <----- Validate Description ----->
87+
if re.match(description, text) is not None:
88+
desc = re.match(description, text)[1]
89+
90+
# For GitHub Actions logging
91+
print("<----- MetaData ----->")
92+
print("Categories:", category_list)
93+
print("Title:", title)
94+
print("Path:", folder)
95+
print("Requirements:", requirements)
96+
print("Entry:", script)
97+
print("Arguments:",argument)
98+
print("Contributer:", user)
99+
print("Description:", desc)
100+
print("<----- ----- ----->")
101+
102+
# The loop is for scripts that will be added to multiple categories.
103+
for cat in category_list:
104+
add_script(cat, title, folder, script, argument, requirements, user, desc, pa_token)
105+
106+
107+
# Start Checkpoint
108+
if __name__ == "__main__":
109+
# Get PR body and pass pa_token
110+
data = sys.argv[1]
111+
extract_from_pr_body(data, sys.argv[2])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /