]>
git.proxmox.com Git - mirror_linux-firmware.git/blob - contrib/process_linux_firmware.py
14 from datetime
import date
17 URL
= "https://lore.kernel.org/linux-firmware/new.atom"
20 class ContentType(Enum
):
28 "are available in the Git repository at": ContentType
.PULL_REQUEST
,
29 "diff --git": ContentType
.PATCH
,
30 "Signed-off-by:": ContentType
.PATCH
,
34 def classify_content(content
):
35 # load content into the email library
36 msg
= email
.message_from_string(content
)
39 subject
= msg
["Subject"]
41 return ContentType
.REPLY
42 if "PATCH" in subject
:
43 return ContentType
.PATCH
45 for part
in msg
.walk():
46 if part
.get_content_type() == "text/plain":
48 body
= part
.get_payload(decode
=True).decode("utf-8")
49 for key
in content_types
.keys():
51 return content_types
[key
]
53 except UnicodeDecodeError as e
:
54 logging
.warning("Failed to decode email: %s, treating as SPAM" % e
)
56 return ContentType
.SPAM
60 with urllib
.request
.urlopen(url
) as response
:
61 return response
.read().decode("utf-8")
65 logging
.debug("Running {}".format(cmd
))
66 output
= subprocess
.check_output(cmd
, stderr
=subprocess
.STDOUT
)
70 def reply_email(content
, branch
):
71 if "SMTP_USER" in os
.environ
:
72 user
= os
.environ
["SMTP_USER"]
73 if "SMTP_PASS" in os
.environ
:
74 password
= os
.environ
["SMTP_PASS"]
75 if "SMTP_SERVER" in os
.environ
:
76 server
= os
.environ
["SMTP_SERVER"]
77 if "SMTP_PORT" in os
.environ
:
78 port
= os
.environ
["SMTP_PORT"]
79 if not user
or not password
or not server
or not port
:
80 logging
.debug("Missing SMTP configuration, not sending email")
83 reply
= email
.message
.EmailMessage()
85 orig
= email
.message_from_string(content
)
86 reply
["To"] = ", ".join(
87 email
.utils
.formataddr(t
)
88 for t
in email
.utils
.getaddresses(
89 orig
.get_all("from", []) + orig
.get_all("to", []) + orig
.get_all("cc", [])
93 reply
["From"] = "linux-firmware@kernel.org"
94 reply
["Subject"] = "Re: {}".format(orig
["Subject"])
95 reply
["In-Reply-To"] = orig
["Message-Id"]
96 reply
["References"] = orig
["Message-Id"]
97 reply
["Thread-Topic"] = orig
["Thread-Topic"]
98 reply
["Thread-Index"] = orig
["Thread-Index"]
101 "Your request has been forwarded by the Linux Firmware Kernel robot.\n"
102 "Please follow up at https://gitlab.com/kernel-firmware/linux-firmware/-/merge_requests to ensure it gets merged\n"
103 "Your request is '{}'".format(branch
)
105 reply
.set_content(content
)
107 mailserver
= smtplib
.SMTP(server
, port
)
109 mailserver
.starttls()
111 mailserver
.login(user
, password
)
112 mailserver
.sendmail(reply
["From"], reply
["To"], reply
.as_string())
116 def create_pr(remote
, branch
):
124 "merge_request.create",
126 "merge_request.remove_source_branch",
128 "merge_request.target=main",
130 "merge_request.title={}".format(branch
),
135 def refresh_branch():
136 quiet_cmd(["git", "checkout", "main"])
137 quiet_cmd(["git", "pull"])
140 def delete_branch(branch
):
141 quiet_cmd(["git", "checkout", "main"])
142 quiet_cmd(["git", "branch", "-D", branch
])
145 def process_pr(mbox
, num
, remote
):
146 branch
= "robot/pr-{}-{}".format(num
, int(time
.time()))
148 cmd
= ["b4", "--debug", "pr", "-b", branch
, "-"]
149 logging
.debug("Running {}".format(cmd
))
150 p
= subprocess
.Popen(
151 cmd
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
153 stdout
, stderr
= p
.communicate(mbox
.encode("utf-8"))
154 for line
in stdout
.splitlines():
155 logging
.debug(line
.decode("utf-8"))
156 for line
in stderr
.splitlines():
157 logging
.debug(line
.decode("utf-8"))
159 # determine if it worked (we can't tell unfortunately by return code)
160 cmd
= ["git", "branch", "--list", branch
]
161 logging
.debug("Running {}".format(cmd
))
162 result
= subprocess
.check_output(cmd
)
165 for line
in result
.splitlines():
166 logging
.debug(line
.decode("utf-8"))
167 logging
.info("Forwarding PR for {}".format(branch
))
169 create_pr(remote
, branch
)
170 delete_branch(branch
)
175 def process_patch(mbox
, num
, remote
):
176 # create a new branch for the patch
177 branch
= "robot/patch-{}-{}".format(num
, int(time
.time()))
178 cmd
= ["git", "checkout", "-b", branch
]
183 logging
.debug("Running {}".format(cmd
))
184 p
= subprocess
.Popen(
185 cmd
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
187 stdout
, stderr
= p
.communicate(mbox
.encode("utf-8"))
188 for line
in stdout
.splitlines():
189 logging
.debug(line
.decode("utf-8"))
190 for line
in stderr
.splitlines():
191 logging
.debug(line
.decode("utf-8"))
192 if p
.returncode
!= 0:
193 quiet_cmd(["git", "am", "--abort"])
195 logging
.info("Opening PR for {}".format(branch
))
197 create_pr(remote
, branch
)
199 delete_branch(branch
)
200 if p
.returncode
== 0:
205 def update_database(conn
, url
):
209 """CREATE TABLE IF NOT EXISTS firmware (url text, processed integer default 0, spam integer default 0)"""
213 if os
.path
.exists(url
):
214 with
open(url
, "r") as f
:
218 logging
.info("Fetching {}".format(url
))
219 atom
= fetch_url(url
)
221 # Parse the atom and extract the URLs
222 feed
= feedparser
.parse(atom
)
224 # Insert the URLs into the database (oldest first)
225 feed
["entries"].reverse()
226 for entry
in feed
["entries"]:
227 c
.execute("SELECT url FROM firmware WHERE url = ?", (entry
.link
,))
230 c
.execute("INSERT INTO firmware VALUES (?, ?, ?)", (entry
.link
, 0, 0))
232 # Commit the changes and close the connection
236 def process_database(conn
, remote
):
239 # get all unprocessed urls that aren't spam
240 c
.execute("SELECT url FROM firmware WHERE processed = 0 AND spam = 0")
247 logging
.info("No new entries")
252 # loop over all unprocessed urls
255 msg
= "Processing ({}%)".format(round(num
/ len(rows
) * 100))
256 print(msg
, end
="\r", flush
=True)
258 url
= "{}raw".format(row
[0])
259 logging
.debug("Processing {}".format(url
))
260 mbox
= fetch_url(url
)
261 classification
= classify_content(mbox
)
263 if classification
== ContentType
.PATCH
:
264 logging
.debug("Processing patch ({})".format(row
[0]))
265 branch
= process_patch(mbox
, num
, remote
)
267 if classification
== ContentType
.PULL_REQUEST
:
268 logging
.debug("Processing PR ({})".format(row
[0]))
269 branch
= process_pr(mbox
, num
, remote
)
271 if classification
== ContentType
.SPAM
:
272 logging
.debug("Marking spam ({})".format(row
[0]))
273 c
.execute("UPDATE firmware SET spam = 1 WHERE url = ?", (row
[0],))
275 if classification
== ContentType
.REPLY
:
276 logging
.debug("Ignoring reply ({})".format(row
[0]))
278 c
.execute("UPDATE firmware SET processed = 1 WHERE url = ?", (row
[0],))
280 print(" " * len(msg
), end
="\r", flush
=True)
287 reply_email(mbox
, branch
)
289 logging
.info("Finished processing {} new entries".format(len(rows
)))
292 if __name__
== "__main__":
293 parser
= argparse
.ArgumentParser(description
="Process linux-firmware mailing list")
294 parser
.add_argument("--url", default
=URL
, help="URL to get ATOM feed from")
297 default
=os
.path
.join("contrib", "linux_firmware.db"),
298 help="sqlite database to store entries in",
300 parser
.add_argument("--dry", action
="store_true", help="Don't open pull requests")
302 "--debug", action
="store_true", help="Enable debug logging to console"
304 parser
.add_argument("--remote", default
="origin", help="Remote to push to")
306 "--refresh-cycle", default
=0, help="How frequently to run (in minutes)"
308 args
= parser
.parse_args()
310 if not os
.path
.exists("WHENCE"):
312 "Please run this script from the root of the linux-firmware repository"
318 "{prefix}-{date}.{suffix}".format(
319 prefix
="linux_firmware", suffix
="txt", date
=date
.today()
323 format
="%(asctime)s %(levelname)s:\t%(message)s",
329 # set a format which is simpler for console use
330 console
= logging
.StreamHandler()
332 console
.setLevel(logging
.DEBUG
)
334 console
.setLevel(logging
.INFO
)
335 formatter
= logging
.Formatter("%(asctime)s : %(levelname)s : %(message)s")
336 console
.setFormatter(formatter
)
337 logging
.getLogger("").addHandler(console
)
340 conn
= sqlite3
.connect(args
.database
)
341 # update the database
342 update_database(conn
, args
.url
)
349 # process the database
350 process_database(conn
, remote
)
354 if args
.refresh_cycle
:
355 logging
.info("Sleeping for {} minutes".format(args
.refresh_cycle
))
356 time
.sleep(int(args
.refresh_cycle
) * 60)