initial version of siteupdate
This commit is contained in:
		
							parent
							
								
									9c018aa8e7
								
							
						
					
					
						commit
						969e800973
					
				
							
								
								
									
										88
									
								
								siteupdate.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										88
									
								
								siteupdate.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,88 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
import argparse
 | 
			
		||||
import subprocess
 | 
			
		||||
import os
 | 
			
		||||
from glob import glob
 | 
			
		||||
 | 
			
		||||
def site_update(args):
 | 
			
		||||
    print('update site')
 | 
			
		||||
    if not os.path.isdir(args.src):
 | 
			
		||||
        print(f'directory {args.src} does not exist', file=sys.stderr)
 | 
			
		||||
        return 1
 | 
			
		||||
    p = subprocess.run(['git', 'fetch', 'origin'], cwd=args.src)
 | 
			
		||||
    if p.returncode != 0:
 | 
			
		||||
        return 1
 | 
			
		||||
    p = subprocess.run(['git', 'status'], cwd=args.src, capture_output=True)
 | 
			
		||||
    if p.returncode != 0:
 | 
			
		||||
        return 1
 | 
			
		||||
    if 'behind' in str(p.stdout):
 | 
			
		||||
        p = subprocess.run(['git', 'merge', 'origin/master'], cwd=args.src)
 | 
			
		||||
        if p.returncode != 0:
 | 
			
		||||
            return 1
 | 
			
		||||
        if args.b:
 | 
			
		||||
            if args.dest is None:
 | 
			
		||||
                print('need --dest argument for -b', file=sys.stderr)
 | 
			
		||||
                return 1
 | 
			
		||||
            site_build(args)
 | 
			
		||||
    else:
 | 
			
		||||
        print('up to date')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
def site_build(args):
 | 
			
		||||
    if not os.path.isdir(args.src):
 | 
			
		||||
        print(f'directory {args.src} does not exist', file=sys.stderr)
 | 
			
		||||
        return 1
 | 
			
		||||
    if not os.path.isdir(args.dest):
 | 
			
		||||
        try:
 | 
			
		||||
            os.mkdir(args.dest)
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            print(e, file=sys.stderr)
 | 
			
		||||
            return 1
 | 
			
		||||
    print('building site')
 | 
			
		||||
    p = subprocess.run('hugo', cwd=args.src)
 | 
			
		||||
    if p.returncode != 0:
 | 
			
		||||
        return 1
 | 
			
		||||
    new_files = glob('public/*', root_dir=args.src)
 | 
			
		||||
    p = subprocess.run(['which', 'rsync'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
 | 
			
		||||
    if p.returncode == 0:
 | 
			
		||||
        print('copying new files')
 | 
			
		||||
        p = subprocess.run(['rsync', '-r', *new_files, args.dest], cwd=args.src)
 | 
			
		||||
        if p.returncode != 0:
 | 
			
		||||
            return 1
 | 
			
		||||
    else:
 | 
			
		||||
        print('rsync not found, using basic rm and cp')
 | 
			
		||||
        old_files = glob('*', root_dir=args.dest)
 | 
			
		||||
        print('cleaning up old files')
 | 
			
		||||
        p = subprocess.run(['rm','-rf', *old_files], cwd=args.dest)
 | 
			
		||||
        if p.returncode != 0:
 | 
			
		||||
            return 1
 | 
			
		||||
        print('copying new files')
 | 
			
		||||
        p = subprocess.run(['cp', '-r', *new_files, args.dest], cwd=args.src)
 | 
			
		||||
        if p.returncode != 0:
 | 
			
		||||
            return 1
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    parser = argparse.ArgumentParser()
 | 
			
		||||
    subparsers = parser.add_subparsers(help='sub-command help')
 | 
			
		||||
 | 
			
		||||
    parser_update = subparsers.add_parser('update', help='update help')
 | 
			
		||||
    parser_update.set_defaults(func=site_update)
 | 
			
		||||
 | 
			
		||||
    parser_build = subparsers.add_parser('build', help='build help')
 | 
			
		||||
    parser_build.set_defaults(func=site_build)
 | 
			
		||||
 | 
			
		||||
    parser_update.add_argument('--src', required=True)
 | 
			
		||||
    parser_update.add_argument('--dest', required=False)
 | 
			
		||||
    parser_update.add_argument('-b', action='store_true', required=False)
 | 
			
		||||
    parser_build.add_argument('--src', required=True)
 | 
			
		||||
    parser_build.add_argument('--dest', required=True)
 | 
			
		||||
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
    rc = args.func(args)
 | 
			
		||||
    exit(rc)
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user