Skip to content

Commit e2106d0

Browse files
authored
Examples (#252)
* Examples * upload script * moved examples * tidy
1 parent 9be803c commit e2106d0

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

examples/README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This directory contains a number of example command line apps using PyFilesystem.
2+
3+
They are intended to be a learning aid and not exactly finished products, but all these examples are completely functional.

examples/count_py.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Display how much storage is used in your Python files.
3+
4+
Usage:
5+
python count_py.py <PATH or FS URL>
6+
7+
"""
8+
9+
import sys
10+
11+
from fs import open_fs
12+
from fs.filesize import traditional
13+
14+
15+
fs_url = sys.argv[1]
16+
count = 0
17+
18+
with open_fs(fs_url) as fs:
19+
for _path, info in fs.walk.info(filter=["*.py"], namespaces=["details"]):
20+
count += info.size
21+
22+
print(f'There is {traditional(count)} of Python in "{fs_url}"')

examples/find_dups.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Find paths to files with identical contents.
3+
4+
Usage:
5+
6+
python find_dups.py <PATH or FS URL>
7+
8+
"""
9+
10+
from collections import defaultdict
11+
import hashlib
12+
import sys
13+
14+
from fs import open_fs
15+
16+
17+
def get_hash(bin_file):
18+
"""Get the md5 hash of a file."""
19+
file_hash = hashlib.md5()
20+
while True:
21+
chunk = bin_file.read(1024 * 1024)
22+
if not chunk:
23+
break
24+
file_hash.update(chunk)
25+
return file_hash.hexdigest()
26+
27+
28+
hashes = defaultdict(list)
29+
with open_fs(sys.argv[1]) as fs:
30+
for path in fs.walk.files():
31+
with fs.open(path, "rb") as bin_file:
32+
file_hash = get_hash(bin_file)
33+
hashes[file_hash].append(path)
34+
35+
for paths in hashes.values():
36+
if len(paths) > 1:
37+
for path in paths:
38+
print(f" {path}")
39+
print()
40+

examples/rm_pyc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Remove all pyc files in a directory.
3+
4+
Usage:
5+
6+
python rm_pyc.py <PATH or FS URL>
7+
8+
"""
9+
10+
import sys
11+
12+
from fs import open_fs
13+
14+
15+
with open_fs(sys.argv[1]) as fs:
16+
count = fs.glob("**/*.pyc").remove()
17+
print(f"{count} .pyc files remove")

examples/upload.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Upload a file to a server (or other filesystem)
3+
4+
Usage:
5+
6+
python upload.py FILENAME <FS URL>
7+
8+
example:
9+
10+
python upload.py foo.txt ftp://example.org/uploads/
11+
12+
13+
"""
14+
15+
import os
16+
import sys
17+
18+
from fs import open_fs
19+
20+
_, file_path, fs_url = sys.argv
21+
filename = os.path.basename(file_path)
22+
23+
with open_fs(fs_url) as fs:
24+
if fs.exists(filename):
25+
print("destination exists! aborting.")
26+
else:
27+
with open(file_path, "rb") as bin_file:
28+
fs.upload(filename, bin_file)
29+
print("upload successful!")

0 commit comments

Comments
 (0)