Create useful .build/.flatten scripts

Also, factor out some common code
This commit is contained in:
Elliot Nunn 2019-06-29 22:56:27 +08:00
parent 93dfc883a1
commit f4d0dadebe
5 changed files with 40 additions and 35 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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')

9
bin/folder.py Normal file
View File

@ -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

View File

@ -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='''