-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir_creator.py
203 lines (144 loc) · 6.06 KB
/
dir_creator.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import sys
import sqlite3
import hashlib
def db_check():
try:
open('psps.db')
return True
#database does exist
except IOError as e:
if e.args[0] == 2:
print "This database doesn't exist."
sys.exit(1)
#if database doesn't exist, warn user and quit
else:
print e
sys.exit(1)
if db_check():
db = sqlite3.connect('psps.db')
cursor = db.cursor()
#gets location system of directories will be droped:
print "A system of directories and files with the information of the psps database will be created."
user_location = raw_input("Where do you want put the directories?")
if user_location == "":
user_location = os.getcwd()
else:
while not os.path.isdir(user_location):
print "Invalid path given. Try again."
user_location = raw_input("Where do you want to put the system of directories?")
#checks to make sure the user gave a valid file and doesn't let them continue until they enter a valid one
print user_location
#makes list of md5_abinet's:
cursor.execute( ''' SELECT md5_abinet FROM pseudos ''')
md5_raw_list = cursor.fetchall()
md5_abinet_list = []
for one_md5 in md5_raw_list:
if one_md5[0] != None:
md5 = one_md5[0].encode('ascii', 'ignore')
md5_abinet_list.append(md5)
print "List of current md5's: "
print md5_abinet_list
print "\n"
#grabs all md5's from table
def file_creator(type):
#takes string input of file name like "opts"
type_name = type + "_name"
cursor.execute( ''' SELECT ''' + type_name + ''' FROM pseudos WHERE md5_abinet=? ''', (md5_abinet,))
#have to connect strings because ? doesn't work for column or table names
retrieved = cursor.fetchone()[0]
if retrieved is None:
print "There is no " + type + "\n"
info_location = os.path.join(md5_directory, "info.txt")
with open(info_location, "a") as info:
info.write("\n")
#for that file, just a line return is entered in info.txt
else:
name = retrieved.encode('ascii', 'ignore')
print type + " file name is: " + name
cursor.execute( ''' SELECT ''' + type + ''' FROM pseudos WHERE md5_abinet=? ''', (md5_abinet,))
retrieved = cursor.fetchall()[0]
database_text = str(retrieved[0])
#gets file name and text from database and saves it as variables
location = os.path.join(md5_directory, name)
with open(location, "w") as one_file:
one_file.write(database_text)
print name + " was written.\n"
#writes found information to a created file with found name
info_location = os.path.join(md5_directory, "info.txt")
with open(info_location, "a") as info:
info.write(name + "\n")
print "info.txt was written.\n"
#for each file created, that file name is added to info.txt
#to write files if an entry exists in the database
def file_creator_main(column_name, name):
if column_name == "citation":
cursor.execute('''SELECT citation FROM pseudos WHERE md5_abinet=?''', (md5_abinet,))
retrieved = cursor.fetchall()[0]
value = str(retrieved[0])
#for citation, fetchall() should be used, and it's grabbed from pseudo table
print name + " was written.\n"
#for citation, text is grabbed from pseudos and then written to a file
elif column_name == "ppot":
cursor.execute('''SELECT ppot FROM pseudos WHERE md5_abinet=?''', (md5_abinet,))
retrieved = cursor.fetchall()[0]
if retrieved[0] is None or retrieved[0] is " " or retrieved[0] is "\n" or retrieved[0] is "":
print "ppot will not be written."
value = "ppot not exist"
#info.txt for ppot:
info_location = os.path.join(md5_directory, "info.txt")
with open(info_location, "a") as info:
info.write("\n")
else:
value = str(retrieved[0])
print name + " was written.\n"
#info.txt for ppot:
info_location = os.path.join(md5_directory, "info.txt")
with open(info_location, "a") as info:
info.write("ppot\n")
#for ppot, text is grabbed from pseudos and then written to a file
else:
cursor.execute( ''' SELECT ''' + column_name + ''' FROM main WHERE id=? ''', (id,))
retrieved = cursor.fetchone()[0]
#gets information for column asked for, for the specific id
if column_name == "semicore":
value = retrieved.encode('ascii', 'ignore')
else:
value = str(retrieved)
#for semicore, string needs to be converted, for others, int needs to be changed to a string
print value + " was written to " + name + " file.\n"
#gets information for stuff from main and then writes that file
if value != "ppot not exist":
new_file_location = os.path.join(md5_directory, name)
with open(new_file_location, "w") as new:
new.write(value)
#writes info to file
#for znucl, qf, semicore, and cite their files are written
for md5_abinet in md5_abinet_list:
print "\nmd5_abinet: " + md5_abinet
md5_directory = os.path.join(user_location, md5_abinet)
os.makedirs(md5_directory)
#makes a directory with name '[md5 abinet]' using list of md5's
cursor.execute( ''' SELECT id FROM pseudos WHERE md5_abinet=? ''', (md5_abinet,))
id = cursor.fetchone()[0]
print "id: " + str(id)
#grabs id using md5 so that id can be used for main table
#znucl, quality, semicore:
file_creator_main("z", "znucl")
file_creator_main("qf", "quality")
file_creator_main("semicore", "semicore")
#pseudo files:
file_creator("abinet")
file_creator("upf")
file_creator("fill")
file_creator("opts")
#citation:
file_creator_main("citation", "cite")
#info.txt for citation:
info_location = os.path.join(md5_directory, "info.txt")
with open(info_location, "a") as info:
info.write("cite\n")
#ppot:
file_creator_main("ppot", "ppot")
#writes all of the files in each md5_abinet directory
db.close()