Skip to content

Commit 2960439

Browse files
authored
Create generate_source.py
1 parent 49b4ce9 commit 2960439

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

.github/generate_source.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import requests
5+
import unicodedata
6+
import re
7+
import zipfile
8+
from github import Github
9+
from datetime import datetime
10+
11+
def slugify(value, allow_unicode=False):
12+
"""
13+
Taken from https://github.com/django/django/blob/master/django/utils/text.py
14+
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
15+
dashes to single dashes. Remove characters that aren't alphanumerics,
16+
underscores, or hyphens. Convert to lowercase. Also strip leading and
17+
trailing whitespace, dashes, and underscores.
18+
"""
19+
value = str(value)
20+
if allow_unicode:
21+
value = unicodedata.normalize('NFKC', value)
22+
else:
23+
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
24+
value = re.sub(r'[^\w\s-]', '', value.lower())
25+
return re.sub(r'[-\s]+', '-', value).strip('-_')
26+
27+
# Define the repository URL and the folder to clone to
28+
repo_name = "Leopard20/All-In-One-Command-Menu"
29+
repo_url = "https://github.com/"+repo_name
30+
local_folder = r"F:\Python\ww2\tmp"
31+
32+
# Define the list of files to keep in the folder
33+
files_to_keep = [".git",".github",".gitignore","LICENSE","README.md"]
34+
35+
# Create an instance of the Github API client
36+
# and authenticate using a token or username and password
37+
g = Github(GITHUB_TOKEN)
38+
39+
# Get the repository
40+
repo = g.get_repo(repo_name)
41+
42+
# Clone the repository
43+
result = subprocess.run(["git", "clone", repo_url, local_folder], stdout=subprocess.PIPE)
44+
print("Repository cloned!")
45+
print(result.stdout.decode())
46+
47+
# Get the list of releases from the repository
48+
releases = repo.get_releases().reversed
49+
50+
# Loop through the releases and download them
51+
for release in releases:
52+
# if release.created_at < datetime(2019, 8, 22, 16, 35, 54):
53+
# print(f"Skipping release {release.title}, too old ({release.created_at})")
54+
# continue
55+
release_file = os.path.join(local_folder, f"{slugify(release.title)}.zip")
56+
download_url = release.zipball_url
57+
# Remove files from the folder
58+
removed = 0
59+
for item in os.listdir(local_folder):
60+
if item not in files_to_keep:
61+
s = os.path.join(local_folder, item)
62+
if os.path.isdir(s): shutil.rmtree(s)
63+
else: os.unlink(s)
64+
removed += 1
65+
print(f"{removed} Unwanted files removed!")
66+
67+
# Download the release
68+
r = requests.get(download_url)
69+
open(release_file, "wb").write(r.content)
70+
print(f"{release.title} downloaded!")
71+
72+
# Unpack the release
73+
# result = subprocess.run(["unzip", , "-d", local_folder], stdout=subprocess.PIPE)
74+
# print(result.stdout.decode())
75+
with zipfile.ZipFile(release_file, 'r') as zfile:
76+
zfile.extractall(local_folder)
77+
print(f"{release_file} unpacked!")
78+
os.unlink(release_file)
79+
print(f"{release_file} deleted!")
80+
81+
# Add and commit the files with the release name as the commit message
82+
result = subprocess.run(["git", "-C", local_folder, "add", "."], stdout=subprocess.PIPE)
83+
print(result.stdout.decode())
84+
result = subprocess.run(["git", "-C", local_folder, "commit", "-m", release.title], stdout=subprocess.PIPE)
85+
print(f"{release.title} commited!")
86+
print(result.stdout.decode())
87+
88+
input("test")
89+
90+
# Push the changes to GitHub
91+
result = subprocess.run(["git", "-C", local_folder, "push", "origin", "master"], stdout=subprocess.PIPE)
92+
print("Changes pushed to GitHub!")
93+
print(result.stdout.decode())

0 commit comments

Comments
 (0)