Skip to content

Commit

Permalink
Special treatment for .patch and .zip/.7z HOOK files
Browse files Browse the repository at this point in the history
(closes #44)
  • Loading branch information
ralphlange committed Jun 29, 2020
1 parent ee362d8 commit 1b066fe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ recursing into submodules. [default is including submodules: `YES`]
be always be extended by the release or branch name as `<name>-<version>`.
[default is the slug in lower case: `foo`]
`FOO_HOOK=<script>` Set the name of a script that will be run after cloning
the module, before compiling it. Working directory when running the script
is the root of the targeted module (e.g. `.../.cache/foo-1.2`).
[default: no hooks are run]
`FOO_HOOK=<hook>` Set the name of a `.patch` file, a `.zip` or `.7z` archive
or a script that will be applied (using `-p1`), extracted or run after cloning
the module, before compiling it.
Working directory is the root of the targeted module,
e.g., `.../.cache/foo-1.2`). [default: no hook]
`FOO_VARNAME=<name>` Set the name that is used for the module when creating
the `RELEASE.local` files. [default is the slug in upper case: `FOO`]
Expand Down
38 changes: 38 additions & 0 deletions cue-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,44 @@ def test_ExtraMakeArgs(self):
'Extra make arg [{0}] not set (expected "bla {0}", found "{1}")'
.format(ind, cue.extra_makeargs[ind]))


class TestHooks(unittest.TestCase):
location = os.path.join(cue.cachedir, 'hook_test')
bla_file = os.path.join(location, 'bla.txt')
new_file = os.path.join(location, 'dd', 'new.txt')

def setUp(self):
if os.path.exists(self.location):
shutil.rmtree(self.location, onerror=cue.remove_readonly)
try:
os.makedirs(self.location)
except:
pass
with open(self.bla_file, 'w') as f:
f.write('''LINE1=YES
LINE2=NO''')

def test_patchfile(self):
hook = os.path.join(builddir, 'test.patch')
cue.apply_patch(hook, cwd=self.location)
line1_yes = False
with open(self.bla_file) as f:
if 'LINE1=YES' in f.read():
line1_yes = True
self.assertFalse(line1_yes, "Patch didn't change line in test file 'bla.txt'")
self.assertTrue(os.path.exists(self.new_file), "patch didn't add new file")

def test_archiveZip(self):
hook = os.path.join(builddir, 'test.zip')
cue.extract_archive(hook, cwd=self.location)
self.assertTrue(os.path.exists(self.new_file), "archive extract didn't add new file")

def test_archive7z(self):
hook = os.path.join(builddir, 'test.7z')
cue.extract_archive(hook, cwd=self.location)
self.assertTrue(os.path.exists(self.new_file), "archive extract didn't add new file")


if __name__ == "__main__":
if 'VV' in os.environ and os.environ['VV'] == '1':
logging.basicConfig(level=logging.DEBUG)
Expand Down
41 changes: 33 additions & 8 deletions cue.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def detect_context():
has_test_results = False
silent_dep_builds = True
do_recompile = False
installed_7z = False


def clear_lists():
Expand Down Expand Up @@ -403,6 +404,24 @@ def call_make(args=[], **kws):
sys.exit(exitcode)


def apply_patch(file, **kws):
place = kws.get('cwd', os.getcwd())
print('Applying patch {0} in {1}'.format(file, place))
logger.debug("EXEC '%s' in %s", ' '.join(['patch', '-p1', '-i', file]), place)
sys.stdout.flush()
sp.check_call(['patch', '-p1', '-i', file], cwd=place)
logger.debug('EXEC DONE')


def extract_archive(file, **kws):
place = kws.get('cwd', os.getcwd())
print('Extracting archive {0} in {1}'.format(file, place))
logger.debug("EXEC '%s' in %s", ' '.join(['7z', 'x', '-aoa', '-bd', file]), place)
sys.stdout.flush()
sp.check_call(['7z', 'x', '-aoa', '-bd', file], cwd=place)
logger.debug('EXEC DONE')


def get_git_hash(place):
logger.debug("EXEC 'git log -n1 --pretty=format:%%H' in %s", place)
sys.stdout.flush()
Expand Down Expand Up @@ -515,13 +534,19 @@ def add_dependency(dep):
with open(release, 'w') as fout:
print('-include $(TOP)/../RELEASE.local', file=fout)

# run hook if defined
# Apply HOOK
if dep + '_HOOK' in setup:
hook = os.path.join(place, setup[dep + '_HOOK'])
if os.path.exists(hook):
print('Running hook {0} in {1}'.format(setup[dep + '_HOOK'], place))
sys.stdout.flush()
sp.check_call(hook, shell=True, cwd=place)
hook = setup[dep + '_HOOK']
hook_file = os.path.join(curdir, hook)
if os.path.exists(hook_file):
if re.match(r'.+\\.patch$', hook):
apply_patch(hook_file, cwd=place)
elif re.match(r'.+\\.(zip|7z)$', hook):
extract_archive(hook_file, cwd=place)
else:
print('Running hook {0} in {1}'.format(hook, place))
sys.stdout.flush()
sp.check_call(hook_file, shell=True, cwd=place)

# write checked out commit hash to marker file
head = get_git_hash(place)
Expand Down Expand Up @@ -685,9 +710,9 @@ def fix_etc_hosts():
# 127.0.1.1 localhost localhost ip4-loopback
# 127.0.0.1 localhost nettuno travis vagrant travis-job-....

logger.debug("EXEC sudo sed -ie '/^127\.0\.1\.1/ s|localhost\s*||g' /etc/hosts")
logger.debug("EXEC sudo sed -ie '/^127\\.0\\.1\\.1/ s|localhost\\s*||g' /etc/hosts")
sys.stdout.flush()
exitcode = sp.call(['sudo', 'sed', '-ie', '/^127\.0\.1\.1/ s|localhost\s*||g', '/etc/hosts'])
sp.call(['sudo', 'sed', '-ie', '/^127\\.0\\.1\\.1/ s|localhost\\s*||g', '/etc/hosts'])
logger.debug('EXEC DONE')


Expand Down
Binary file added test.7z
Binary file not shown.
14 changes: 14 additions & 0 deletions test.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff -ruN a/bla.txt b/bla.txt
--- a/bla.txt 2020-06-19 18:54:43.129076711 +0200
+++ b/bla.txt 2020-06-19 18:55:05.093948316 +0200
@@ -1,3 +1,3 @@
-LINE1=YES
+LINE1=NO
LINE2=NO

diff -ruN a/dd/new.txt b/dd/new.txt
--- a/dd/new.txt 1970-01-01 01:00:00.000000000 +0100
+++ b/dd/new.txt 2020-06-19 18:55:35.255032413 +0200
@@ -0,0 +1,2 @@
+NEW LINE 1
+NEW LINE 2
Binary file added test.zip
Binary file not shown.

0 comments on commit 1b066fe

Please sign in to comment.