-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
This directory contains a number of example command line apps. | ||
They are intentionally very simple and not exactly a finished product, but | ||
all these examples are completely functional. | ||
You typically run then from the command line with the following: | ||
python -m fs.examples.SCRIPT | ||
See the docstrings for details. | ||
Note that if the command requires a path, a FS URL will also work. So the | ||
script will work with archives / servers etc. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Display how much storage is used in your Python files. | ||
Usage: | ||
python -m fs.examples.count_py <PATH or FS URL> | ||
""" | ||
|
||
import sys | ||
|
||
from fs import open_fs | ||
from fs.filesize import traditional | ||
|
||
|
||
fs_url = sys.argv[1] | ||
count = 0 | ||
|
||
with open_fs(fs_url) as fs: | ||
for _path, info in fs.walk.info(filter=["*.py"], namespaces=["details"]): | ||
count += info.size | ||
|
||
print(f'There is {traditional(count)} of Python in "{fs_url}"') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Find an print paths to files with identical contents. | ||
Usage: | ||
python -m fs.examples.find_dups <PATH or FS URL> | ||
""" | ||
|
||
from collections import defaultdict | ||
import hashlib | ||
import sys | ||
|
||
from fs import open_fs | ||
|
||
|
||
def get_hash(bin_file): | ||
"""Get the md5 hash of a file.""" | ||
file_hash = hashlib.md5() | ||
while True: | ||
chunk = bin_file.read(1024 * 1024) | ||
if not chunk: | ||
break | ||
file_hash.update(chunk) | ||
return file_hash.hexdigest() | ||
|
||
|
||
hashes = defaultdict(list) | ||
with open_fs(sys.argv[1]) as fs: | ||
for path in fs.walk.files(): | ||
with fs.open(path, "rb") as bin_file: | ||
file_hash = get_hash(bin_file) | ||
hashes[file_hash].append(path) | ||
|
||
for paths in hashes.values(): | ||
if len(paths) > 1: | ||
for path in paths[1:]: | ||
print(f" {path}") | ||
print() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Remove all pyc files in a directory. | ||
Usage: | ||
python -m fs.examples.rm_pyc <PATH or FS URL> | ||
""" | ||
|
||
import sys | ||
|
||
from fs import open_fs | ||
|
||
|
||
with open_fs(sys.argv[1]) as fs: | ||
count = fs.glob("**/*.pyc").remove() | ||
print(f"{count} .pyc files remove") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Upload a file to a server (or other filesystem) | ||
Usage: | ||
python -m fs.examples.upload FILENAME <FS URL> | ||
example: | ||
python -m fs.examples.upload foo.txt ftp://example.org/uploads/ | ||
""" | ||
|
||
import os | ||
import sys | ||
|
||
from fs import open_fs | ||
|
||
_, file_path, fs_url = sys.argv | ||
filename = os.path.basename(file_path) | ||
|
||
with open_fs(fs_url) as fs: | ||
if fs.exists(filename): | ||
print("destination exists! aborting.") | ||
else: | ||
with open(file_path, "rb") as bin_file: | ||
fs.upload(filename, bin_file) | ||
print("upload successful!") |