|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import datetime |
| 4 | +import logging |
| 5 | + |
| 6 | +from catsup.options import g, config |
| 7 | +from catsup.utils import call, check_git, check_rsync |
| 8 | + |
| 9 | + |
| 10 | +def git(): |
| 11 | + if not check_git(): |
| 12 | + logging.error("Catsup can't find git.Please install git first.") |
| 13 | + sys.exit(1) |
| 14 | + logging.info("Deploying your blog via git") |
| 15 | + |
| 16 | + cwd = os.path.abspath(config.config.output) |
| 17 | + def _call(*args, **kwargs): |
| 18 | + return call(*args, cwd=cwd, **kwargs) |
| 19 | + |
| 20 | + if not os.path.exists(os.path.join(cwd, '.git')): |
| 21 | + _call('rm -rf *') |
| 22 | + # Hasn't setup git. |
| 23 | + _call('git init', silence=True) |
| 24 | + _call(['git', 'remote', 'add', 'origin', config.deploy.git.repo], |
| 25 | + silence=False) |
| 26 | + if config.deploy.git.branch != 'master': |
| 27 | + _call('git branch -m %s' % config.deploy.git.branch, silence=True) |
| 28 | + _call(['git', 'pull', 'origin', config.deploy.git.branch]) |
| 29 | + |
| 30 | + logging.info("Rebuild your blog..") |
| 31 | + import catsup.build |
| 32 | + catsup.build.build() |
| 33 | + |
| 34 | + # GitHub custom domain support |
| 35 | + with open(os.path.join(cwd, 'CNAME'), 'w') as f: |
| 36 | + domain = config.site.url.split('//')[-1].rstrip('/') |
| 37 | + f.write(domain) |
| 38 | + |
| 39 | + _call('git add .', silence=True) |
| 40 | + _call(['git', 'commit', |
| 41 | + '-m',"Update at %s" % str(datetime.datetime.utcnow())], |
| 42 | + silence=True) |
| 43 | + _call(['git', 'push', 'origin', config.deploy.git.branch]) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +def rsync(): |
| 48 | + if not check_rsync(): |
| 49 | + logging.error("Catsup can't find rsync.Please install rsync first.") |
| 50 | + sys.exit(1) |
| 51 | + logging.info("Deploying your blog via rsync") |
| 52 | + if config.deploy.rsync.delete: |
| 53 | + args = "--delete" |
| 54 | + else: |
| 55 | + args = "" |
| 56 | + cmd = "rsync -avze 'ssh -p {ssh_port}' {args}" \ |
| 57 | + " {deploy_dir}/ {ssh_user}@{ssh_host}:{document_root}".format( |
| 58 | + ssh_port=config.deploy.rsync.ssh_port, |
| 59 | + args=args, |
| 60 | + deploy_dir=config.config.output, |
| 61 | + ssh_user=config.deploy.rsync.ssh_user, |
| 62 | + ssh_host=config.deploy.rsync.ssh_host, |
| 63 | + document_root=config.deploy.rsync.document_root |
| 64 | + ) |
| 65 | + os.system(cmd) |
0 commit comments