From f4d0dadebe5d77d3133defb75330d65583efc24c Mon Sep 17 00:00:00 2001 From: Elliot Nunn Date: Sat, 29 Jun 2019 22:56:27 +0800 Subject: [PATCH] Create useful .build/.flatten scripts Also, factor out some common code --- bin/build | 11 ++--------- bin/deepen | 35 ++++++++++++++++++++++++++--------- bin/flatten | 11 ++--------- bin/folder.py | 9 +++++++++ bin/getsrc | 9 +-------- 5 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 bin/folder.py diff --git a/bin/build b/bin/build index ac81093..c7fbe7c 100755 --- a/bin/build +++ b/bin/build @@ -2,6 +2,7 @@ # In this folder from splice import splice +from folder import folder # python3 -m pip install machfs macresources import machfs @@ -20,19 +21,11 @@ import sys -def folder(default_search, user_input): - if path.sep in user_input: - return user_input - else: - the_path = path.dirname(path.dirname(path.abspath(__file__))) - the_path = path.join(the_path, default_search, user_input) - return the_path - - parser = argparse.ArgumentParser(description=''' Build the sources in the working directory. ''') +parser.add_argument('worktree', metavar='WORKTREE', action='store', type=lambda x: folder('worktree', x), help='Worktree (assumed in ../worktree/)') parser.add_argument('passthru', metavar='ARG', nargs='+', help='Build script args (RISC/ROM/LC930/dbLite or FNDR)') config = parser.parse_args() diff --git a/bin/deepen b/bin/deepen index cbe406d..6b8f3a6 100755 --- a/bin/deepen +++ b/bin/deepen @@ -1,18 +1,25 @@ #!/usr/bin/env python3 import argparse -from os import path, makedirs, walk, listdir, remove +from os import path, makedirs, walk, listdir, remove, stat, chmod from shutil import copy, rmtree from subprocess import run +from shlex import quote + +from folder import folder -def folder(default_search, user_input): - if path.sep in user_input: - return user_input - else: - the_path = path.dirname(path.dirname(path.abspath(__file__))) - the_path = path.join(the_path, default_search, user_input) - return the_path +def make_executable(path): # thanks to Jonathon Reinhart on SO + mode = stat(path).st_mode + mode |= (mode & 0o444) >> 2 # copy R bits to X + chmod(path, mode) + +def relpath(dest, src): + return path.relpath(path.abspath(dest), path.abspath(src)) + +def path_to_script(script_name, from_worktree): + script_path = path.join(path.dirname(__file__), script_name) + return relpath(script_path, from_worktree) parser = argparse.ArgumentParser(description=''' @@ -45,8 +52,18 @@ makedirs(args.worktree) with open(path.join(args.worktree, '.gitignore'), 'a') as f: f.write('.*\n*.dmg\nBuild*/\n') +with open(path.join(args.worktree, '.build'), 'w') as f: + print('#!/bin/sh', file=f) + print('exec ' + quote(path_to_script('build', args.worktree)) + ' . "$@"', file=f) + make_executable(f.name) + +with open(path.join(args.worktree, '.flatten'), 'w') as f: + print('#!/bin/sh', file=f) + print('exec ' + quote(path_to_script('flatten', args.worktree)) + ' . ' + relpath(args.patchset, args.worktree) + ' "$@"', file=f) + make_executable(f.name) + git('init') -git('add', '-f', '.gitignore') +git('add', '-f', '.gitignore', '.build', '.flatten') git('commit', '-m', 'Useful non-source things') for walk_base, walk_dirs, walk_files in walk(args.base): diff --git a/bin/flatten b/bin/flatten index 39b8c09..6fa28a2 100755 --- a/bin/flatten +++ b/bin/flatten @@ -4,21 +4,14 @@ import argparse from os import path, getcwd, makedirs, listdir, remove, rename from subprocess import run, DEVNULL - -def folder(default_search, user_input): - if path.sep in user_input: - return user_input - else: - the_path = path.dirname(path.dirname(path.abspath(__file__))) - the_path = path.join(the_path, default_search, user_input) - return the_path +from folder import folder parser = argparse.ArgumentParser(description=''' Shrink a git changelog into a patchset (based on git format-patch) ''') -parser.add_argument('worktree', metavar='WORKTREE', nargs='?', action='store', default=getcwd(), type=lambda x: folder('worktree', x), help='Worktree (default is cwd, or assumed in ../worktree/)') +parser.add_argument('worktree', metavar='WORKTREE', action='store', type=lambda x: folder('worktree', x), help='Worktree (assumed in ../worktree/)') parser.add_argument('patchset', metavar='PATCHSET', action='store', type=lambda x: folder('patchset', x), help='Destination patchset (assumed in ../patchset/)') parser.add_argument('--raw', action='store_true', help='Do not neaten up commits') diff --git a/bin/folder.py b/bin/folder.py new file mode 100644 index 0000000..dbdbe77 --- /dev/null +++ b/bin/folder.py @@ -0,0 +1,9 @@ +from os import path + +def folder(default_search, user_input): + if path.sep in user_input or user_input in ('..', '.'): + return user_input + else: + the_path = path.dirname(path.dirname(path.abspath(__file__))) + the_path = path.join(the_path, default_search, user_input) + return the_path diff --git a/bin/getsrc b/bin/getsrc index b894d14..3c86a6f 100755 --- a/bin/getsrc +++ b/bin/getsrc @@ -4,14 +4,7 @@ import argparse from os import path, makedirs from shutil import rmtree - -def folder(default_search, user_input): - if path.sep in user_input: - return user_input - else: - the_path = path.dirname(path.dirname(path.abspath(__file__))) - the_path = path.join(the_path, default_search, user_input) - return the_path +from folder import folder parser = argparse.ArgumentParser(description='''