-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip_job.py
66 lines (55 loc) · 2.88 KB
/
zip_job.py
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
import logging
import os
import zipfile
from pathlib import Path # This is required for line 62 when running the script as standalone
import time # This is required in case line 10 will be enabled
def create_file(full_file_name):
with open(full_file_name, 'w') as f:
#time.sleep(20) #Using sleep will give you the option to delete the file manually in order to check the if statment in case the file was not created
if os.path.exists(full_file_name) == True:
print("Created filename: " + filename + file_extension + " under " + file_path)
logging.info("Created filename: " + filename + file_extension + " under " + file_path)
else:
print("Failed to create file - exiting")
logging.error("Could not create filename: " + filename + file_extension + " under " + file_path + " exiting")
exit()
def create_zip(zip_file_name, full_file_name):
handle = zipfile.ZipFile(zip_file_name, 'w')
handle.write(full_file_name,compress_type= zipfile.ZIP_DEFLATED)
handle.close()
#time.sleep(20) #Using sleep will give you the option to delete the file manually in order to check the if statment in case the file was not created
if os.path.exists(zip_file_name) == True:
print("Zip file was created under : " + zip_file_name)
logging.info("Zip file was created under : " + zip_file_name)
else:
print("Failed to create file - exiting")
logging.error("Could not create file under : " + zip_file_name + " exiting")
exit()
if __name__ == '__main__':
## Log file parameters ##
logFile = '/tmp/output.log'
logging.basicConfig(filename=logFile, filemode='w', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
## Program variables ##
files = {'a', 'b', 'c', 'd'}
file_extension = '.txt'
file_path = '/tmp/' #define the path were txt files will be created
version = os.getenv('VERSION')
zip_file_path = '/zip/' #define the path were zip files will be created
## Check if env Version exists
if os.getenv('VERSION') is not None:
print(version)
version = os.getenv('VERSION')
logging.info("Running version is: " + version)
else:
print("env VERSION is not defined - exiting")
logging.error("env VERSION is not defined - exiting")
exit()
# ## Create folder if are not exists - This is required when running the script as standalone
# Path(file_path).mkdir(parents=True, exist_ok=True)
# Path(zip_file_path).mkdir(parents=True, exist_ok=True)
# os.chmod(zip_file_path, 0o777)
for filename in files:
full_file_name = file_path+filename+file_extension
create_file(full_file_name)
zip_file_name = zip_file_path+filename+"_"+version+".zip"
create_zip(zip_file_name, full_file_name)