This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_flatfiles.py
64 lines (56 loc) · 1.85 KB
/
generate_flatfiles.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
#!/usr/bin/env python
# -- coding: utf-8 --
import sqlite3
from hashlib import md5
import filetype as ft
import directorytools as dt
import unpack
import feedtoflatfiles as ftff
from os import listdir
from shutil import make_archive, move, rmtree
FEED_DIR = ""
DB_LOC = ""
TEMP_DIR = "temp/"
def setup_db(conn):
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS file_data (file_name TEXT, hash TEXT)")
def has_changed(conn, fname):
cur = conn.cursor()
cur.execute("SELECT hash FROM file_data WHERE file_name = '" + fname + "'")
new_hash = file_hash(fname)
old_vals = cur.fetchone()
if not old_vals:
cur.execute("INSERT INTO file_data (file_name, hash) VALUES('" + fname + "','" + new_hash + "')")
conn.commit()
return True
elif old_vals[0] != new_hash:
cur.execute("UPDATE file_data SET hash = '" + new_hash + "' WHERE file_name = '" + fname + "'")
conn.commit()
return True
return False
def file_hash(fname):
m = md5()
with open(fname, "rb") as fh:
for data in fh.read(8192):
m.update(data)
return m.hexdigest()
conn = sqlite3.connect(DB_LOC)
setup_db(conn)
folders = listdir(FEED_DIR)
for f in folders:
files = listdir(FEED_DIR + f)
for fname in files:
if fname.startswith("vipFeed") and fname.split(".")[0].endswith("2012-11-06"):
fullpath = FEED_DIR + f + "/" + fname
if has_changed(conn, fullpath):
flatfiledir = fname.split(".")[0] + "_flatfiles/"
dt.clear_or_create(flatfiledir)
dt.clear_or_create(TEMP_DIR)
unpack.unpack(fullpath, TEMP_DIR)
unpack.flatten_folder(TEMP_DIR)
xml_file = dt.file_by_extension(".xml", TEMP_DIR)
ftff.feed_to_db_files(flatfiledir, xml_file)
make_archive(fname.split(".")[0] + "_flatfiles", "zip", flatfiledir)
move(fname.split(".")[0] + "_flatfiles.zip", FEED_DIR + f + "/" + fname.split(".")[0] + "_flatfiles.zip")
rmtree(TEMP_DIR)
rmtree(flatfiledir)