Added files

This commit is contained in:
Antonio de la Rosa 2018-08-30 02:51:31 +02:00
commit eb2c6b1db6
5 changed files with 220 additions and 0 deletions

104
.gitignore vendored Normal file
View file

@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
.static_storage/
.media/
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/

1
README.md Normal file
View file

@ -0,0 +1 @@
# Utils for scripts in *nix like systems.

74
pastafariutils/unix.py Normal file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env python3
import time
import os
import re
import argparse
import json
import pwd
import sys
import crypt
from subprocess import call, DEVNULL
def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/nologin'):
if user_directory=='':
user_directory=user
try:
user_pwd=pwd.getpwnam(user)
return (False, 'User exists')
except KeyError:
# add user
if password!='':
salt=crypt.mksalt(crypt.METHOD_SHA512)
password='-p \"%s\"' % crypt.crypt(password, salt).replace('$', '\$')
if group!='':
stat_group=os.stat('/home/%s' % user_directory)
gid=stat_group.st_gid
func_user="sudo useradd -m -s %s -g %i %s -d /home/%s %s" % (shell, gid, password, user_directory, user)
else:
func_user="sudo useradd -m -s %s %s -d /home/%s %s" % (shell, password, user_directory, user)
if call(func_user, shell=True, stdout=DEVNULL) > 0:
return (True, '')
else:
return (False, 'Error executing useradd command')
def change_password(user, new_password):
try:
user_pwd=pwd.getpwnam(user)
if call("sudo echo \"%s:%s\" | chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0:
return (True, 'Change password successfully')
except KeyError:
return (False, 'I cannot change password, user exists?')
def del_user(user):
if call("sudo userdel -r %s" % user, shell=True, stdout=DEVNULL, stderr=DEVNULL) > 0:
return (True, 'Deleted user successfully')
else:
return (False, '')

3
setup.cfg Normal file
View file

@ -0,0 +1,3 @@
[metadata]
description-file = README.md

38
setup.py Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python3
import sys
import os
from setuptools import setup, find_packages
if sys.version_info < (3, 3):
raise NotImplementedError("Sorry, you need at least Python 3.3 for use pastafariutils.")
#import paramecio
# Pillow should be installed after if you need ImageField
# If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc
setup(name='paramecio',
version='0.5.0',
description='Utils for make *nix scripts.',
long_description='Utils for make *nix scripts.',
author='Antonio de la Rosa Caballero',
author_email='antonio.delarosa@coesinfo.com',
url='https://bitbucker.org/paramecio/parameciofm/',
packages=['pastafariutils'],
include_package_data=True,
install_requires=[],
license='GPLV3',
platforms = 'any',
classifiers=['Development Status :: 1 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: GPLV2 License',
'Topic :: Software Development :: Libraries :: Utils for *nix servers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
],
)