1
+ """
2
+ This script uploads XML files to GitHub and GitLab repositories.
3
+ """
4
+
1
5
import os
2
- import yaml
3
- import requests
4
6
import base64
5
7
import logging
8
+ import requests
9
+ import yaml
6
10
7
11
# Set up logging
8
12
logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(levelname)s - %(message)s' )
9
13
10
- # Function to load configuration from YAML or environment variables
11
14
def load_config (config_path = 'config.yaml' ):
12
- logging .info (f"Loading configuration from { config_path } or environment variables..." )
15
+ """
16
+ Load configuration from YAML or environment variables.
17
+ """
18
+ logging .info ("Loading configuration from %s or environment variables..." , config_path )
13
19
if os .path .exists (config_path ):
14
- with open (config_path , 'r' ) as file :
20
+ with open (config_path , 'r' , encoding = 'utf-8' ) as file :
15
21
config = yaml .safe_load (file )
16
22
else :
17
- logging .warning (f "Configuration file { config_path } not found. Falling back to environment variables." )
23
+ logging .warning ("Configuration file %s not found. Falling back to environment variables." , config_path )
18
24
config = {}
19
25
20
26
config ['github_token' ] = config .get ('github_token' , os .getenv ('GITHUB_TOKEN' ))
@@ -26,8 +32,10 @@ def load_config(config_path='config.yaml'):
26
32
27
33
return config
28
34
29
- # Function to upload file to GitHub
30
35
def upload_to_github (file_path , config ):
36
+ """
37
+ Upload file to GitHub.
38
+ """
31
39
logging .info ("Uploading to GitHub..." )
32
40
repo_name = config ['github_repo' ]
33
41
token = config ['github_token' ]
@@ -43,7 +51,7 @@ def upload_to_github(file_path, config):
43
51
content = base64 .b64encode (file .read ()).decode ('utf-8' )
44
52
45
53
# Check if the file exists in the repository
46
- response = requests .get (url , headers = headers )
54
+ response = requests .get (url , headers = headers , timeout = 10 )
47
55
if response .status_code == 200 :
48
56
# File exists, retrieve its SHA
49
57
sha = response .json ()['sha' ]
@@ -54,7 +62,7 @@ def upload_to_github(file_path, config):
54
62
'branch' : 'main'
55
63
}
56
64
method = requests .put
57
- logging .info (f "File { file_name } exists in GitHub repo, updating it." )
65
+ logging .info ("File %s exists in GitHub repo, updating it." , file_name )
58
66
elif response .status_code == 404 :
59
67
# File does not exist, create it
60
68
data = {
@@ -63,22 +71,24 @@ def upload_to_github(file_path, config):
63
71
'branch' : 'main'
64
72
}
65
73
method = requests .put
66
- logging .info (f "File { file_name } does not exist in GitHub repo, creating it." )
74
+ logging .info ("File %s does not exist in GitHub repo, creating it." , file_name )
67
75
else :
68
- logging .error (f "GitHub file check failed: { response .text } " )
76
+ logging .error ("GitHub file check failed: %s" , response .text )
69
77
raise Exception (f"GitHub file check failed: { response .text } " )
70
78
71
79
# Upload or update the file
72
- response = method (url , json = data , headers = headers )
80
+ response = method (url , json = data , headers = headers , timeout = 10 )
73
81
if response .status_code in (200 , 201 ):
74
82
download_url = response .json ()['content' ]['download_url' ]
75
83
return download_url
76
84
else :
77
- logging .error (f "GitHub upload failed: { response .text } " )
85
+ logging .error ("GitHub upload failed: %s" , response .text )
78
86
raise Exception (f"GitHub upload failed: { response .text } " )
79
87
80
- # Function to upload file to GitLab
81
88
def upload_to_gitlab (file_path , config ):
89
+ """
90
+ Upload file to GitLab.
91
+ """
82
92
logging .info ("Uploading to GitLab..." )
83
93
repo_name = config ['gitlab_repo' ]
84
94
token = config ['gitlab_token' ]
@@ -88,7 +98,7 @@ def upload_to_gitlab(file_path, config):
88
98
file_name = os .path .basename (file_path )
89
99
url = f'https://gitlab.com/api/v4/projects/{ repo_name } /repository/files/{ file_name } '
90
100
91
- with open (file_path , 'r' ) as file :
101
+ with open (file_path , 'r' , encoding = 'utf-8' ) as file :
92
102
content = file .read ()
93
103
94
104
data = {
@@ -97,41 +107,43 @@ def upload_to_gitlab(file_path, config):
97
107
'commit_message' : 'Add uglyfeed.xml'
98
108
}
99
109
100
- response = requests .post (url , json = data , headers = headers )
110
+ response = requests .post (url , json = data , headers = headers , timeout = 10 )
101
111
if response .status_code == 201 :
102
112
download_url = f'https://gitlab.com/{ repo_name } /-/raw/main/{ file_name } '
103
113
return download_url
104
114
elif response .status_code == 400 and 'already exists' in response .text :
105
115
# Update file if it already exists
106
116
logging .info ("File already exists on GitLab, attempting to update..." )
107
- response = requests .put (url , json = data , headers = headers )
117
+ response = requests .put (url , json = data , headers = headers , timeout = 10 )
108
118
if response .status_code == 200 :
109
119
download_url = f'https://gitlab.com/{ repo_name } /-/raw/main/{ file_name } '
110
120
return download_url
111
121
else :
112
- logging .error (f "GitLab update failed: { response .text } " )
122
+ logging .error ("GitLab update failed: %s" , response .text )
113
123
raise Exception (f"GitLab update failed: { response .text } " )
114
124
else :
115
- logging .error (f "GitLab upload failed: { response .text } " )
125
+ logging .error ("GitLab upload failed: %s" , response .text )
116
126
raise Exception (f"GitLab upload failed: { response .text } " )
117
127
118
- # Main function to deploy XML file
119
128
def deploy_xml (file_path , config ):
129
+ """
130
+ Deploy XML file to GitHub and GitLab based on the configuration.
131
+ """
120
132
urls = {}
121
133
122
134
if config .get ('enable_github' , False ):
123
135
try :
124
136
github_url = upload_to_github (file_path , config )
125
137
urls ['github' ] = github_url
126
138
except Exception as e :
127
- logging .error (f "GitHub upload error: { e } " )
139
+ logging .error ("GitHub upload error: %s" , e )
128
140
129
141
if config .get ('enable_gitlab' , False ):
130
142
try :
131
143
gitlab_url = upload_to_gitlab (file_path , config )
132
144
urls ['gitlab' ] = gitlab_url
133
145
except Exception as e :
134
- logging .error (f "GitLab upload error: { e } " )
146
+ logging .error ("GitLab upload error: %s" , e )
135
147
136
148
return urls
137
149
@@ -140,10 +152,10 @@ def deploy_xml(file_path, config):
140
152
config = load_config ()
141
153
142
154
# File to deploy
143
- xml_file_path = 'uglyfeeds/uglyfeed.xml'
155
+ XML_FILE_PATH = 'uglyfeeds/uglyfeed.xml'
144
156
145
157
# Deploy the XML file
146
- urls = deploy_xml (xml_file_path , config )
158
+ urls = deploy_xml (XML_FILE_PATH , config )
147
159
148
160
# Print the URLs
149
161
if urls :
0 commit comments