Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 164 lines (142 sloc) 6.2 KB
#!/usr/bin/python3
import time
import traceback
import os
import shutil
mountPoint = "/run/media/bredol/DPT-S1"
bibPath = os.path.expanduser("~/Documents/bib")
notesPath = os.path.expanduser("~/Documents/notes")
ticketsPath = os.path.expanduser("~/Documents/tickets")
reportsPath = os.path.expanduser("~/Documents/reports/rolling")
#recentCount = 20
def moveFile(src, tar):
os.makedirs(os.path.dirname(tar), exist_ok=True)
os.rename(src, tar)
def copyIfNewer(src, tar):
if not os.path.exists(tar) or os.stat(tar).st_mtime < os.stat(src).st_mtime:
os.makedirs(os.path.dirname(tar), exist_ok=True)
shutil.copy2(src, tar)
while True:
try:
if os.path.exists(mountPoint + "/note"):
#================================================================================
print("placing busy file...")
open(mountPoint+"/busy", "w").close()
#================================================================================
print("save notes to harddrive...")
os.chdir(mountPoint + "/note")
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".pdf"):
copyIfNewer(root+"/"+f, notesPath+"/"+root+"/"+f)
#================================================================================
print("move notes not existing on the tablet to deleted folder...")
os.chdir(notesPath)
for root, dirs, files in os.walk("."):
if root.startswith("./deleted"):
continue
if root.startswith("./bib"):
srcFolder = root[5:]
else:
srcFolder = "note/"+root
for f in files:
if not os.path.exists(mountPoint+"/"+srcFolder+"/"+f):
moveFile(root+"/"+f, "./deleted/"+root+"/"+f)
#================================================================================
print("copy bib to tablet...")
os.chdir(bibPath)
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".pdf") and not os.path.exists(mountPoint+"/"+root+"/"+f):
copyIfNewer(root+"/"+f, mountPoint+"/"+root+"/"+f)
#================================================================================
print("copy tickets to tablet...")
os.chdir(ticketsPath)
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".pdf") and not os.path.exists(mountPoint+"/"+root+"/"+f):
copyIfNewer(root+"/"+f, mountPoint+"/tickets/"+root+"/"+f)
#================================================================================
print("remove non existing tickets from tablet...")
os.makedirs(mountPoint+"/tickets", exist_ok=True)
os.chdir(mountPoint+"/tickets")
for root, dirs, files in os.walk("."):
for f in files:
if not os.path.exists(ticketsPath+"/"+root+"/"+f):
os.remove(root+"/"+f)
#================================================================================
print("copy reports to tablet...")
os.chdir(reportsPath)
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".pdf") and not os.path.exists(mountPoint+"/"+root+"/"+f):
copyIfNewer(root+"/"+f, mountPoint+"/reports/"+root+"/"+f)
#================================================================================
print("remove non existing reports from tablet...")
os.makedirs(mountPoint+"/reports", exist_ok=True)
os.chdir(mountPoint+"/reports")
for root, dirs, files in os.walk("."):
for f in files:
if not os.path.exists(reportsPath+"/"+root+"/"+f):
os.remove(root+"/"+f)
#================================================================================
print("copy papers with annotations to annotations folder...")
os.chdir(mountPoint)
for root, dirs, files in os.walk("."):
if (root.startswith("./note")
or root.startswith("./old")
or root.startswith("./tickets")
or root.startswith("./reports")):
#or root.startswith("./recent"):
continue
for f in files:
if f.endswith(".pdf"):
bibFile = bibPath+"/"+root+"/"+f
if not os.path.exists(bibFile) or os.stat(bibFile).st_mtime < os.stat(root+"/"+f).st_mtime:
copyIfNewer(root+"/"+f, notesPath+"/bib/"+root+"/"+f)
#================================================================================
#print("populate 'recent' directory...")
#os.chdir(mountPoint)
#filesList = []
#for root, dirs, files in os.walk("."):
# for f in files:
# if f.endswith(".pdf"):
# try:
# recentness = time.mktime(time.strptime(f[:7], "%Y_%m"))
# except ValueError:
# recentness = 0
#
# recentness = max(recentness, os.stat(root+"/"+f).st_mtime)
# filesList.append((recentness, root+"/"+f))
#
#if os.path.exists(".recent"):
# shutil.rmtree(".recent")
#os.makedirs(".recent", exist_ok=True)
#
#for i, (_, path) in enumerate(sorted(filesList)[-recentCount:]):
# copyIfNewer(path, ".recent/{:02}_{}".format(i, os.path.basename(path)))
#
#if os.path.exists("recent"):
# shutil.rmtree("recent")
#
#os.rename(".recent", "recent")
#
#================================================================================
print("remove empty directories in bib...")
cleanComplete = False
while not cleanComplete:
cleanComplete = True
for cleanDir in (mountPoint, bibPath, notesPath):
os.chdir(cleanDir)
for root, dirs, files in os.walk("."):
if len(dirs) == 0 and len(files) == 0:
os.rmdir(root)
cleanComplete = False
#================================================================================
print("removing busy file...")
os.remove(mountPoint+"/busy")
#================================================================================
print("go back to sleep...")
except:
traceback.print_exc()
time.sleep(300)