Projet

Général

Profil

0002-misc-clean-old-thumbnails-52528.patch

Lauréline Guérin, 09 avril 2021 15:03

Télécharger (3 ko)

Voir les différences:

Subject: [PATCH 2/2] misc: clean old thumbnails (#52528)

 tests/test_publisher.py | 18 ++++++++++++++++++
 wcs/qommon/publisher.py | 18 +++++++++++++-----
 2 files changed, 31 insertions(+), 5 deletions(-)
tests/test_publisher.py
319 319

  
320 320
    pub.clean_tempfiles()
321 321
    assert os.listdir(dirname) == ['a']
322

  
323

  
324
def test_clean_thumbnails():
325
    pub = create_temporary_pub()
326
    pub.clean_thumbnails()
327

  
328
    dirname = os.path.join(pub.app_dir, 'thumbs')
329
    if not os.path.exists(dirname):
330
        os.mkdir(dirname)
331

  
332
    with open(os.path.join(dirname, 'a'), 'w') as fd:
333
        fd.write('a')
334

  
335
    with open(os.path.join(dirname, 'b'), 'w') as fd:
336
        os.utime(fd.fileno(), times=(time.time() - 40 * 86400, time.time() - 40 * 86400))
337

  
338
    pub.clean_thumbnails()
339
    assert os.listdir(dirname) == ['a']
wcs/qommon/publisher.py
613 613
                # started more than two days ago, probably aborted job
614 614
                job.remove_self()
615 615

  
616
    def clean_tempfiles(self):
617
        now = time.time()
618
        one_month_ago = now - 30 * 86400
619
        dirname = os.path.join(self.app_dir, 'tempfiles')
616
    def _clean_files(self, limit, dirname):
620 617
        if not os.path.exists(dirname):
621 618
            return
622 619
        for filename in os.listdir(dirname):
623
            if os.stat(os.path.join(dirname, filename))[8] < one_month_ago:
620
            if os.stat(os.path.join(dirname, filename))[8] < limit:
624 621
                try:
625 622
                    os.unlink(os.path.join(dirname, filename))
626 623
                except OSError:
627 624
                    pass
628 625

  
626
    def clean_tempfiles(self):
627
        now = time.time()
628
        one_month_ago = now - 30 * 86400
629
        self._clean_files(one_month_ago, os.path.join(self.app_dir, 'tempfiles'))
630

  
631
    def clean_thumbnails(self):
632
        now = time.time()
633
        one_month_ago = now - 30 * 86400
634
        self._clean_files(one_month_ago, os.path.join(self.app_dir, 'thumbs'))
635

  
629 636
    @classmethod
630 637
    def register_cronjobs(cls):
631 638
        cls.register_cronjob(CronJob(cls.clean_sessions, minutes=range(0, 60, 5), name='clean_sessions'))
632 639
        cls.register_cronjob(CronJob(cls.clean_nonces, minutes=range(0, 60, 5), name='clean_nonces'))
633 640
        cls.register_cronjob(CronJob(cls.clean_afterjobs, minutes=[0], name='clean_afterjobs'))
634 641
        cls.register_cronjob(CronJob(cls.clean_tempfiles, minutes=[0], name='clean_tempfiles'))
642
        cls.register_cronjob(CronJob(cls.clean_thumbnails, minutes=[0], name='clean_thumbnails'))
635 643

  
636 644
    _initialized = False
637 645

  
638
-