-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureTools.py
More file actions
110 lines (88 loc) · 2.82 KB
/
AzureTools.py
File metadata and controls
110 lines (88 loc) · 2.82 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
99
100
101
102
103
104
105
106
107
108
109
__author__ = 'Natalie'
import os
import zipfile
from getpass import getuser
from time import localtime
import shutil
# The current user on the computer; used to access the right folder under C:/Users
comp_user = getuser()
def timestamp():
"""
Creates a timestamp
:return: stamp
"""
# Simulation name and it's online ID (VM name) is written to the user's project file
year = str(localtime()[0])[-2:].zfill(2)
month = str(localtime()[1]).zfill(2)
date = str(localtime()[2]).zfill(2)
hour = str(localtime()[3]).zfill(2)
minute = str(localtime()[4]).zfill(2)
sec = str(localtime()[5]).zfill(2)
stamp = month + '/' + date + '/' + year + ' ' + hour + ':' + minute + ':' + sec
return stamp
def zip_files(user, inputs):
"""
Zips all input files into one archive
:param user:
:param inputs:
:return:
"""
inputs_dir = os.path.dirname(inputs) # ie C:/Users/SomeName
inputs_folder_name = os.path.basename(inputs) # ie InputFiles
os.chdir("C:/Users/" + comp_user + "/Simulations/" + user)
# Zip the input folder, if not already
if not zipfile.is_zipfile(inputs):
print "\nZipping file..."
z = zipfile.ZipFile("Inputs.zip", "w", zipfile.ZIP_DEFLATED)
z.write("AzureUserInfo.pickle")
# If there is one input file...
if os.path.isfile(inputs):
print inputs +"\n"
os.chdir(inputs_dir)
z.write(inputs_folder_name)
# If there are multiple input files/folders in the given folder...
else:
for base, dirs, files in os.walk(inputs):
for f in files:
print f + "\n"
fn = os.path.join(base, f)
z.write(fn, f)
else:
shutil.copyfile(inputs, "Inputs.zip")
z = zipfile.ZipFile("Inputs.zip", "a", zipfile.ZIP_DEFLATED)
z.write("AzureUserInfo.pickle")
z.close()
print "In archive...\n"
print zipfile.ZipFile.namelist(z)
'''
for f in os.walk("Inputs.zip"):
print "\t" + f + "\n"
'''
return "C:/Users/" + comp_user + "/Simulations/" + user + "/Inputs.zip"
def extract_files(file_name, username):
"""
Unzips files from the provided path.
:param file_name:
:param username:
:return:
"""
output_path = os.path.normpath('C:/Users/' + comp_user + '/Simulations/' + username)
os.chdir(output_path)
z = zipfile.ZipFile(file_name + '.zip', 'r')
z.extractall(file_name)
z.close()
#TODO cleanup function
def cleanup(self, service="", file=""):
'''
:param service:
:param file:
:return:
'''
'''
check if cloud service given
check if cloud service exists
delete cloud service
check if file name given
check if file exists
delete file
'''