Skip to content

Commit

Permalink
update: tmp file remove
Browse files Browse the repository at this point in the history
  • Loading branch information
BoYanZh committed Aug 24, 2020
1 parent 4913489 commit 9e27734
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ settings.json
.vscode
__pycache__
courses/
upload.sh
21 changes: 12 additions & 9 deletions canvassyncer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from threading import Thread
from urllib3.util.retry import Retry
from tqdm import tqdm
import ntpath

__version__ = pkg_resources.require("canvassyncer")[0].version
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
Expand Down Expand Up @@ -54,7 +55,7 @@ def downloadFile(self, i, queue):
queue.put([src, dst])
break
self.currentDownload[i] = dst.split('/')[-1].split('\\')[-1]
tryTime = 0
tmpFilePath = ''
try:
r = self.sess.get(src, timeout=10, stream=True)
tmpFilePath = f"{dst}.tmp.{int(time.time())}"
Expand All @@ -64,17 +65,14 @@ def downloadFile(self, i, queue):
self.tqdm.update(len(chunk))
fd.write(chunk)
if self.stopSignal:
raise MultithreadDownloaderStop
break
os.rename(tmpFilePath, dst)
except MultithreadDownloaderStop as e:
if os.path.exists(tmpFilePath):
os.remove(tmpFilePath)
except Exception as e:
print(
f"\nError: {e.__class__.__name__}. Download {dst} fails!")
finally:
if os.path.exists(tmpFilePath):
os.remove(tmpFilePath)
finally:
with self.countLock:
self.downloadedCnt += 1

Expand Down Expand Up @@ -118,6 +116,7 @@ def finished(self):

def waitTillFinish(self):
word = 'None'
downloadingFileName = ''
while not self.finished:
for fileName in reversed(self.currentDownload):
if fileName:
Expand Down Expand Up @@ -364,7 +363,10 @@ def checkLaterFiles(self):
localCreatedTimeStamp = int(os.path.getctime(path))
try:
try:
os.rename(path, f"{path}.{localCreatedTimeStamp}")
newPath = os.path.join(
ntpath.dirname(path),
f"{localCreatedTimeStamp}_{ntpath.basename(path)}")
os.rename(path, newPath)
except Exception as e:
os.remove(path)
laterFiles.append((fileUrl, path))
Expand Down Expand Up @@ -437,6 +439,7 @@ def initConfig():


def run():
Syncer = None
try:
parser = argparse.ArgumentParser(
description='A Simple Canvas File Syncer')
Expand Down Expand Up @@ -472,9 +475,9 @@ def run():
f"\nUnexpected Error: {e.__class__.__name__}. Please check your network and your token!"
)
raise e
exit(1)
except KeyboardInterrupt as e:
Syncer.downloader.stop()
if Syncer:
Syncer.downloader.stop()
exit(1)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='canvassyncer',
version='1.2.0',
version='1.2.1',
description='A canvas file syner',
url='https://github.com/BoYanZh/Canvas-Syncer',
author='SJTU JI Tech',
Expand Down

0 comments on commit 9e27734

Please sign in to comment.