[Buildroot] [PATCH buildroot-test] scripts/autobuild-run: properly delete output dir even with write-protected folders

Yann E. MORIN yann.morin.1998 at free.fr
Sun Jul 19 12:15:24 UTC 2020


Thomas, All,

On 2020-07-18 22:55 +0200, Yann E. MORIN spake thusly:
> On 2020-07-18 22:18 +0200, Thomas Petazzoni spake thusly:
> > We're already deleting the output directory before a new build with
> > "rm -rf" so that write protected files are removed as well, which
> > shutil.rmtree() doesn't do.
> > 
> > However "rm -rf" fails on write protected folders.
[--SNIP--]
> >          if os.path.exists(self.outputdir):
> > -            # shutil.rmtree doesn't remove write-protected files
> > -            subprocess.call(["rm", "-rf", self.outputdir])
> > +            subprocess.call(["chmod", "-R", "+w", self.outputdir])
> > +            shutil.rmtree(self.outputdir)
> 
> Why can't we do as suggested in the docs:
>     https://docs.python.org/3/library/shutil.html#rmtree-example
> 
>     def rm_ro(f, p, _):
>         os.chmod(p, stat.S_IWRITE)
>         f(p)
> 
>     shutil.rmtree(self.outputdir, onerror=rm_ro)

We might need to be a little bit more careful, though:

    def rm_ro(f, p, _):
        os.chmod(os.path.dirname(p), stat.S_IWRITE)
        os.chmod(p, stat.S_IWRITE)
        f(p)

    shutil.rmtree(self.outputdir, onerror=rm_ro)

Regards,
Yann E. MORIN.

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'


More information about the buildroot mailing list