-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler.py
99 lines (87 loc) · 2.75 KB
/
filehandler.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
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
import os, re, sys, getopt
from File import File
from Node import Node
PATH = os.path.dirname(os.path.abspath(__file__))
INPUT_PATH = PATH + "/data-input/"
OUTPUT_PATH = PATH + "/data-output/"
def parse(argv):
inputFileName = ""
nodeFileName = ""
outputFileName =""
try:
opts, args = getopt.getopt(argv, "hf:n:o:", ["ffile=", "nfile=", "ofile="])
if len(argv)> 6 and '-h' not in argv:
print("Error: Too many arguments")
sys.exit(2)
except getopt.GetoptError:
print('solution.py -f <inputFile> -n <inputNode> -o <outputFile> -h ')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('solution.py -f <inputFile> -n <inputNode> -o <outputFile> -h ')
sys.exit()
elif opt in ("-f", "--ffile"):
inputFileName = arg
elif opt in ("-n","--nfile"):
nodeFileName = arg
elif opt in ("-o", "--ofile"):
outputFileName = arg
if inputFileName == '' and nodeFileName == '':
print("Error: <inputFile> and <inputNode> missing ")
sys.exit()
elif inputFileName == '':
print("Error: <inputFile> missing ")
sys.exit()
elif nodeFileName == '' or nodeFileName == '-o':
print("Error: <inputNode> missing ")
sys.exit()
return [inputFileName, nodeFileName,outputFileName]
def buildArrays(filename,nodename):
try:
fileArray = []
nodeArray = []
#build file array
with open(INPUT_PATH + filename, 'r') as rf1:
#to avoid memory issues with large files, read input per line
for line in rf1:
line = line.strip()
# check if line is empty or contains a #
if line:
if(line.startswith('#')):
continue
#data of interest could be file or node
file_info = line.split(" ")
fileInstance = File(file_info[0], file_info[1])
fileArray.append(fileInstance)
#build node array
with open(INPUT_PATH + nodename, 'r') as rf2:
#to avoid memory issues with large files, read input per line
for line in rf2:
line = line.strip()
# check if line is empty or contains a #
if line:
if(line.startswith('#')):
continue
#data of interest could be file or node
file_info = line.split(" ")
nodeInstance = Node(file_info[0], file_info[1])
nodeArray.append(nodeInstance)
#returns tuple of filearry and node array
return (fileArray,nodeArray)
except:
print("Error: Rows of input file should be of the format <filename> <filesize>")
sys.exit()
def output(inFileArray,outputFileName):
for f in inFileArray:
if(f.location == "unknown"):
f.location = "NULL"
if outputFileName == "":
for f in inFileArray:
print(f.name + " " + f.location)
else:
try:
with open(OUTPUT_PATH + outputFileName, 'w') as outFile:
for f in inFileArray:
outFile.write(f.name + " " + f.location + "\n")
except:
print("Error: Unable to write to file <" + outputFileName + ">")