-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '8f77775d77f23ddf2248783e7c5b087c1eb09573' as 'pdf-book-…
…manager'
- Loading branch information
Showing
15 changed files
with
229 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 @@ | ||
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,82 @@ | ||
#+TITLE: Documentation | ||
* Intro | ||
#+BEGIN_QUOTE | ||
Centralizamos los comentarios sobre las implementaciones en los Makefiles, | ||
para separar la explicación de la implementación en si | ||
#+END_QUOTE | ||
* Funciones de Sustitución | ||
#+BEGIN_SRC makefile | ||
libros = $(wildcard *.pdf) | ||
archivos= $(wildcard *.tar.gz) | ||
|
||
libros_comprimidos = $(libros:.pdf=.tar.gz) | ||
|
||
# - Alternativa de sustitución con la función patsubst | ||
libros_comprimidos = $(patsubst %.pdf, %.tar.gz, $(libros)) | ||
#+END_SRC | ||
* Macro especiales | ||
#+BEGIN_SRC makefile | ||
# 1. $@ es una macro especial que toma el valor del target/objetivo | ||
# 2. $< es una macro especial que toma la primer dependencia de la regla | ||
%.tar.gz: %.pdf | ||
@echo "Comprimiendo $* en $@ .." | ||
@tar -cvzf $@ $< | ||
#+END_SRC | ||
* Targets con Iteración | ||
#+BEGIN_SRC makefile | ||
libros = $(wildcard *.pdf) | ||
|
||
comprimir-archivos: $(libros_comprimidos) ## | ||
@echo "Listo! archivos comprimidos!" | ||
|
||
# 1. Es común pensar de forma algorítmica, iterar sobre una lista de nombres de libros | ||
# y ejecutar un comando para comprimir/descomprimir pasandole de parámetro el nombre.. | ||
# | ||
# 2. El problema de lo anterior es que se ejecutaría a cada rato comprimiendo/descomprimiendo | ||
# desaprovechando el concepto que tiene GNU Make con los objetivos/target (archivos).. | ||
# es decir "crear el objetivo (archivo) sólo si es necesario", | ||
# | ||
# ésta regla tiene asociada como orden un resultado NO deseado.. | ||
comprimir-archivos: | ||
$(foreach libro, $(libros),\ | ||
tar -cvzf $(libro).tar.gz $(libro);) | ||
#+END_SRC | ||
* Comando ls - Listar directorios | ||
#+BEGIN_SRC makefile | ||
# 1. Para listar archivos con misma extensión de múltiples directorios podemos usar el comando ls pasandole ambas rutas | ||
# por ejemplo ls ruta/*.extension otra-ruta/*.extension | ||
# | ||
# 2. Si el comando ls no encuentra el archivo ó patrón pasado por parámetro, lanzará un mensaje al file descriptor 2 STDERR | ||
# 3. Ocutamos los mensajes de error de ls, redireccionando los mensajes que el comando ls envía al fd 2 stderr al dispositivo nulo /dev/null | ||
# con el operador de redirección > por ejemplo ls /pupi 2>/dev/null | ||
STD_ERR=2 | ||
NULL_DEVICE=/dev/null | ||
|
||
listar-libros: ## | ||
@ls -l *.pdf */*.pdf $(STD_ERR)>$(NULL_DEVICE) | \ | ||
| column -t | ||
#+END_SRC | ||
* Comando rename - Expresiones Regulares | ||
#+BEGIN_SRC makefile | ||
# Breve explicación de los patrones utilizadas con rename | ||
# 1. Transformamos las mayúsculas en minúsculas, reemplazamos los espacios y guión bajo por guión - | ||
# rename -v 'y/A-Z _/a-z-/' *.pdf | ||
# | ||
# 2. Usamos el ^ como una operación de complemento, removiendo todo caracter NO alfanumérico ó que no sea _.- | ||
# rename 's/[^a-zA-Z0-9_.-]//g' *.pdf | ||
# | ||
# Notas: | ||
# 1. si al comando rename combinamos los parámetros -n -v imprimirá como quedarían los nombres de los archivos, | ||
# sin generar efecto sobre los archivos (no los renombra) | ||
# | ||
# 2. alternativa al comando rename + la orden (s) sustitución con expresiones de PERL | ||
# es combinar los comandos tr y rename con el parámetro -f, por ejemplo rename -f 'tr/ A-Z/-a-z/' *.pdf | ||
# | ||
# 3. la orden (s) de sustitución del comando rename es similar al del comando SED | ||
# 4. la órden (y) del comando rename, nos permite transformación de Mayúsculas->Minusculas de manera sencilla | ||
|
||
# renombra los nombres de los archivos | ||
formatear-archivos: | ||
@rename -v 'y/A-Z _/a-z-/' *.pdf && \ | ||
rename -v 's/[^a-zA-Z0-9_.-]//g' *.pdf | ||
#+END_SRC |
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,37 @@ | ||
# TODO: ifeq no funciona como se espera con include | ||
# https://stackoverflow.com/questions/27268450/gnu-make-ifeq-comparison-not-working | ||
-include config.cfg | ||
-include unix-utils.mk | ||
|
||
.DEFAULT_GOAL=help | ||
MAKEFILE_DEPTH=1 | ||
|
||
libros = $(wildcard *.$(BOOK_EXTENSION)) | ||
archivos= $(wildcard *.$(COMPRESSED_FILE_EXTENSION)) | ||
|
||
libros_comprimidos = $(libros:.$(BOOK_EXTENSION)=.$(COMPRESSED_FILE_EXTENSION)) | ||
archivos_descomprimidos = $(archivos:.$(COMPRESSED_FILE_EXTENSION)=.$(BOOK_EXTENSION)) | ||
|
||
##@ Utilidades | ||
h help: ## Mostrar menú de ayuda | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nOpciones para usar:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
clean-pdf-files: | ||
$(RM) *.$(BOOK_EXTENSION) | ||
|
||
clean-tar-files: | ||
$(RM) *.$(COMPRESSED_FILE_EXTENSION) | ||
|
||
##@ Comandos | ||
crear-categoria: ## Ex. make crear-categoria NAME=topologias-de-red | ||
@$(MKDIR) $(NAME) && \ | ||
cp .template.mk $(NAME)/Makefile | ||
|
||
descargar: ## Ej. make descargar URL=http://ruta/archivo.pdf | ||
@curl -O $(URL) | ||
|
||
listar-libros: ## | ||
@$(LS) *.$(BOOK_EXTENSION) */*.$(BOOK_EXTENSION) $(STD_ERR)>$(NULL_DEVICE) | \ | ||
$(NAWK_LIBROS) | $(COLUMN) | ||
|
||
.PHONY: h help renombrar-archivos comprimir-archivos extraer-archivos crear-categoria listar-libros |
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 @@ | ||
#+TITLE: PDF Book Manager | ||
* ¿Qué es? | ||
- Compresor de mútiples archivos ~.pdf~ a un formato ~.tar.gz~ (viceversa para extraerlos) | ||
* ¿Por qué lo utilizo? | ||
- Comprimir/Descomprimir múltiples archivos de un directorio de forma individual con un simple comando | ||
- Formatear nombre de archivos sin espacios ni mayúsculas | ||
* ¿Qué ventajas tiene? | ||
- Subir a un repositorio git libros con un alto nivel de compresión (/por default no sube los archivos .pdf sólo los comprimidos/) | ||
* ¿Cómo utilizar? | ||
#+BEGIN_SRC shell | ||
# menú de ayuda con todos los comandos de la aplicación | ||
help | ||
|
||
# crea un directorio con un Makefile con las operaciones de la aplicación | ||
crear-categoria NAME=nombre | ||
|
||
# remueve espacios y mayúsculas de los nombres de los archivos | ||
formatear-archivos | ||
|
||
# lista los archivos por su nombre, ordenados por su tamaño | ||
listar-libros | ||
|
||
# comprime cada archivo .pdf de forma individual a un formato .tar.gz | ||
comprimir-archivos | ||
|
||
# extrae el .pdf de cada archivok .tar.gz | ||
extraer-archivos | ||
#+END_SRC | ||
* Libros Referentes | ||
1. Managing Projects with GNU Make, 3rd Edition | ||
* TODO Referencias | ||
#+BEGIN_COMMENT | ||
pendiente validar | ||
1. https://phoenixnap.com/kb/rename-file-linux | ||
2. https://www.computerhope.com/unix/rename.htm | ||
3. https://autotel.co/posts/2021-01-06-linux-regex-batch-file-renaming | ||
4. https://swapps.com/blog/rename-files-in-bash-with-regular-expresions/ | ||
5. https://stackoverflow.com/questions/21359567/awk-sed-regex-to-rename-files | ||
6. https://www.javatpoint.com/linux-rename-regular-expression | ||
#+END_COMMENT |
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,2 @@ | ||
BOOK_EXTENSION=pdf | ||
COMPRESSED_FILE_EXTENSION=tar.gz |
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,7 @@ | ||
-include ../config.cfg | ||
-include ../unix-utils.mk | ||
-include ../Makefile | ||
-include ../tar-compression.mk | ||
|
||
# Nota: es importante el orden de los include | ||
MAKEFILE_DEPTH = 2 |
Binary file not shown.
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,7 @@ | ||
-include ../config.cfg | ||
-include ../unix-utils.mk | ||
-include ../Makefile | ||
-include ../tar-compression.mk | ||
|
||
# Nota: es importante el orden de los include | ||
MAKEFILE_DEPTH = 2 |
Binary file not shown.
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,7 @@ | ||
-include ../config.cfg | ||
-include ../unix-utils.mk | ||
-include ../Makefile | ||
-include ../tar-compression.mk | ||
|
||
# Nota: es importante el orden de los include | ||
MAKEFILE_DEPTH = 2 |
Binary file not shown.
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,7 @@ | ||
-include ../config.cfg | ||
-include ../unix-utils.mk | ||
-include ../Makefile | ||
-include ../tar-compression.mk | ||
|
||
# Nota: es importante el orden de los include | ||
MAKEFILE_DEPTH = 2 |
Binary file not shown.
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,21 @@ | ||
##@ Tareas de compresión | ||
|
||
%.$(COMPRESSED_FILE_EXTENSION): %.$(BOOK_EXTENSION) | ||
@echo "Comprimiendo $* en $@ .." | ||
@$(TAR_COMPRESS) $@ $< | ||
|
||
# IMPORTANTE: | ||
# %.pdf: %.tar.gz genería una dependencia circular con %.tar.gz: %.pdf | ||
%.$(BOOK_EXTENSION): | ||
$(info Extrayendo $< $*.) | ||
@$(TAR_EXTRACT) $*.$(COMPRESSED_FILE_EXTENSION) | ||
|
||
comprimir-archivos: $(libros_comprimidos) ## | ||
@echo "Listo! archivos comprimidos!" | ||
|
||
extraer-archivos: $(archivos_descomprimidos) ## | ||
@echo "Listo! archivos extraídos!" | ||
|
||
formatear-archivos: ## renombra los nombres de los archivos (sugerido previo a comprimir) | ||
@$(RENAME) 'y/A-Z _/a-z-/' *.$(BOOK_EXTENSION) && \ | ||
$(RENAME) 's/[^a-zA-Z0-9_.-]//g' *.$(BOOK_EXTENSION) |
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 @@ | ||
STD_ERR=2 | ||
NULL_DEVICE=/dev/null | ||
|
||
MKDIR=mkdir -p | ||
LS=ls -lth --time-style=long-iso | ||
RENAME=rename -v | ||
RM=rm -vf | ||
COLUMN=column -t | ||
|
||
TAR_EXTRACT=tar -xvf | ||
TAR_COMPRESS=tar -cvzf | ||
|
||
ifeq ($(wildcard */*.pdf),) | ||
NAWK_LIBROS=nawk 'BEGIN{print "\# Nombre Tamaño Fecha Hora"} {print NR, $$NF, $$5, $$6, $$7}' | ||
else | ||
NAWK_LIBROS=nawk 'BEGIN{print "\# Categoria Nombre Tamaño Fecha Hora"} {split($$NF, DIR, "/"); print NR, DIR[1], DIR[2], $$5, $$6, $$7}' | ||
endif | ||
|