11import os
2- import json
32import copy
43import re
54import shutil
1110TEMPLATE_FILE = "pkg_template.html"
1211YAML_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
1528def 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+
2042def 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
0 commit comments