-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
98 lines (84 loc) · 3.73 KB
/
functions.py
File metadata and controls
98 lines (84 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import subprocess
import requests
import os
import logging
import urllib.request
from config import Config
class Functions(Config):
def __init__(self):
super().__init__()
logging.basicConfig(
filename=self.diretorioarcom + "\\INSTALL_COOPS.log",
filemode="a",
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
self.logger = logging
# Super classe para funcao de report
def reportar(self, msg):
self.logger.debug(msg)
# Execucao de aplicativos e scripts
def executar(self, command):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
process = subprocess.Popen(
command,
cwd=self.diretorioarcom,
startupinfo=startupinfo,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
shell=True,
)
result, error = process.communicate()
exitCode = process.wait()
errortext = error.decode("cp1252")
if exitCode > 0:
if str(error) == "":
error = result
self.reportar("Code: " + str(exitCode) + " - Message: " + errortext)
else:
self.reportar("Finalizado sem problema")
# Adicionar no domínio
def addtodomain(self, dominio, usuario, senha):
self.executar(
f'@"%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "$domain = "{dominio}"; $password = "{senha}" | ConvertTo-SecureString -asPlainText -Force; $username = \'{usuario}@$domain\';$credential = New-Object System.Management.Automation.PSCredential($username,$password);Add-Computer -DomainName $domain -Credential $credential"'
)
# Executa instalacao de programas
def installprograma(self, diretorioarcom, programa):
if not os.path.isfile(self.chocolateypath):
script_url = 'https://community.chocolatey.org/install.ps1'
ps_script_name = 'install_chocolatey.ps1'
ps_script_path = diretorioarcom + '\\' + ps_script_name
self.reportar("Executando instalação do chocolatey")
urllib.request.urlretrieve(script_url, ps_script_path)
self.executar(
'powershell.exe -noprofile -executionpolicy bypass -file ' + ps_script_path
)
self.executar(
self.chocolateypath + " config set cacheLocation " + diretorioarcom
)
self.reportar("Executando instalação, " + programa)
self.executar(self.chocolateypath + " install -y " + programa)
# Baixa arquivos do google drive
def download_file_from_google_drive(self, id, destination):
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith("download_warning"):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params={"id": id}, stream=True)
token = get_confirm_token(response)
if token:
params = {"id": id, "confirm": token}
response = session.get(URL, params=params, stream=True)
save_response_content(response, destination)