Skip to content

Commit 92a9cd5

Browse files
authored
🔀 Merge pull request #71 from pabnas/general_fix_and_update_styles
General fix and update styles
2 parents ef10c66 + 78a9726 commit 92a9cd5

File tree

15 files changed

+972
-522
lines changed

15 files changed

+972
-522
lines changed

‎.github/__init__.py‎

Whitespace-only changes.

‎.github/actions.py‎

Lines changed: 90 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import json
32
import copy
43
import re
54
import shutil
@@ -11,12 +10,35 @@
1110
TEMPLATE_FILE = "pkg_template.html"
1211
YAML_ACTION_FILES = [".github/workflows/delete.yml", ".github/workflows/update.yml"]
1312

13+
INDEX_CARD_HTML = '''
14+
<a class="card" href="">
15+
placeholder_name
16+
<span>
17+
</span>
18+
<span class="version">
19+
placehholder_version
20+
</span>
21+
<br/>
22+
<span class="description">
23+
placeholder_description
24+
</span>
25+
</a>'''
26+
1427

1528
def normalize(name):
1629
""" From PEP503 : https://www.python.org/dev/peps/pep-0503/ """
1730
return re.sub(r"[-_.]+", "-", name).lower()
1831

1932

33+
def normalize_version(version):
34+
version = version.lower()
35+
return version[1:] if version.startswith("v") else version
36+
37+
38+
def is_stable(version):
39+
return not ("dev" in version or "a" in version or "b" in version or "rc" in version)
40+
41+
2042
def package_exists(soup, package_name):
2143
package_ref = package_name + "/"
2244
for anchor in soup.find_all('a'):
@@ -25,26 +47,40 @@ def package_exists(soup, package_name):
2547
return False
2648

2749

28-
def register(pkg_name, version, author, short_desc, long_desc, homepage, link):
50+
def transform_github_url(input_url):
51+
# Split the input URL to extract relevant information
52+
parts = input_url.rstrip('/').split('/')
53+
username, repo = parts[-2], parts[-1]
54+
55+
# Create the raw GitHub content URL
56+
raw_url = f'https://raw.githubusercontent.com/{username}/{repo}/main/README.md'
57+
return raw_url
58+
59+
60+
def register(pkg_name, version, author, short_desc, homepage):
61+
link = f'git+{homepage}@{version}'
62+
long_desc = transform_github_url(homepage)
2963
# Read our index first
3064
with open(INDEX_FILE) as html_file:
3165
soup = BeautifulSoup(html_file, "html.parser")
3266
norm_pkg_name = normalize(pkg_name)
67+
norm_version = normalize_version(version)
3368

3469
if package_exists(soup, norm_pkg_name):
35-
raise ValueError("Package {} seems to already exists".format(norm_pkg_name))
70+
raise ValueError(f"Package {norm_pkg_name} seems to already exists")
3671

3772
# Create a new anchor element for our new package
38-
last_anchor = soup.find_all('a')[-1] # Copy the last anchor element
39-
new_anchor = copy.copy(last_anchor)
40-
new_anchor['href'] = "{}/".format(norm_pkg_name)
41-
new_anchor.contents[0].replace_with(pkg_name)
42-
spans = new_anchor.find_all('span')
43-
spans[1].string = version # First span contain the version
73+
placeholder_card = BeautifulSoup(INDEX_CARD_HTML, 'html.parser')
74+
placeholder_card = placeholder_card.find('a')
75+
new_package = copy.copy(placeholder_card)
76+
new_package['href'] = f"{norm_pkg_name}/"
77+
new_package.contents[0].replace_with(pkg_name)
78+
spans = new_package.find_all('span')
79+
spans[1].string = norm_version # First span contain the version
4480
spans[2].string = short_desc # Second span contain the short description
4581

4682
# Add it to our index and save it
47-
last_anchor.insert_after(new_anchor)
83+
soup.find('h6', class_='text-header').insert_after(new_package)
4884
with open(INDEX_FILE, 'wb') as index:
4985
index.write(soup.prettify("utf-8"))
5086

@@ -53,49 +89,71 @@ def register(pkg_name, version, author, short_desc, long_desc, homepage, link):
5389
template = temp_file.read()
5490

5591
template = template.replace("_package_name", pkg_name)
56-
template = template.replace("_version", version)
57-
template = template.replace("_link", "{}#egg={}-{}".format(link, norm_pkg_name, version))
92+
template = template.replace("_version", norm_version)
93+
template = template.replace("_link", f"{link}#egg={norm_pkg_name}-{norm_version}")
5894
template = template.replace("_homepage", homepage)
5995
template = template.replace("_author", author)
6096
template = template.replace("_long_description", long_desc)
97+
template = template.replace("_latest_main", norm_version)
6198

6299
os.mkdir(norm_pkg_name)
63100
package_index = os.path.join(norm_pkg_name, INDEX_FILE)
64101
with open(package_index, "w") as f:
65102
f.write(template)
66103

67104

68-
def update(pkg_name, version, link):
105+
def update(pkg_name, version):
69106
# Read our index first
70107
with open(INDEX_FILE) as html_file:
71108
soup = BeautifulSoup(html_file, "html.parser")
72109
norm_pkg_name = normalize(pkg_name)
110+
norm_version = normalize_version(version)
73111

74112
if not package_exists(soup, norm_pkg_name):
75-
raise ValueError("Package {} seems to not exists".format(norm_pkg_name))
113+
raise ValueError(f"Package {norm_pkg_name} seems to not exists")
76114

77-
# Change the version in the main page
78-
anchor = soup.find('a', attrs={"href": "{}/".format(norm_pkg_name)})
79-
spans = anchor.find_all('span')
80-
spans[1].string = version
81-
with open(INDEX_FILE, 'wb') as index:
82-
index.write(soup.prettify("utf-8"))
115+
# Change the version in the main page (only if stable)
116+
if is_stable(version):
117+
anchor = soup.find('a', attrs={"href": f"{norm_pkg_name}/"})
118+
spans = anchor.find_all('span')
119+
spans[1].string = norm_version
120+
with open(INDEX_FILE, 'wb') as index:
121+
index.write(soup.prettify("utf-8"))
83122

84123
# Change the package page
85124
index_file = os.path.join(norm_pkg_name, INDEX_FILE)
86125
with open(index_file) as html_file:
87126
soup = BeautifulSoup(html_file, "html.parser")
127+
128+
# Extract the URL from the onclick attribute
129+
button = soup.find('button', id='repoHomepage')
130+
if button:
131+
link = button.get("onclick")[len("location.href='"):-1]
132+
else:
133+
raise Exception("Homepage URL not found")
88134

89135
# Create a new anchor element for our new version
90-
last_anchor = soup.find_all('a')[-1] # Copy the last anchor element
91-
new_anchor = copy.copy(last_anchor)
92-
new_anchor['href'] = "{}#egg={}-{}".format(link, norm_pkg_name, version)
136+
original_div = soup.find('section', class_='versions').findAll('div')[-1]
137+
new_div = copy.copy(original_div)
138+
anchor = new_div.find('a')
139+
new_div['onclick'] = f"load_readme('{version}', scroll_to_div=true);"
140+
new_div['id'] = norm_version
141+
new_div['class'] = ""
142+
if not is_stable(version):
143+
new_div['class'] += "prerelease"
144+
else:
145+
# replace the latest main version
146+
main_version_span = soup.find('span', id='latest-main-version')
147+
main_version_span.string = norm_version
148+
anchor.string = norm_version
149+
anchor['href'] = f"git+{link}@{version}#egg={norm_pkg_name}-{norm_version}"
93150

94151
# Add it to our index
95-
last_anchor.insert_after(new_anchor)
152+
original_div.insert_after(new_div)
96153

97-
# Change the latest version
98-
soup.html.body.div.section.find_all('span')[1].contents[0].replace_with(version)
154+
# Change the latest version (if stable)
155+
if is_stable(version):
156+
soup.html.body.div.section.find_all('span')[1].contents[0].replace_with(norm_version)
99157

100158
# Save it
101159
with open(index_file, 'wb') as index:
@@ -109,13 +167,13 @@ def delete(pkg_name):
109167
norm_pkg_name = normalize(pkg_name)
110168

111169
if not package_exists(soup, norm_pkg_name):
112-
raise ValueError("Package {} seems to not exists".format(norm_pkg_name))
170+
raise ValueError(f"Package {norm_pkg_name} seems to not exists")
113171

114172
# Remove the package directory
115173
shutil.rmtree(norm_pkg_name)
116174

117175
# Find and remove the anchor corresponding to our package
118-
anchor = soup.find('a', attrs={"href": "{}/".format(norm_pkg_name)})
176+
anchor = soup.find('a', attrs={"href": f"{norm_pkg_name}/"})
119177
anchor.extract()
120178
with open(INDEX_FILE, 'wb') as index:
121179
index.write(soup.prettify("utf-8"))
@@ -131,17 +189,16 @@ def main():
131189
version=os.environ["PKG_VERSION"],
132190
author=os.environ["PKG_AUTHOR"],
133191
short_desc=os.environ["PKG_SHORT_DESC"],
134-
long_desc=os.environ["PKG_LONG_DESC"],
135192
homepage=os.environ["PKG_HOMEPAGE"],
136-
link=os.environ["PKG_LINK"],
137193
)
138194
elif action == "DELETE":
139-
delete(pkg_name=os.environ["PKG_NAME"])
195+
delete(
196+
pkg_name=os.environ["PKG_NAME"]
197+
)
140198
elif action == "UPDATE":
141199
update(
142200
pkg_name=os.environ["PKG_NAME"],
143-
version=os.environ["PKG_VERSION"],
144-
link=os.environ["PKG_LINK"],
201+
version=os.environ["PKG_VERSION"]
145202
)
146203

147204

‎.github/workflows/register.yml‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,21 @@ on:
88
required: true
99
type: string
1010
version:
11-
description: 'Version of the package'
11+
description: 'Version of the package (tag name)'
1212
required: true
1313
type: string
1414
author:
1515
description: 'Author(s) of the package'
1616
required: true
1717
type: string
1818
short_desc:
19-
description: 'A short description of the package'
20-
required: true
21-
type: string
22-
long_desc:
23-
description: 'A longer description of the package (HTML)'
19+
description: 'A short description of the package to show on the index'
2420
required: true
2521
type: string
2622
homepage:
2723
description: 'Homepage of the package (link to the github repository)'
2824
required: true
2925
type: string
30-
link:
31-
description: 'Link used for `pip`. For example, for a github-hosted package refered by the tag `v3.0.2`, it would be : git+https://github.com/huggingface/transformers@v3.0.2'
32-
required: true
33-
type: string
3426

3527
jobs:
3628
update:
@@ -52,9 +44,7 @@ jobs:
5244
PKG_VERSION: ${{ inputs.version }}
5345
PKG_AUTHOR: ${{ inputs.author }}
5446
PKG_SHORT_DESC: ${{ inputs.short_desc }}
55-
PKG_LONG_DESC: ${{ inputs.long_desc }}
5647
PKG_HOMEPAGE: ${{ inputs.homepage }}
57-
PKG_LINK: ${{ inputs.link }}
5848
run: |
5949
pip install beautifulsoup4
6050
python .github/actions.py

‎.github/workflows/update.yml‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ on:
88
required: true
99
type: string
1010
version:
11-
description: New version of the package
12-
required: true
13-
type: string
14-
link:
15-
description: 'Link used for `pip`. For example, for a github-hosted package
16-
refered by the tag `v3.0.2`, it would be : git+https://github.com/huggingface/transformers@v3.0.2'
11+
description: New version of the package (tag name)
1712
required: true
1813
type: string
1914

@@ -35,7 +30,6 @@ jobs:
3530
PKG_ACTION: UPDATE
3631
PKG_NAME: ${{ inputs.package_name }}
3732
PKG_VERSION: ${{ inputs.version }}
38-
PKG_LINK: ${{ inputs.link }}
3933
run: |
4034
pip install beautifulsoup4
4135
python .github/actions.py

‎index.html‎

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,16 @@
88
<link crossorigin="anonymous" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet"/>
99
<!-- Font -->
1010
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600&amp;display=swap" rel="stylesheet" type="text/css"/>
11-
<!-- JQuery -->
12-
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
13-
<!-- Our JS -->
14-
<script type="text/javascript" src="pypi_checker.js"></script>
1511
<!-- Favicon -->
1612
<link href="https://gist.githubusercontent.com/astariul/c09af596e802e945d3032774e10e1047/raw/f693a2e2b65966494da082887bc4be2917f615e4/random_icon.svg" rel="icon"/>
17-
<style>
18-
body { font-family: "Montserrat"; }
19-
code {
20-
font-size: 100%;
21-
display: inline-block !important;
22-
font-size: 1.3rem; }
23-
.header {
24-
margin-top: 6rem;
25-
text-align: center; }
26-
.text-header {
27-
text-transform: uppercase;
28-
font-size: 1.4rem;
29-
letter-spacing: .2rem;
30-
font-weight: 600; }
31-
.card {
32-
display: inline-block;
33-
height: auto;
34-
min-width: 32%;
35-
padding: 0.5rem 2rem;
36-
margin: 0.5rem 0.5rem;
37-
color: #555;
38-
font-size: 1.5rem;
39-
font-weight: 600;
40-
line-height: 1.5;
41-
letter-spacing: .05rem;
42-
text-decoration: none;
43-
white-space: normal;
44-
background-color: transparent;
45-
border-radius: 4px;
46-
border: 1px solid #bbb;
47-
cursor: pointer;
48-
box-sizing: border-box; }
49-
.card:hover {
50-
border-color: darkcyan;
51-
color: darkcyan; }
52-
.version {
53-
font-size: 1rem;
54-
font-style: italic;}
55-
.description {
56-
font-weight: 300; }
57-
.redalert {
58-
border-color: crimson;
59-
color: crimson; }
60-
.redalert:hover {
61-
border-color: tomato;
62-
color: tomato;
63-
}
64-
</style>
13+
<!-- Custom Styles -->
14+
<link href="static/index_styles.css" rel="stylesheet"/>
15+
<!-- JQuery -->
16+
<script src="https://code.jquery.com/jquery-latest.min.js">
17+
</script>
18+
<!-- Our JS -->
19+
<script src="static/pypi_checker.js" type="text/javascript">
20+
</script>
6521
<title>
6622
Pypi - YourCompany
6723
</title>
@@ -128,7 +84,7 @@ <h6 class="text-header">
12884
<span>
12985
</span>
13086
<span class="version">
131-
3.0
87+
3.0.0
13288
</span>
13389
<br/>
13490
<span class="description">
@@ -137,19 +93,19 @@ <h6 class="text-header">
13793
</a>
13894
</div>
13995
<script>
140-
$(document).ready(function(){
96+
$(document).ready(function(){
14197
for (let lnk of document.getElementsByTagName('a')) {
142-
var content = lnk.textContent.trim().replace(/\s\s+/g, ' ').split(" ");
143-
var pkg_name = content[0];
144-
var pkg_vers = content[1];
98+
var content = lnk.textContent.trim().replace(/\s\s+/g, ' ').split(" ");
99+
var pkg_name = content[0];
100+
var pkg_vers = content[1];
145101

146-
function mark_red() {
147-
lnk.classList.add("redalert");
148-
}
102+
function mark_red() {
103+
lnk.classList.add("redalert");
104+
}
149105

150-
check_supply_chain_attack(pkg_name, pkg_vers, mark_red);
106+
check_supply_chain_attack(pkg_name, pkg_vers, mark_red);
151107
}
152-
});
108+
});
153109
</script>
154110
</body>
155-
</html>
111+
</html>

0 commit comments

Comments
 (0)