Filesystem Scripts¶
batch_renamer
¶
Renames a directory of files based on a template.
First we’ll examine the contents of a directory and ensure it only contains files with names we want to rename.
>>> os.listdir("/path/to/dir")
['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
As we can see it does we’ll then invoke the script like so.
python3 batch_renamer.py /path/to/dir
img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpg
-
class
pyutil.batch_renamer.
BatchRename
(template)[source]¶ Bases:
string.Template
Delimiter for string substitutions.
-
delimiter
= '%'¶
-
pattern
= re.compile('\n %(?:\n (?P<escaped>%) | # Escape sequence of two delimiters\n (?P<named>(?a:[_a-z][_a-z0-9]*)) | # delimiter and a Python identifier\n {(?P<braced>(?a:[_a-z][_a-z0-9]*)), re.IGNORECASE|re.VERBOSE)¶
-
-
pyutil.batch_renamer.
batch_mover
(pattern, new_pattern, directory=None)[source]¶ Move files in the current working directory that match a pattern.
dir_cleaner
¶
Deletes extraneous files.
Without frequent monitoring, directories like /tmp and /var/log can frequently grow to sizes that are difficult to manage because of clutter and files.
However, there has to be a middle ground between deleting thousands of files one by one and rm -rf /tmp/.
This module attempts that.
Initially tested on the android app Termux, this specifically deletes directories with only month old sockets.
$PREFIX
/tmp/nvim
$PREFIX
/tmp/ssh
Note
On Ubuntu the big one is /var/log/journal so we might need to
remind the user for credentials. getpass.getpass()
?
In addition, it felt like a good way to get more familiar with the new
pathlib
module.
Concrete Roadmap¶
Up until now this has been fairly general and quite abstract.
Let’s come up with a few pre-planned and clear endpoints.
One way to implement this module would be:
Compare packages that are installed to what’s contained /usr/share/doc. I have like 4500 folders in that directory and nowhere near that many packages installed. Cruft and accumulation frequently doesn’t get cleared out of there correctly.
-
pyutil.dir_cleaner.
clean
(ftype='*.pyc', recursive=False)[source]¶ Remove all pyc files. Add input for filetype later.
- Parameters
ftype (filetype) – File to iterately remove.
recursive (Bool, Optional) – Whether to search the directory recursively or not.
-
pyutil.dir_cleaner.
rmtree
(path, ignore_errors=False, onerror=None)[source]¶ Returns
shutil.rmtree()
.
dlink
¶
Symlink all of the files in one directory into another.
Synopsis¶
This module can be used to create individual symlinks to every file in a directory. This is a huge convenience wen symlinking dotfiles or configuration files held in a different location than where the software of interest expects it.
This is quite easily one of my most frequently used scripts.
Usage¶
ln -s path/to/dest/* [path/to/src]
As an example, one can git clone dotfiles
in a directory named projects
or src
. The location of the git repository is irrelevant, and as such, we’ll
refer to it as src
from here, as dest
as it’s where the symlinks point to.
For example if wants symlinks pointing to /home/User/dotfiles/.vim
,
then running dlink.py in /home/User/.vim
with $HOME/dotfiles/.vim
as
an argument will create symlinks in $HOME/.vim
pointing to
$HOME/dotfiles/.vim
.
Inspiration¶
If the module is given 2 args, the intended response is for it to behave similarly to the classic Unix idiom:
ln -s path/to/dest/* [path/to/src]
or treat it similarly to os.symlink()
.
dlink2
¶
This is a rewrite of a script I’ve had for years, so I decided to go above and beyond.
The idea behind it was to create something that would easily allow for creating arbitrarily nested trees of symlinks.
Todo
Sep 17, 2019: Recursive option
Currently doesnt work.
Todo
literally what
$: dlink2.py ~/projects/viconf/.config/nvim/rplugin/python3
Traceback (most recent call last):
File "/data/data/com.termux/files/home/bin/dlink2.py", line 233, in <module>
main()
File "/data/data/com.termux/files/home/bin/dlink2.py", line 210, in main
args = user_arguments.parse_args()
AttributeError: 'NoneType' object has no attribute 'parse_args'
See also
IPython.utils.path.ensure_dir_exists()
functionCheck for a dir and create it if it doesn’t exist.
-
exception
pyutil.dlink2.
PermissionsError
[source]¶ Bases:
OSError
Symlinking error typically from Windows.
-
pyutil.dlink2.
dlink
(destination_dir, source_dir, is_recursive=False, glob_pattern=None)[source]¶ Symlink user provided files.
The module doesn’t immediately check for correct permissions or operating system.
As a result, the onus is put on the user to ensure that the necessary requirements per OS are met.
Namely on Windows 10, that if symlinks are allowed on the filesystem, whether they can only be created by an administrator. Recent enough versions of Windows 10 have introduced the ability for regular users to create symlinks as well as admins.
- Parameters
destination_dir (str) – Directory where symlinks point to.
source_dir (str, optional) – Directory where symlinks are created.
recursive (bool, optional) – Whether to recursively symlink directories beneath the
destination_dir
. Defaults to False.glob_pattern (str) – Only symlink files that match a certain pattern.
linktree
¶
From the package “python3.6-examples” in the Ubuntu repositories.
Minor modifications for flake8
, pydocstyle
etc.
Mar 10, 2019:
Added logging.
Make a copy of a directory tree with symbolic links to all files in the original tree.
All symbolic links go to a special symbolic link at the top, so you can easily fix things if the original source tree moves.
This would probably get a huge improvement in readability from pathlib.
See also
mkreal
mv_to_repo
¶
Move files from the home directory to the dotfiles repo.
This is a script I’ve been using for the better part of a year, so while the docstring formatting isn’t consistent and there are a couple odd sections, this script has served a very utilitiarian purpose.
May refactor one day. But it continues to work.
Note
This module assumes a python interpreter above version 3.4.
-
pyutil.mv_to_repo.
backup_file
(src)[source]¶ Backs up file src. Utilizes
shutil.copy2()
.- Parameters
src (str) – File to backup
-
pyutil.mv_to_repo.
main
()[source]¶ Dispatch the remaining implementation of the module.
Determine if a file name is in the current directory or absolute path.
Then set up a relative path from
$HOME
. Use the root of the repo as the new root and move the file there, all while creating directories and backups.Runs checks, calls func to backup file
src
, moves it to the dotfiles repo and symlinks it.Moves file to a hardcoded path but will be generalized to take as an argument.
Assumes
User runs the script from inside the folder of the file they want to move.