mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-22 10:52:17 +03:00
execute a command as a specific user,add text md5 and timestamp
This commit is contained in:
parent
6fdcf31ae6
commit
4cd85c8bce
1 changed files with 15 additions and 2 deletions
|
@ -10,6 +10,7 @@ import shutil
|
||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
class Util:
|
class Util:
|
||||||
|
@ -116,9 +117,11 @@ class Util:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def execute(command, stdin=None, env=None, cwd=None, shell=True, result=True):
|
def execute(command, stdin=None, env=None, cwd=None, shell=True, result=True, as_user=None):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if as_user is not None:
|
||||||
|
command = 'su - {0} -c "{1}"'.format(as_user, command)
|
||||||
process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell)
|
process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell)
|
||||||
|
|
||||||
if result is True:
|
if result is True:
|
||||||
|
@ -225,4 +228,14 @@ class Util:
|
||||||
with open(fname, 'rb') as f:
|
with open(fname, 'rb') as f:
|
||||||
for chunk in iter(lambda: f.read(4096), b""):
|
for chunk in iter(lambda: f.read(4096), b""):
|
||||||
hash_md5.update(chunk)
|
hash_md5.update(chunk)
|
||||||
return str(hash_md5.hexdigest())
|
return str(hash_md5.hexdigest())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_md5_text(content):
|
||||||
|
hash_md5 = hashlib.md5()
|
||||||
|
hash_md5.update(content.encode())
|
||||||
|
return str(hash_md5.hexdigest())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def timestamp():
|
||||||
|
return str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
||||||
|
|
Loading…
Reference in a new issue