Skip to content

Commit

Permalink
Add cpython-sync script
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 27, 2023
1 parent 05d7fef commit c35776b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions jaraco/develop/cpython-sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Sync files from an upstream CPython release.
"""

import json
import pathlib
import subprocess
import re

import autocommand
import packaging.version
from requests_toolbelt import sessions


gh_content = sessions.BaseUrlSession(
'https://raw.githubusercontent.com/python/cpython/'
)
gh_api = sessions.BaseUrlSession('https://api.github.com/repos/python/cpython/')


def load_file_map():
text = pathlib.Path('file map.json').read_text(encoding='utf-8')
clean = re.sub('^#.*', '', text, re.MULTILINE)
return json.loads(clean)


class Version(packaging.version.Version):
@property
def is_stable(self):
"""
Include release candidates in stable.
"""
return not self.is_prerelease or self.is_rc

@property
def is_rc(self):
return self.pre[1:] == ['rc']


def by_tag(tag):
return Version(tag['name'])


def is_stable(tag):
return not by_tag(tag).is_stable


@autocommand.autocommand(__name__)
def run(pre=False):
tags = gh_api.get('tags').json()
filtered = tags if pre else filter(is_stable, tags)
tag = max(filtered, key=by_tag)
version = tag['name']
for src, dst in load_file_map().items():
resp = gh_content.get(f'{version}/{src}')
resp.raise_for_status()
with open(dst, 'wb') as out:
out.write(resp.content)
cmd = [
'git',
'commit',
'-a',
'-m',
f'cpython-{version} rev={tag["commit"]["sha"][:12]}',
]
subprocess.run(cmd)
1 change: 1 addition & 0 deletions newsfragments/+b9d13ee1.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding script for syncing a backport (configparser, singledispatch) to a CPython branch.

0 comments on commit c35776b

Please sign in to comment.