1- #!/usr/bin/env python3
1+ #!/usr/bin/python3
22import re
33import requests
44import json
55import sys , os
66import logging
77import httplib2
88
9- from googleapiclient .discovery import build
10- from google .oauth2 import service_account
11-
129
1310class RelGooglePlaystore :
14- def __init__ (self ):
15- self .credentials_file_path = None
16- self .package_name = None
17- with open ('release.properties' , 'r' ) as fd :
18- lines = fd .readlines ()
19-
20- for line in lines :
21- if line .startswith ('google_playstore_creds_filepath' ):
22- self .credentials_file_path = line .split ("=" )[1 ].strip ()
23- elif line .startswith ('app_package_name' ):
24- self .package_name = line .split ("=" )[1 ].strip ()
25- # Create an HTTP object with a timeout
26-
27- http = httplib2 .Http (timeout = 600 )
28-
29- credentials = service_account .Credentials .from_service_account_file (self .credentials_file_path )
30- credentials .http = http
31-
32- self .service = build (serviceName = 'androidpublisher' , version = 'v3' ,
33- credentials = credentials ,
34- num_retries = 5 )
35-
36- def flight (self ):
37- edit_request = self .service .edits ().insert (packageName = self .package_name )
38- edit_response = edit_request .execute ()
39- edit_id = edit_response ['id' ]
40-
41-
4211 def create_edit_for_draft_release (self ,
4312 version_code ,
4413 version_name ,
@@ -50,21 +19,44 @@ def create_edit_for_draft_release(self,
5019 changesNotSentForReview = True ):
5120 """
5221 """
22+ from googleapiclient .discovery import build
23+ from google .oauth2 import service_account
5324 from googleapiclient .http import MediaFileUpload # Import MediaFileUpload
5425
5526 credentials_file_path = None
27+ package_name = None
28+
29+ with open ('release.properties' , 'r' ) as fd :
30+ lines = fd .readlines ()
31+
32+ for line in lines :
33+ if line .startswith ('google_playstore_creds_filepath' ):
34+ credentials_file_path = line .split ("=" )[1 ].strip ()
35+ elif line .startswith ('app_package_name' ):
36+ package_name = line .split ("=" )[1 ].strip ()
37+ # Create an HTTP object with a timeout
38+
39+ http = httplib2 .Http (timeout = timeout_seconds )
40+
41+ credentials = service_account .Credentials .from_service_account_file (credentials_file_path )
42+ credentials .http = http
43+
44+ service = build (serviceName = 'androidpublisher' , version = 'v3' ,
45+ credentials = credentials ,
46+ num_retries = 5 )
5647
5748 # return service.edits().insert(editId=release_id, body=edit_body).execute()
58- edit_request = self . service .edits ().insert (packageName = self . package_name )
49+ edit_request = service .edits ().insert (packageName = package_name )
5950 edit_response = edit_request .execute ()
6051 edit_id = edit_response ['id' ]
6152
53+
6254 # Create a media upload request
6355 media_upload = MediaFileUpload (bundle_file ,
6456 mimetype = "application/octet-stream" , resumable = True )
6557
66- bundle_response = self . service .edits ().bundles ().upload (
67- packageName = self . package_name ,
58+ bundle_response = service .edits ().bundles ().upload (
59+ packageName = package_name ,
6860 editId = edit_id ,
6961 media_body = media_upload
7062 ).execute ()
@@ -81,8 +73,8 @@ def create_edit_for_draft_release(self,
8173 'versionCodes' :[version_code ]
8274 }]
8375
84- track_request = self . service .edits ().tracks ().update (
85- packageName = self . package_name ,
76+ track_request = service .edits ().tracks ().update (
77+ packageName = package_name ,
8678 editId = edit_id ,
8779 track = track ,
8880 body = {'releases' : release_body }
@@ -92,8 +84,8 @@ def create_edit_for_draft_release(self,
9284 logging .info ("[Playstore] %s release %s created with version code %d" , status , version_name , version_code )
9385
9486 # Commit the changes to finalize the edit
95- commit_request = self . service .edits ().commit (
96- packageName = self . package_name ,
87+ commit_request = service .edits ().commit (
88+ packageName = package_name ,
9789 editId = edit_id
9890 )
9991 commit_request .execute ()
@@ -102,26 +94,6 @@ def create_edit_for_draft_release(self,
10294
10395
10496class RelGithub :
105- def __init__ (self ):
106- self .github_token = None
107-
108- with open ('release.properties' , 'r' ) as fd :
109- lines = fd .readlines ()
110-
111- for line in lines :
112- if line .startswith ('github_token' ):
113- self .github_token = line .split ("=" )[1 ].strip ()
114- break
115-
116- self .headers = {"Authorization" : "Bearer {}" .format (self .github_token ),
117- "X-GitHub-Api-Version" : "2022-11-28" ,
118- "Accept" : "application/vnd.github+json" }
119-
120-
121- def flight (self , url ):
122- response = requests .get (url , headers = self .headers )
123- response .raise_for_status ()
124-
12597 def create_edit_for_draft_release (self ,
12698 version_code ,
12799 version_name ,
@@ -134,6 +106,7 @@ def create_edit_for_draft_release(self,
134106
135107 status = True if status == 'draft' else False
136108
109+ # url = "https://api.github.com/repos/deku-messaging/Deku-SMS-Android/releases"
137110 data = {
138111 "tag_name" : str (version_code ),
139112 "name" : version_name ,
@@ -145,7 +118,21 @@ def create_edit_for_draft_release(self,
145118 }
146119 logging .info (data )
147120
148- response = requests .post (url , json = data , headers = self .headers )
121+ github_token = None
122+
123+ with open ('release.properties' , 'r' ) as fd :
124+ lines = fd .readlines ()
125+
126+ for line in lines :
127+ if line .startswith ('github_token' ):
128+ github_token = line .split ("=" )[1 ].strip ()
129+ break
130+
131+ headers = {"Authorization" : "Bearer {}" .format (github_token ),
132+ "X-GitHub-Api-Version" : "2022-11-28" ,
133+ "Accept" : "application/vnd.github+json" }
134+
135+ response = requests .post (url , json = data , headers = headers )
149136 response .raise_for_status ()
150137
151138 logging .info ("[GitHub] Create new release: %d" , response .status_code )
@@ -154,7 +141,7 @@ def create_edit_for_draft_release(self,
154141
155142 # Upload assets to a new release on GitHub.
156143 headers = {'Content-Type' : 'application/octet-stream' ,
157- "Authorization" : "Bearer {}" .format (self . github_token ),
144+ "Authorization" : "Bearer {}" .format (github_token ),
158145 "X-GitHub-Api-Version" : "2022-11-28" ,
159146 "Accept" : "application/vnd.github+json" }
160147
@@ -175,46 +162,23 @@ def create_edit_for_draft_release(self,
175162 # return json.loads(response.text)
176163
177164
178- def test_flight (url ):
179- print ("-- Beginning flight test" )
180- try :
181- # url = "https://api.github.com/repos/dekusms/DekuSMS-Android/releases"
182- print ("Github url:" , url )
183- rel_github = RelGithub ()
184- rel_github .flight (url )
185- print ("++ Github passed!" )
186-
187- except Exception as error :
188- logging .exception (error )
189- exit ("-- Github failed" )
190- else :
191- try :
192- rel_playstore = RelGooglePlaystore ()
193- rel_playstore .flight ()
194- print ("++ Playstore passed!" )
195-
196- except Exception as error :
197- logging .exception (error )
198- exit ("-- Playstore failed" )
199-
200-
201165if __name__ == "__main__" :
202166 import argparse
203167 import threading
204168
205169 parser = argparse .ArgumentParser (description = "An argument parser for Python" )
206170
207- parser .add_argument ("--version_code" , type = int , required = False , help = "The version code of the app" )
208- parser .add_argument ("--version_name" , type = str , required = False , help = "The version name of the app" )
209- parser .add_argument ("--description" , type = str , required = False , help = "The description of the app" )
210- parser .add_argument ("--branch" , type = str , required = False , help = "The branch of the app" )
211- parser .add_argument ("--track" , type = str , required = False , help = "The track of the app" )
212- parser .add_argument ("--app_bundle_file" , type = str , required = False , help = "The app bundle file" )
213- parser .add_argument ("--app_apk_file" , type = str , required = False , help = "The app APK file" )
214- parser .add_argument ("--status" , type = str , required = False , help = "The app release status" )
215- parser .add_argument ("--github_url" , type = str , required = False , help = "The github repo URL" )
171+ parser .add_argument ("--version_code" , type = int , required = True , help = "The version code of the app" )
172+ parser .add_argument ("--version_name" , type = str , required = True , help = "The version name of the app" )
173+ parser .add_argument ("--description" , type = str , required = True , help = "The description of the app" )
174+ parser .add_argument ("--branch" , type = str , required = True , help = "The branch of the app" )
175+ parser .add_argument ("--track" , type = str , required = True , help = "The track of the app" )
176+ parser .add_argument ("--app_bundle_file" , type = str , required = True , help = "The app bundle file" )
177+ parser .add_argument ("--app_apk_file" , type = str , required = True , help = "The app APK file" )
178+ parser .add_argument ("--status" , type = str , required = True , help = "The app release status" )
179+ parser .add_argument ("--github_url" , type = str , required = True , help = "The github repo URL" )
216180 parser .add_argument ("--log_level" , type = str , default = 'INFO' , required = False , help = "The level of the log" )
217- parser .add_argument ("--platform " , type = str , default = "test-flight " , required = False , help = "Platform to be released on: \
181+ parser .add_argument ("--platforms " , type = str , default = "all " , required = False , help = "Platform to be released on: \
218182 playstore, github" )
219183
220184 args = parser .parse_args ()
@@ -230,21 +194,18 @@ def test_flight(url):
230194
231195 logging .basicConfig (level = args .log_level )
232196
233- if args .platform == "test-flight" :
234- test_flight (args .github_url )
235-
236- elif args .platform == "all" :
197+ if args .platforms == "all" :
237198 thread_playstore .start ()
238199 thread_playstore .join ()
239200
240201 thread_github .start ()
241202 thread_github .join ()
242203
243- elif args .platform == "playstore" :
204+ elif args .platforms == "playstore" :
244205 thread_playstore .start ()
245206 thread_playstore .join ()
246207
247- elif args .platform == "github" :
208+ elif args .platforms == "github" :
248209 thread_github .start ()
249210 thread_github .join ()
250211
0 commit comments