~james-page/charms/trusty/cinder/lp1531102-trunk

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2016-01-07 14:13:23 UTC
  • Revision ID: james.page@ubuntu.com-20160107141323-vh619kqojsjh7i0r
Resync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
566
566
        os.chdir(cur)
567
567
 
568
568
 
569
 
def chownr(path, owner, group, follow_links=True):
 
569
def chownr(path, owner, group, follow_links=True, chowntopdir=False):
 
570
    """
 
571
    Recursively change user and group ownership of files and directories
 
572
    in given path. Doesn't chown path itself by default, only its children.
 
573
 
 
574
    :param bool follow_links: Also Chown links if True
 
575
    :param bool chowntopdir: Also chown path itself if True
 
576
    """
570
577
    uid = pwd.getpwnam(owner).pw_uid
571
578
    gid = grp.getgrnam(group).gr_gid
572
579
    if follow_links:
574
581
    else:
575
582
        chown = os.lchown
576
583
 
 
584
    if chowntopdir:
 
585
        broken_symlink = os.path.lexists(path) and not os.path.exists(path)
 
586
        if not broken_symlink:
 
587
            chown(path, uid, gid)
577
588
    for root, dirs, files in os.walk(path):
578
589
        for name in dirs + files:
579
590
            full = os.path.join(root, name)