Last active
March 24, 2017 09:12
-
-
Save snoack/51cf7448173a72bce4cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
import sys | |
import re | |
import datetime | |
import shutil | |
import urllib.parse | |
import urllib.request | |
import html.parser | |
HGWEB_URL = "https://hg.adblockplus.org/" | |
CURRENT_YEAR = datetime.datetime.now().year | |
def process_repo(url): | |
repo = url.rstrip('/').split('/')[-1] | |
if repo in { | |
# deprecated | |
'adblockpluskmeleon', | |
'adblockplusopera', | |
'chimerascript', | |
'jsdoc-toolkit', | |
'jshydra', 'www', | |
# headers are copied from libadblockplus, no need to update seperately | |
'libadblockplus-binaries', | |
# huge and only contains autogenerated builds | |
'downloads', | |
}: | |
return | |
try: | |
subprocess.check_call(["hg", "clone", url, repo]) | |
if repo == 'adblockbrowser': | |
# adblockbrowser is a FF fork with its own changes in a seperate branch | |
subprocess.check_call(["hg", "up", "--rev", "adblockbrowser", '--repository', repo]) | |
else: | |
# switch to 'master' bookmark if it exists | |
subprocess.call(["hg", "up", "--rev", "master", '--repository', repo]) | |
for dirpath, dirnames, filenames in os.walk(repo): | |
if dirpath == repo: | |
dirnames.remove('.hg') | |
for filename in filenames: | |
with open(os.path.join(dirpath, filename), "r+", encoding="utf-8", newline="") as file: | |
try: | |
text = file.read() | |
except UnicodeDecodeError: | |
continue | |
text = re.sub( | |
r"(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh", | |
r"\1-%s eyeo GmbH" % CURRENT_YEAR, text, 0, re.I | |
) | |
file.seek(0) | |
file.write(text) | |
file.truncate() | |
try: | |
subprocess.check_call(['hg', 'commit', '-m', 'Noissue - Updated copyright year', '--repository', repo]) | |
except subprocess.CalledProcessError as e: | |
if e.returncode == 1: # no changes | |
return | |
raise | |
# Push changes, or save patch if access denied | |
if subprocess.call(['hg', 'push', '--repository', repo, 'ssh://hg@hg.adblockplus.org/' + repo]) != 0: | |
with open(repo + '.patch', 'wb') as file: | |
subprocess.check_call(['hg', 'export', '--repository', repo], stdout=file) | |
finally: | |
shutil.rmtree(repo, ignore_errors=True) | |
class Parser(html.parser.HTMLParser): | |
def handle_starttag(self, tag, attrs): | |
if tag == "a": | |
attrs = dict(attrs) | |
if "list" in attrs.get("class", "").split(): | |
process_repo(urllib.parse.urljoin(HGWEB_URL, attrs["href"])) | |
def process_all(): | |
parser = Parser() | |
with urllib.request.urlopen(HGWEB_URL) as response: | |
parser.feed(response.read().decode('utf-8')) | |
parser.close() | |
if __name__ == "__main__": | |
process_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment