-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemo.py
130 lines (104 loc) · 2.84 KB
/
demo.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
# -*- coding: utf-8 -*-
import os
import sys
welcome = """
**************************
Thanks for using d2pi
this is a demo
Thanks to my girlfriend
without you I would make
it faster.
But I still love you. :)
**************************
"""
downloader = """
And this is downloader
Downloader will only download files
Anything you do in local will do nothing to server.
"""
uploader = """
And this is uploader
Uploader will only upload files.
"""
auto_downloader = """
This is default watcher
This watcher will watch all events in local like:
- NEW FILE/DIR
- DELETE FILE/DIR
- MOVE FILE/DIR
And it will auto download files to local.
I will cache downloaded files and will be flushed
when file statue changed.
It have a simple lock, if upload is working,
download will be blocked.
The same, if auto download is working, then
new file could create but upload event will be block.
Because this is a simple tool, will not connect to
any server and not be pushed by any server
So we don't have better way to solve that problem.
Why don't we add a queue and insert event to a queue?
This tool is using watchdog, every time download a file
it will cause a create file event. Then we will upload
file again, with same content.
"""
simple_watcher = """
This is the watcher with no auto download.
It will download and sync to server at first time
Then it will not download automatically.
This watcher will watch all events in local like:
- NEW FILE/DIR
- DELETE FILE/DIR
- MOVE FILE/DIR
"""
help = """
Try:
downloader: demo.py -d
uploader: demo.py -u
watcher: demo.py -w
"""
def demo():
print(welcome)
args = sys.argv
args = args[1:]
print(help)
from d2pi.config import config
print(config.is_useable())
if not config.is_useable():
from d2pi.auth import warn
warn(overwrite=True)
print('Done! Run again to auth!')
elif not config.has_token():
from d2pi.auth import auth
auth()
print('Success! Run demo again!')
else:
if args and args[0] == '-d':
from d2pi.watch import downloader
downloader.run()
elif args and args[0] == '-u':
from d2pi.watch import uploader
uploader.run()
elif args and args[0] == '-w':
from d2pi.watch import watcher
watcher.run()
else:
from d2pi.watch import watcher
watcher.auto_download = False
watcher.run()
if __name__ == '__main__':
try:
pid = str(os.getpid())
pidfile = '/tmp/d2pi.pid'
if os.path.isfile(pidfile):
print('d2pi already exists, exiting %s' % pidfile)
sys.exit()
else:
open(pidfile, 'w').write(pid)
demo()
except Exception as e:
print(e)
finally:
try:
os.unlink(pidfile)
except:
pass