~ajkavanagh/charms/trusty/mongodb/fix-unit-test-lint-lp1533654

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Jorge Niedbalski
  • Author(s): Mario Splivalo
  • Date: 2015-01-22 17:09:22 UTC
  • mfrom: (56.1.66 mongodb.replsets-fix-try)
  • Revision ID: jorge.niedbalski@canonical.com-20150122170922-nz94p2e0j4xqbbda
[mariosplivalo, r=niedbalski,freyes] Fixes various replicaset issues bug LP: #1403698, LP: #1370542, LP: #1379604

unit tests Ok, amulet tests OK.

Show diffs side-by-side

added added

removed removed

Lines of Context:
162
162
    uid = pwd.getpwnam(owner).pw_uid
163
163
    gid = grp.getgrnam(group).gr_gid
164
164
    realpath = os.path.abspath(path)
165
 
    if os.path.exists(realpath):
166
 
        if force and not os.path.isdir(realpath):
 
165
    path_exists = os.path.exists(realpath)
 
166
    if path_exists and force:
 
167
        if not os.path.isdir(realpath):
167
168
            log("Removing non-directory file {} prior to mkdir()".format(path))
168
169
            os.unlink(realpath)
169
 
    else:
 
170
            os.makedirs(realpath, perms)
 
171
    elif not path_exists:
170
172
        os.makedirs(realpath, perms)
171
173
    os.chown(realpath, uid, gid)
 
174
    os.chmod(realpath, perms)
172
175
 
173
176
 
174
177
def write_file(path, content, owner='root', group='root', perms=0o444):
404
407
        os.chdir(cur)
405
408
 
406
409
 
407
 
def chownr(path, owner, group):
 
410
def chownr(path, owner, group, follow_links=True):
408
411
    uid = pwd.getpwnam(owner).pw_uid
409
412
    gid = grp.getgrnam(group).gr_gid
 
413
    if follow_links:
 
414
        chown = os.chown
 
415
    else:
 
416
        chown = os.lchown
410
417
 
411
418
    for root, dirs, files in os.walk(path):
412
419
        for name in dirs + files:
413
420
            full = os.path.join(root, name)
414
421
            broken_symlink = os.path.lexists(full) and not os.path.exists(full)
415
422
            if not broken_symlink:
416
 
                os.chown(full, uid, gid)
 
423
                chown(full, uid, gid)
 
424
 
 
425
 
 
426
def lchownr(path, owner, group):
 
427
    chownr(path, owner, group, follow_links=False)