99
1010INDEX_FILE = "index.html"
1111TEMPLATE_FILE = "pkg_template.html"
12+ YAML_ACTION_FILES = [".github/workflows/delete.yml" , ".github/workflows/update.yml" ]
1213
1314
1415def normalize (name ):
1516 """ From PEP503 : https://www.python.org/dev/peps/pep-0503/ """
1617 return re .sub (r"[-_.]+" , "-" , name ).lower ()
1718
1819
19- def parse_issue (issue_ctx ):
20- arguments = {}
21-
22- parts = issue_ctx ['body' ].split ('- **' )[1 :] # Ignore the first one : it's the title
23- for text_arg in parts :
24- arg_name , arg_value = text_arg .split (':**' )
25- arg_name , arg_value = arg_name .strip (), arg_value .strip ()
26-
27- if arg_name == "Long description" :
28- # Special case, where we have more than 1 line : it contain HTML code
29- arg_value = arg_value .split ('```' )[1 ] if '```' in arg_value else arg_value .split ('`' )[1 ]
30-
31- code_lang = arg_value .split ('\n ' )[0 ].strip ()
32- if code_lang != 'html' :
33- raise ValueError ("The {} argument should contain a HTML code section. But it contain {} code." .format (arg_name , code_lang ))
34-
35- arg_value = "\n " .join (arg_value .split ('\n ' )[1 :])
36- else :
37- if "\n " in arg_value :
38- raise ValueError ("The {} argument should be a single line. Current value : {}" .format (arg_name , arg_value ))
39-
40- arguments [arg_name .lower ()] = arg_value
41- return arguments
42-
43-
44- def print_args (args ):
45- print ("\n --- Arguments detected from issue ---\n " )
46- for arg_name , arg_value in args .items ():
47- print ("\t {} : {}" .format (arg_name , arg_value ))
48- print ("\n " )
49-
50-
51- def check_args (args , must_have ):
52- for name in must_have :
53- if name not in args :
54- raise ValueError ("Couldn't find argument {}" .format (name ))
55- if args [name ].strip () == "" :
56- raise ValueError ("Argument {} is empty. Please specify it" .format (name ))
57-
58-
5920def package_exists (soup , package_name ):
6021 package_ref = package_name + "/"
6122 for anchor in soup .find_all ('a' ):
@@ -64,25 +25,23 @@ def package_exists(soup, package_name):
6425 return False
6526
6627
67- def register (issue_ctx ):
68- args = parse_issue (issue_ctx )
69- print_args (args )
70- check_args (args , ['package name' , 'version' , 'author' , 'short description' , 'long description' , 'homepage' , 'link' ])
28+ def register (pkg_name , version , author , short_desc , long_desc , homepage , link ):
29+ # Read our index first
7130 with open (INDEX_FILE ) as html_file :
7231 soup = BeautifulSoup (html_file , "html.parser" )
73- n_package_name = normalize (args [ 'package name' ] )
32+ norm_pkg_name = normalize (pkg_name )
7433
75- if package_exists (soup , n_package_name ):
76- raise ValueError ("Package {} seems to already exists" .format (n_package_name ))
34+ if package_exists (soup , norm_pkg_name ):
35+ raise ValueError ("Package {} seems to already exists" .format (norm_pkg_name ))
7736
7837 # Create a new anchor element for our new package
7938 last_anchor = soup .find_all ('a' )[- 1 ] # Copy the last anchor element
8039 new_anchor = copy .copy (last_anchor )
81- new_anchor ['href' ] = "{}/" .format (n_package_name )
82- new_anchor .contents [0 ].replace_with (args [ 'package name' ] )
40+ new_anchor ['href' ] = "{}/" .format (norm_pkg_name )
41+ new_anchor .contents [0 ].replace_with (pkg_name )
8342 spans = new_anchor .find_all ('span' )
84- spans [1 ].string = args [ ' version' ] # First span contain the version
85- spans [2 ].string = args [ 'short description' ] # Second span contain the short description
43+ spans [1 ].string = version # First span contain the version
44+ spans [2 ].string = short_desc # Second span contain the short description
8645
8746 # Add it to our index and save it
8847 last_anchor .insert_after (new_anchor )
@@ -93,93 +52,97 @@ def register(issue_ctx):
9352 with open (TEMPLATE_FILE ) as temp_file :
9453 template = temp_file .read ()
9554
96- template = template .replace ("_package_name" , args [ 'package name' ] )
97- template = template .replace ("_version" , args [ ' version' ] )
98- template = template .replace ("_link" , "{}#egg={}-{}" .format (args [ ' link' ], n_package_name , args [ ' version' ] ))
99- template = template .replace ("_homepage" , args [ ' homepage' ] )
100- template = template .replace ("_author" , args [ ' author' ] )
101- template = template .replace ("_long_description" , args [ 'long description' ] )
55+ 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 ))
58+ template = template .replace ("_homepage" , homepage )
59+ template = template .replace ("_author" , author )
60+ template = template .replace ("_long_description" , long_desc )
10261
103- os .mkdir (n_package_name )
104- package_index = os .path .join (n_package_name , INDEX_FILE )
62+ os .mkdir (norm_pkg_name )
63+ package_index = os .path .join (norm_pkg_name , INDEX_FILE )
10564 with open (package_index , "w" ) as f :
10665 f .write (template )
10766
10867
109- def update (issue_ctx ):
110- args = parse_issue (issue_ctx )
111- print_args (args )
112- check_args (args , ['package name' , 'new version' , 'link for the new version' ])
68+ def update (pkg_name , version , link ):
69+ # Read our index first
11370 with open (INDEX_FILE ) as html_file :
11471 soup = BeautifulSoup (html_file , "html.parser" )
115- n_package_name = normalize (args [ 'package name' ] )
72+ norm_pkg_name = normalize (pkg_name )
11673
117- if not package_exists (soup , n_package_name ):
118- raise ValueError ("Package {} seems to not exists" .format (n_package_name ))
74+ if not package_exists (soup , norm_pkg_name ):
75+ raise ValueError ("Package {} seems to not exists" .format (norm_pkg_name ))
11976
12077 # Change the version in the main page
121- anchor = soup .find ('a' , attrs = {"href" : "{}/" .format (n_package_name )})
78+ anchor = soup .find ('a' , attrs = {"href" : "{}/" .format (norm_pkg_name )})
12279 spans = anchor .find_all ('span' )
123- spans [1 ].string = args [ 'new version' ]
80+ spans [1 ].string = version
12481 with open (INDEX_FILE , 'wb' ) as index :
12582 index .write (soup .prettify ("utf-8" ))
12683
12784 # Change the package page
128- index_file = os .path .join (n_package_name , INDEX_FILE )
85+ index_file = os .path .join (norm_pkg_name , INDEX_FILE )
12986 with open (index_file ) as html_file :
13087 soup = BeautifulSoup (html_file , "html.parser" )
13188
13289 # Create a new anchor element for our new version
13390 last_anchor = soup .find_all ('a' )[- 1 ] # Copy the last anchor element
13491 new_anchor = copy .copy (last_anchor )
135- new_anchor ['href' ] = "{}#egg={}-{}" .format (args [ ' link for the new version' ], n_package_name , args [ 'new version' ] )
92+ new_anchor ['href' ] = "{}#egg={}-{}" .format (link , norm_pkg_name , version )
13693
13794 # Add it to our index
13895 last_anchor .insert_after (new_anchor )
13996
14097 # Change the latest version
141- soup .html .body .div .section .find_all ('span' )[1 ].contents [0 ].replace_with (args [ 'new version' ] )
98+ soup .html .body .div .section .find_all ('span' )[1 ].contents [0 ].replace_with (version )
14299
143100 # Save it
144101 with open (index_file , 'wb' ) as index :
145102 index .write (soup .prettify ("utf-8" ))
146103
147104
148- def delete (issue_ctx ):
149- args = parse_issue (issue_ctx )
150- print_args (args )
151- check_args (args , ['package name' ])
105+ def delete (pkg_name ):
106+ # Read our index first
152107 with open (INDEX_FILE ) as html_file :
153108 soup = BeautifulSoup (html_file , "html.parser" )
154- n_package_name = normalize (args [ 'package name' ] )
109+ norm_pkg_name = normalize (pkg_name )
155110
156- if not package_exists (soup , n_package_name ):
157- raise ValueError ("Package {} seems to not exists" .format (n_package_name ))
111+ if not package_exists (soup , norm_pkg_name ):
112+ raise ValueError ("Package {} seems to not exists" .format (norm_pkg_name ))
158113
159114 # Remove the package directory
160- shutil .rmtree (n_package_name )
115+ shutil .rmtree (norm_pkg_name )
161116
162117 # Find and remove the anchor corresponding to our package
163- anchor = soup .find ('a' , attrs = {"href" : "{}/" .format (n_package_name )})
118+ anchor = soup .find ('a' , attrs = {"href" : "{}/" .format (norm_pkg_name )})
164119 anchor .extract ()
165120 with open (INDEX_FILE , 'wb' ) as index :
166121 index .write (soup .prettify ("utf-8" ))
167122
168123
169124def main ():
170- # Get the context from the environment variable
171- context = json .loads (os .environ ['GITHUB_CONTEXT' ])
172- issue_ctx = context ['event' ]['issue' ]
173- title = issue_ctx ['title' ]
174-
175- if title .startswith ("🟢" ):
176- register (issue_ctx )
177-
178- if title .startswith ("🔵" ):
179- update (issue_ctx )
180-
181- if title .startswith ("🔴" ):
182- delete (issue_ctx )
125+ # Call the right method, with the right arguments
126+ action = os .environ ["PKG_ACTION" ]
127+
128+ if action == "REGISTER" :
129+ register (
130+ pkg_name = os .environ ["PKG_NAME" ],
131+ version = os .environ ["PKG_VERSION" ],
132+ author = os .environ ["PKG_AUTHOR" ],
133+ short_desc = os .environ ["PKG_SHORT_DESC" ],
134+ long_desc = os .environ ["PKG_LONG_DESC" ],
135+ homepage = os .environ ["PKG_HOMEPAGE" ],
136+ link = os .environ ["PKG_LINK" ],
137+ )
138+ elif action == "DELETE" :
139+ delete (pkg_name = os .environ ["PKG_NAME" ])
140+ elif action == "UPDATE" :
141+ update (
142+ pkg_name = os .environ ["PKG_NAME" ],
143+ version = os .environ ["PKG_VERSION" ],
144+ link = os .environ ["PKG_LINK" ],
145+ )
183146
184147
185148if __name__ == "__main__" :
0 commit comments