-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdrm
executable file
·55 lines (48 loc) · 1.65 KB
/
sdrm
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
#! /usr/bin/env python3
# delete files from Toshiba FlashAir SD card via wi-fi
# see https://www.flashair-developers.com/en/documents/api/
import os
import sys
import fnmatch
import argparse
import configparser
import urllib.parse
import flashair
import httpx
def match(s):
if s in card.args.files:
return True
for arg in card.args.files:
if fnmatch.fnmatch(s, arg):
return True
return False
card=flashair.card()
card.parser.add_argument('-d', '--directory', help='directory', default=card.def_directory)
card.parser.add_argument('files', nargs='+', help='files/directories to delete', default=None)
card.parser.add_argument('-i', '--interactive', help='prompt before every removal', default=False, action='store_true')
card.setup()
card.get_files()
if len(card.files) == 0:
print('no files to delete in', dir)
sys.exit(1)
# keep the host from writing during changes
httpx.get('http://'+card.address+'/upload.cgi?WRITEPROTECT=ON')
# delete matching files
if card.dir[-1] != '/':
card.dir+='/'
for file, size, attrib, date in card.files:
if match(file):
if card.args.interactive:
if attrib & 16:
choice=input("remove directory '"+file+"'? ")
else:
choice=input("remove regular file '"+file+"'? ")
if choice[0].lower() != 'y':
continue
r=httpx.get('http://'+card.address+'/upload.cgi?DEL='+card.dir+urllib.parse.quote_plus(file))
if r.text != 'NG':
print(file, 'deleted')
else:
print('unable to delete', file)
# re-enable disk
httpx.get('http://'+card.address+'/upload.cgi?WRITEPROTECT=OFF')