~gary-lasker/software-center/cherrypick-for-5.2.2.1

« back to all changes in this revision

Viewing changes to softwarecenter/utils.py

  • Committer: Michael Vogt
  • Date: 2012-05-15 07:51:29 UTC
  • mfrom: (2989.17.8 software-center)
  • Revision ID: michael.vogt@ubuntu.com-20120515075129-szlkcz1nqcs0qu46
merge lp:~gary-lasker/software-center/fix-makedirs-race-crashes and add small test for test_safe_makedirs

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import traceback
29
29
import time
30
30
import xml.sax.saxutils
 
31
import errno
31
32
 
32
33
# py3 compat
33
34
try:
709
710
                          file_path, e)
710
711
 
711
712
 
 
713
def safe_makedirs(dir_path):
 
714
    """ This function can be used in place of a straight os.makedirs to
 
715
        handle the possibility of a race condition when more than one
 
716
        process may potentially be creating the same directory, it will
 
717
        not fail if two processes try to create the same dir at the same
 
718
        time
 
719
    """
 
720
    # avoid throwing an OSError, see for example LP: #743003
 
721
    if not os.path.exists(dir_path):
 
722
        try:
 
723
            os.makedirs(dir_path)
 
724
        except OSError as e:
 
725
            print e
 
726
            if e.errno == errno.EEXIST:
 
727
                # it seems that another process has already created this
 
728
                # directory in the meantime, that's ok
 
729
                pass
 
730
            else:
 
731
                # the error is due to something else, so we want to raise it
 
732
                raise
 
733
 
 
734
 
712
735
class SimpleFileDownloader(GObject.GObject):
713
736
 
714
737
    LOG = logging.getLogger("softwarecenter.simplefiledownloader")