Improve CI script

This commit is contained in:
NateScarlet 2019-03-09 18:56:23 +08:00
parent 350ceab783
commit 56f9e396c8
No known key found for this signature in database
GPG Key ID: 5C242793B070309C
2 changed files with 39 additions and 10 deletions

View File

@ -1,7 +1,10 @@
language: python language: python
python: python:
- '3.6' - '3.6'
before_install:
- eval "$(gimme stable)"
install: install:
- go get github.com/github/hub && hub --version
- pip install -r requirements.txt - pip install -r requirements.txt
cache: cache:
- pip - pip
@ -11,7 +14,7 @@ before_script:
- git checkout master - git checkout master
script: script:
- pytest - pytest
- ./update.py - ./update.py --release
branches: branches:
only: only:
- master - master

View File

@ -4,8 +4,12 @@
import argparse import argparse
import json import json
import os import os
import re
import subprocess import subprocess
from datetime import datetime, timedelta, tzinfo from datetime import datetime, timedelta, tzinfo
from itertools import chain
from tempfile import mkstemp
from zipfile import ZipFile
from tqdm import tqdm from tqdm import tqdm
@ -54,20 +58,28 @@ def update_data(year: int) -> str:
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--all', action='store_true') parser.add_argument('--all', action='store_true',
help='Update all years since 2007, default is this year and next year')
parser.add_argument('--release', action='store_true',
help='create new release if repository data is not up to date')
args = parser.parse_args() args = parser.parse_args()
now = datetime.now(ChinaTimezone()) now = datetime.now(ChinaTimezone())
is_release = args.release
filenames = [] filenames = []
progress = tqdm(range(2014 if args.all else now.year, now.year + 2)) progress = tqdm(range(2007 if args.all else now.year, now.year + 2))
for i in progress: for i in progress:
progress.set_description(f'Updating {i} data') progress.set_description(f'Updating {i} data')
filename = update_data(i) filename = update_data(i)
filenames.append(filename) filenames.append(filename)
subprocess.run(['git', 'add', *filenames], check=True) if not is_release:
diff = subprocess.run(['git', 'diff', '--stat', '--cached'], print('Updated repository data, skip release since not specified `--release`')
return
subprocess.run(['hub', 'add', *filenames], check=True)
diff = subprocess.run(['hub', 'diff', '--stat', '--cached', *filenames],
check=True, check=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
encoding='utf-8').stdout encoding='utf-8').stdout
@ -75,11 +87,25 @@ def main():
print('Already up to date.') print('Already up to date.')
return return
subprocess.run( temp_note_fd, temp_note_name = mkstemp()
['git', 'commit', '-m', 'Update data [skip ci]\n\n' + diff], check=True) with open(temp_note_fd, 'w', encoding='utf-8') as f:
subprocess.run(['git', 'tag', now.strftime('%Y.%m.%d')], check=True) f.write(now.strftime('%Y.%m.%d') + '\n\n```diff' + diff + '\n```')
subprocess.run(['git', 'push'], check=True) temp_zip_fd, temp_zip_name = mkstemp()
subprocess.run(['git', 'push', '--tags'], check=True) pack_data(temp_zip_fd)
subprocess.run(['hub', 'release', 'create', '-F', temp_note_name,
'-a', temp_zip_name + '#JSON数据打包',
now.strftime('%Y.%m.%d')], check=True)
os.unlink(temp_note_fd)
os.unlink(temp_note_name)
def pack_data(file):
zip_file = ZipFile(file, 'w')
for i in os.listdir(__dirname__):
if not re.match(r'\d+\.json', i):
continue
zip_file.write(_file_path(i), i)
if __name__ == '__main__': if __name__ == '__main__':