Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dist/tools: add lazysponge tool #9634

Merged
merged 3 commits into from
Aug 20, 2018
Merged

Conversation

cladmi
Copy link
Contributor

@cladmi cladmi commented Jul 25, 2018

Contribution description

Add a tool that writes stdin to a file only if it would modify the file content.

The design and name is based on moreutils tool sponge https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/#sponge

The usage of the tool in the build system would be to save build configuration variables in a file and rebuild make targets if and only if the build configuration changed.
So only checking the variables would be done at each compilation.

I also used this file for genconfigheader to remove the file management out of it and give an usage example.

Todo

  • Add python tests

Issues/PRs references

Follow up to the #9623 PR where .PHONY is replaced by a dependency.
#9589

@cladmi cladmi added Area: build system Area: Build system Area: tools Area: Supplementary tools labels Jul 25, 2018
Copy link
Contributor

@jcarrano jcarrano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost Ok. I don't expect this tool to be used with large files, so I didn't mind checking efficiency.


# No support for 'interactive' input as catching Ctrl+c breaks in 'read'
if os.isatty(sys.stdin.fileno()):
print('Interactive input no supported. Use piped input')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be printed to stderr.

Makefile.include Outdated
@@ -646,7 +646,8 @@ include $(RIOTTOOLS)/desvirt/Makefile.desvirt
# The script will only touch the file if anything has changed since last time.
$(RIOTBUILD_CONFIG_HEADER_C): FORCE
@mkdir -p '$(dir $@)'
$(Q)'$(RIOTTOOLS)/genconfigheader/genconfigheader.sh' '$@' $(CFLAGS_WITH_MACROS)
$(Q)'$(RIOTTOOLS)/genconfigheader/genconfigheader.sh' $(CFLAGS_WITH_MACROS) \
| '$(RIOTTOOLS)/lazysponge/lazysponge.py' $(if $(filter 1,$(QUIET)),,--verbose) '$@'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this tool will be used in more than one place, can '$(RIOTTOOLS)/lazysponge/lazysponge.py' be made into a variable "$(LAZYSPONGE)"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it would also need to be exported to be visible in Makefile.base or pkg.mk for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a variable, should I also add LSFLAGS ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment was more about the tedium of having to write '$(RIOTTOOLS)/lazysponge/lazysponge.py' each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's the same for $(if $(filter 1,$(QUIET)),,--verbose) that's why I would add LSFLAGS or any other name.


def _print_hash_debug_info(outfilename, oldbytes, newbytes):
"""Print debug information on hashs."""
oldhash = hashlib.md5(oldbytes).hexdigest() if oldbytes is not None else ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is printing the hash useful? The sizes would probably give more information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could have the same size and be different. I also kept the previous behavior as before.
And when trying changes a hash is easy enough to recognize for 5 runs in a row.

' If the content is the same, file is not modified.')
PARSER = argparse.ArgumentParser(description=DESCRIPTION)
PARSER.add_argument('outfile', help='Output file')
PARSER.add_argument('--verbose', help='Verbose output', default=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a short option too ("-v")?

@cladmi
Copy link
Contributor Author

cladmi commented Jul 31, 2018

For the efficiency, I did not try either. I am more introducing the concept here of a script to write generic configuration file and force rebuild when we are currently not.
If there is indeed an efficiency issue for starting python at some point it could be re-implemented in C with the same interface.

oldhash = hashlib.md5(oldbytes).hexdigest() if oldbytes is not None else ''
newhash = hashlib.md5(newbytes).hexdigest()
if oldbytes == newbytes:
print('Keeping old {} ({})'.format(outfilename, oldhash))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should also go to stderr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure to put debug to stderr, according to:

https://unix.stackexchange.com/questions/331611/do-progress-reports-logging-information-belong-on-stderr-or-stdout

it depends if it is considered a conventional output or a diagnostic output.
And as its with --verbose I am not sure.

I pushed a fixup to go in your direction anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For tools, I tend to consider:

  • stdout: machine-readable output of the command, that can be piped to another command.
  • stderr: auxiliary/non essential output meant to be read by a human, so that it still shows up even if stdout is redirected.

For an example of a data-copying command, see dd:

$ dd if=/dev/zero of=hhhh bs=1k count=1 >/dev/null
1+0 records in
1+0 records out
1024 bytes (1.0 kB, 1.0 KiB) copied, 0.00027606 s, 3.7 MB/s

@cladmi cladmi added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Aug 7, 2018
@cladmi
Copy link
Contributor Author

cladmi commented Aug 9, 2018

I fixed a typo in the error message and added a test.

@cladmi
Copy link
Contributor Author

cladmi commented Aug 20, 2018

Can I rebase/squash ?

@jcarrano
Copy link
Contributor

@cladmi Please.

cladmi added 3 commits August 20, 2018 11:34
Write stdin to <outfile> if it is different from the previous content.

If data provided in stdin is the same as the data that was in <outfile>, it is
not modified so `last modification date` is not changed.
Remove file management from `genconfigheader` script and use `lazysponge` in
Makefile.include.

Use --verbose option when in non QUIET building mode.
@cladmi cladmi force-pushed the pr/make/lazysponge branch from 4f06318 to cc63d2d Compare August 20, 2018 09:35
@cladmi
Copy link
Contributor Author

cladmi commented Aug 20, 2018

Done, now waiting for murdock.

@jcarrano jcarrano merged commit 635ecf9 into RIOT-OS:master Aug 20, 2018
@cladmi cladmi deleted the pr/make/lazysponge branch August 20, 2018 12:10
@cladmi cladmi added this to the Release 2018.10 milestone Nov 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants