-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backup Pythonista.py
54 lines (47 loc) · 1.89 KB
/
Backup Pythonista.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
# coding: utf-8
'''Creates a zip archive of your Pythonista files and serves them via HTTP in your local network.'''
import sys
import datetime
if sys.version_info[0] >= 3:
from http.server import SimpleHTTPRequestHandler, HTTPServer
else:
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import os, shutil, tempfile, console
import socket
PORT = 8080
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 80))
ip_address = s.getsockname()[0]
s.close()
return ip_address
def main():
now = datetime.datetime.now()
ymd = now.strftime('%Y-%m-%d')
doc_path = os.path.expanduser('~/Documents')
os.chdir(doc_path)
backup_path = os.path.join(doc_path, 'Backup-'+ymd+'.zip')
if os.path.exists(backup_path):
os.remove(backup_path)
print('Creating backup archive...')
shutil.make_archive(os.path.join(tempfile.gettempdir(), 'Backup-'+ymd), 'zip')
shutil.move(os.path.join(tempfile.gettempdir(), 'Backup-'+ymd+'.zip'), backup_path)
print('Backup archive created, starting HTTP server...\n')
local_url = 'http://localhost:{}/Backup-{}.zip'.format(PORT, ymd)
wifi_url = 'http://%s:%i/Backup-%s.zip' % (get_ip_address(), PORT, ymd)
server = HTTPServer(('', PORT), SimpleHTTPRequestHandler)
console.clear()
print('You can tap the following link to open the backup Zip archive in Safari (from where you can transfer it to other apps on this device):')
console.write_link(local_url + '\n', 'safari-' + local_url)
print('\nIf you want to transfer the backup to another device in your local WiFi network, enter the following URL in a web browser on the other device:')
print(wifi_url)
print('\n====\nTap the stop button in the editor or console when you\'re done.')
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
server.socket.close()
print('Server stopped')
if __name__ == '__main__':
main()