diff options
author | Filipp Lepalaan <filipp@mac.com> | 2016-10-27 12:22:50 +0300 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2016-10-27 12:22:50 +0300 |
commit | 8c33675b1e0b2b07d538f17afc84e7ed691154ca (patch) | |
tree | 8c22242b93896a309d1c3617f4e3330dc9f54072 | |
parent | 7944e534b2388dfa511dea9f7185c115bfed6587 (diff) | |
download | machammer-8c33675b1e0b2b07d538f17afc84e7ed691154ca.tar.gz machammer-8c33675b1e0b2b07d538f17afc84e7ed691154ca.tar.bz2 machammer-8c33675b1e0b2b07d538f17afc84e7ed691154ca.zip |
Moved user-related functions, added unzip_app
-rw-r--r-- | machammer/functions.py | 63 | ||||
-rw-r--r-- | machammer/users.py | 70 |
2 files changed, 73 insertions, 60 deletions
diff --git a/machammer/functions.py b/machammer/functions.py index 728f55a..a52dea0 100644 --- a/machammer/functions.py +++ b/machammer/functions.py @@ -101,66 +101,9 @@ def copy_app(path): rsync(path.rstrip('/'), '/Applications/') -def add_login_item(path, name=None, hidden=True): - """Add login item to the current user.""" - if not name: - name = os.path.basename(path) - - hidden = 'true' if hidden else 'false' - tell_app('System Events', - 'make login item at end with properties {path:"%s", hidden:%s, name:"%s"}' % (path, hidden, name)) - - -def remove_login_item(**kwargs): - """Remove login item from the current user.""" - if not (kwargs.get('name') or kwargs.get('path')): - raise ValueError('Need either path or name') - - if kwargs.get('name') and kwargs.get('path'): - raise ValueError('Please specify only path OR name') - - k, v = kwargs.items()[0] - tell_app('System Events', 'delete every login item whose %s is "%s"' % (k, v)) - - -def create_user(username, realname, password): - """Create a user account.""" - os.system("""dscl . create /Users/{0} - dscl . create /Users/{0} RealName "{1}" - dscl . passwd /Users/{0} {2} - dscl . create /Users/{0} UniqueID 501 - dscl . create /Users/{0} PrimaryGroupID 80 - dscl . create /Users/{0} UserShell /bin/bash - dscl . create /Users/{0} NFSHomeDirectory /Users/{0} - cp -R /System/Library/User\ Template/English.lproj /Users/{0} - chown -R {0}:staff /Users/{0}""".format(username, realname, password)) - - -def hide_user(username, hide_home=True): - """Hide a user account.""" - path = '/Users/%s' % username - subprocess.call(['dscl', '.', 'create', path, 'IsHidden', '1']) - - if hide_home: - subprocess.call(['/usr/bin/chflags', 'hidden', path]) - - -def delete_user(username, delete_home=True): - """Delete a user account.""" - path = '/Users/' + username - dscl = subprocess.check_output(['dscl', '-plist', '.', 'read', path]) - userinfo = plistlib.readPlistFromString(dscl) - - subprocess.call(['dscl', '.', 'delete', path]) - - if delete_home: - homedir = userinfo['dsAttrTypeStandard:NFSHomeDirectory'][0] - os.rmdir(homedir) - - -def make_admin(username): - """Give admin rights to username.""" - subprocess.call(['dscl', '.', '-append', '/Groups/admin', 'users', username]) +def unzip_app(path): + """Install zipped application.""" + call('/usr/bin/unzip', '-q', path, '-d', '/Applications') def enable_ard(username): diff --git a/machammer/users.py b/machammer/users.py new file mode 100644 index 0000000..a64691a --- /dev/null +++ b/machammer/users.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +"""machammer.users.""" + +import os +import plistlib +import subprocess + +from .functions import tell_app + + +def add_login_item(path, name=None, hidden=True): + """Add login item to the current user.""" + if not name: + name = os.path.basename(path) + + hidden = 'true' if hidden else 'false' + tell_app('System Events', + 'make login item at end with properties {path:"%s", hidden:%s, name:"%s"}' % (path, hidden, name)) + + +def remove_login_item(**kwargs): + """Remove login item from the current user.""" + if not (kwargs.get('name') or kwargs.get('path')): + raise ValueError('Need either path or name') + + if kwargs.get('name') and kwargs.get('path'): + raise ValueError('Please specify only path OR name') + + k, v = kwargs.items()[0] + tell_app('System Events', 'delete every login item whose %s is "%s"' % (k, v)) + + +def create_user(username, realname, password): + """Create a user account.""" + os.system("""dscl . create /Users/{0} + dscl . create /Users/{0} RealName "{1}" + dscl . passwd /Users/{0} {2} + dscl . create /Users/{0} UniqueID 501 + dscl . create /Users/{0} PrimaryGroupID 80 + dscl . create /Users/{0} UserShell /bin/bash + dscl . create /Users/{0} NFSHomeDirectory /Users/{0} + cp -R /System/Library/User\ Template/English.lproj /Users/{0} + chown -R {0}:staff /Users/{0}""".format(username, realname, password)) + + +def hide_user(username, hide_home=True): + """Hide a user account.""" + path = '/Users/%s' % username + subprocess.call(['dscl', '.', 'create', path, 'IsHidden', '1']) + + if hide_home: + subprocess.call(['/usr/bin/chflags', 'hidden', path]) + + +def delete_user(username, delete_home=True): + """Delete a user account.""" + path = '/Users/' + username + dscl = subprocess.check_output(['dscl', '-plist', '.', 'read', path]) + userinfo = plistlib.readPlistFromString(dscl) + + subprocess.call(['dscl', '.', 'delete', path]) + + if delete_home: + homedir = userinfo['dsAttrTypeStandard:NFSHomeDirectory'][0] + os.rmdir(homedir) + + +def make_admin(username): + """Give admin rights to username.""" + subprocess.call(['dscl', '.', '-append', '/Groups/admin', 'users', username]) |