Pelican's newpost outputs to wrong location - makefile

I'm following this guidance to Make markdown to Pelican blog posts. The guidance says make newpost NAME='testx' will output to ./content/pages, but my files are just going to ./content.
The script appears to use the PAGESDIR variable for output directory, initialised as: PAGESDIR=$(INPUTDIR)/pages where INPUTDIR is $(BASEDIR)/content.
The pages directory does exist in content, with permissions drwxr-xr-x (same as content).
Any idea why this isn't working in practice? This is my Makefile script:
PY?=python3
PELICAN?=pelican
PELICANOPTS=
EDITOR ?= atom
BASEDIR=$(CURDIR)
INPUTDIR=$(BASEDIR)/content
OUTPUTDIR=$(BASEDIR)/output
CONFFILE=$(BASEDIR)/pelicanconf.py
PUBLISHCONF=$(BASEDIR)/publishconf.py
FTP_HOST=localhost
FTP_USER=anonymous
FTP_TARGET_DIR=/
SSH_HOST=localhost
SSH_PORT=22
SSH_USER=root
SSH_TARGET_DIR=/var/www
S3_BUCKET=my_s3_bucket
CLOUDFILES_USERNAME=my_rackspace_username
CLOUDFILES_API_KEY=my_rackspace_api_key
CLOUDFILES_CONTAINER=my_cloudfiles_container
DROPBOX_DIR=~/Dropbox/Public/
GITHUB_PAGES_BRANCH=master
DEBUG ?= 0
ifeq ($(DEBUG), 1)
PELICANOPTS += -D
endif
RELATIVE ?= 0
ifeq ($(RELATIVE), 1)
PELICANOPTS += --relative-urls
endif
help:
#echo 'Makefile for a pelican Web site '
#echo ' '
#echo 'Usage: '
#echo ' make html (re)generate the web site '
#echo ' make clean remove the generated files '
#echo ' make regenerate regenerate files upon modification '
#echo ' make publish generate using production settings '
#echo ' make serve [PORT=8000] serve site at http://localhost:8000'
#echo ' make serve-global [SERVER=0.0.0.0] serve (as root) to $(SERVER):80 '
#echo ' make devserver [PORT=8000] start/restart develop_server.sh '
#echo ' make stopserver stop local server '
#echo ' make ssh_upload upload the web site via SSH '
#echo ' make rsync_upload upload the web site via rsync+ssh '
#echo ' make dropbox_upload upload the web site via Dropbox '
#echo ' make ftp_upload upload the web site via FTP '
#echo ' make s3_upload upload the web site via S3 '
#echo ' make cf_upload upload the web site via Cloud Files'
#echo ' make github upload the web site via gh-pages '
#echo ' '
#echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html '
#echo 'Set the RELATIVE variable to 1 to enable relative urls '
#echo ' '
html:
$(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
clean:
[ ! -d $(OUTPUTDIR) ] || rm -rf $(OUTPUTDIR)
regenerate:
$(PELICAN) -r $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
serve:
ifdef PORT
cd $(OUTPUTDIR) && $(PY) -m pelican.server $(PORT)
else
cd $(OUTPUTDIR) && $(PY) -m pelican.server
endif
serve-global:
ifdef SERVER
cd $(OUTPUTDIR) && $(PY) -m pelican.server 80 $(SERVER)
else
cd $(OUTPUTDIR) && $(PY) -m pelican.server 80 0.0.0.0
endif
devserver:
ifdef PORT
$(BASEDIR)/develop_server.sh restart $(PORT)
else
$(BASEDIR)/develop_server.sh restart
endif
stopserver:
$(BASEDIR)/develop_server.sh stop
#echo 'Stopped Pelican and SimpleHTTPServer processes running in background.'
publish:
$(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR) -s $(PUBLISHCONF) $(PELICANOPTS)
ssh_upload: publish
scp -P $(SSH_PORT) -r $(OUTPUTDIR)/* $(SSH_USER)#$(SSH_HOST):$(SSH_TARGET_DIR)
rsync_upload: publish
rsync -e "ssh -p $(SSH_PORT)" -P -rvzc --delete $(OUTPUTDIR)/ $(SSH_USER)#$(SSH_HOST):$(SSH_TARGET_DIR) --cvs-exclude
dropbox_upload: publish
cp -r $(OUTPUTDIR)/* $(DROPBOX_DIR)
ftp_upload: publish
lftp ftp://$(FTP_USER)#$(FTP_HOST) -e "mirror -R $(OUTPUTDIR) $(FTP_TARGET_DIR) ; quit"
s3_upload: publish
s3cmd sync $(OUTPUTDIR)/ s3://$(S3_BUCKET) --acl-public --delete-removed --guess-mime-type --no-mime-magic --no-preserve
cf_upload: publish
cd $(OUTPUTDIR) && swift -v -A https://auth.api.rackspacecloud.com/v1.0 -U $(CLOUDFILES_USERNAME) -K $(CLOUDFILES_API_KEY) upload -c $(CLOUDFILES_CONTAINER) .
github: publish
ghp-import -m "Generate Pelican site" -b $(GITHUB_PAGES_BRANCH) $(OUTPUTDIR)
git push origin $(GITHUB_PAGES_BRANCH)
.PHONY: html help clean regenerate serve serve-global devserver stopserver publish ssh_upload rsync_upload dropbox_upload ftp_upload s3_upload cf_upload github
## make newpost function
# https://github.com/getpelican/pelican/wiki/Tips-n-Tricks
PAGESDIR=$(INPUTDIR)/pages
DATE := $(shell date +'%Y-%m-%d %H:%M:%S')
SLUG := $(shell echo '${NAME}' | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
EXT ?= md
newpost:
ifdef NAME
echo "Title: $(NAME)" > $(INPUTDIR)/$(SLUG).$(EXT)
echo "Slug: $(SLUG)" >> $(INPUTDIR)/$(SLUG).$(EXT)
echo "Date: $(DATE)" >> $(INPUTDIR)/$(SLUG).$(EXT)
echo "" >> $(INPUTDIR)/$(SLUG).$(EXT)
echo "" >> $(INPUTDIR)/$(SLUG).$(EXT)
${EDITOR} ${INPUTDIR}/${SLUG}.${EXT} &
else
#echo 'Variable NAME is not defined.'
#echo 'Do make newpost NAME='"'"'Post Name'"'"
endif
editpost:
ifdef NAME
${EDITOR} ${INPUTDIR}/${SLUG}.${EXT} &
else
#echo 'Variable NAME is not defined.'
#echo 'Do make editpost NAME='"'"'Post Name'"'"
endif
newpage:
ifdef NAME
echo "Title: $(NAME)" > $(PAGESDIR)/$(SLUG).$(EXT)
echo "Slug: $(SLUG)" >> $(PAGESDIR)/$(SLUG).$(EXT)
echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
${EDITOR} ${PAGESDIR}/${SLUG}.$(EXT)
else
#echo 'Variable NAME is not defined.'
#echo 'Do make newpage NAME='"'"'Page Name'"'"
endif
editpage:
ifdef NAME
${EDITOR} ${PAGESDIR}/${SLUG}.$(EXT)
else
#echo 'Variable NAME is not defined.'
#echo 'Do make editpage NAME='"'"'Page Name'"'"
endif

Just replace INPUTDIR with PAGESDIR in newpost recipe:
newpost:
ifdef NAME
echo "Title: $(NAME)" > $(PAGESDIR)/$(SLUG).$(EXT)
echo "Slug: $(SLUG)" >> $(PAGESDIR)/$(SLUG).$(EXT)
echo "Date: $(DATE)" >> $(PAGESDIR)/$(SLUG).$(EXT)
echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
${EDITOR} ${PAGESDIR}/${SLUG}.${EXT} &
else
#echo 'Variable NAME is not defined.'
#echo 'Do make newpost NAME='"'"'Post Name'"'"
endif

Related

How can I have a Makefile target update dependent on the value of an environment variable?

I have a make target that will have different output depending on the value of an environment variable.
How can I:
skip the dependency and not re-make the target if the environment variable has not changed the last run
make or re-make the target if the environment variable is not set or has changed
I thought I could create or conditionally update a file with the current environment variable value and then use that file as a make dependency. I couldn't find an elegant way to do that with native tools. (sed -i always updated the file's timestamp, maybe awk is possible)
How about using a shell script to update a file that holds the variable value?
SHELL = /bin/bash
var_file := var.txt
var_name := NAME
is_var_updated = [[ ! -e $(var_file) ]] || [[ "$$(< $(var_file))" != "$($(var_name))" ]]
update_var_file = echo "$($(var_name))" > $(var_file)
$(shell $(is_var_updated) && $(update_var_file))
output.txt: $(var_file)
echo "Name is $$NAME" > $#
This works like this.
$ ls
Makefile
$ NAME=foo make
echo "Name is $NAME" > output.txt
$ NAME=foo make
make: `output.txt' is up to date.
$ NAME=bar make
echo "Name is $NAME" > output.txt
Make conditionals could be a starting point:
.PHONY: all
FILE := foobar
ifdef ENV_VAR
OLD_ENV_VAR := $(shell [ -f $(FILE) ] && cat $(FILE))
ifeq ($(ENV_VAR),$(OLD_ENV_VAR))
DONTRUN := 1
endif
endif
ifdef DONTRUN
all:
#echo 'ENV_VAR unmodified'
else
$(shell printenv ENV_VAR > $(FILE))
all:
#echo 'ENV_VAR undefined or modified'
endif

MakeFile Bash - Check if gzipped file is empty

I have a makefile as below . It checks if the gzip file is empty , if not it must called the test_recipe else print out a message . But in the below case , it calls the test_recipe anyway. Not sure what im doing wrong or what needs to be done to check the gzip content .
SHELL := /bin/bash
current_month = $(shell date +"%m")
current_year = $(shell date +"%Y")
current_date = $(shell date +"%Y-%m-%d")
check_file_dump_empty :
# [ -s test_data.txt.gz ] && $(MAKE) test_recipe || echo "file is empty"
test_recipe :
#bash sqoop.sh ${current_date} ${current_year}
Thanks in advance .
A gz file which exists but is empty will not be zero bytes in size. Your check is wrong. You'll want to decompress it and check whether there is any output.
Here's a refactoring which also avoids overriding the SHELL variable for no good reason.
current_month = $(shell date +"%m")
current_year = $(shell date +"%Y")
current_date = ${current_year}-${current_month}
check_file_dump_empty:
gzip -dc test_data.txt.gz | grep -q '^' \
&& $(MAKE) test_recipe \
|| echo "file is empty"
test_recipe:
bash sqoop.sh "${current_date}" "${current_year}"
Instead of explicitly calling bash, making the script file executable and ensuring that it has a correct shebang on the first line is usually preferable.

Bash trap does not run when the exit 1 codes put at some random location

I am using the bash trap to make sure one function runs at any cost. I know trap is not specific to exitO or 1. Here is what I have done.
#!/bin/bash
set -e
#array to store server and deployed status
declare -A server_deployed
#path to file containing the server inventory
readonly filepath="/var/jenkins_home/workspace/server_list.txt"
#array to store to list of IPS
declare -A result #associative array
if [[ $TARGET == "ALL" ]]; then
while read line ; do
server_name=` echo $line | cut -d= -f1 `
result+=(["$server_name"]=${line#*=})
done < $filepath
else
singleserver=`cat $filepath | grep "$TARGET"`
server_name=`echo $singleserver| cut -d= -f1 ` # get the servername
serverip=`echo $singleserver| cut -d= -f2 ` # get tje server ip
result+=(["$server_name"]=$serverip) # gets the ip which is after equalto
fi
#function to send slack notification everytime
function sendMessage(){
for sd in "${!server_deployed[#]}"
do
echo "#########################################################"
echo "Sucecssfully deployed on $sd"
echo "#########################################################"
done
#let find the unsuccess list ,for which we need to find the array diff
for server_name in "${!result[#]}"
do
for sd in "${!server_deployed[#]}"
do
if [[ "$server_name" != "$sd" ]]; then
echo "Failed to deploy on $server_name"
fi
done
done
}
trap "sendMessage" INT EXIT
for server in "${!result[#]}"
do
upstream="MBM-TEST-ADMIN-BUILD"
#bundle filename
bundle="mbm_admin_dist"
name=$server
instance=${result[$name]}
#------copying the hash.php in build job to the deployed server home location
# ssh and move to desired location
sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /tmp/test/admin_hash.json ubuntu#$instance:/home/ubuntu/
mbmcms_workspace="/var/jenkins_home/workspace/$upstream"
cd $mbmcms_workspace
#remove if exist
if [ -e "$bundle.zip" ]
then
echo "Already Exist so Removing it First !!"
rm -f $bundle.zip
fi
#check if the dist folder exist that comes from successful build
if [ ! -d "dist" ]; then
echo "--------------------------------------------------------------"
echo "The dist folder does not exist, Please run BUILD job First !!"
echo "--------------------------------------------------------------"
exit 1
fi
#zip the file from the TEST & Build jobs
echo "starting to zip mbm-admin dist file created after test and build success!!"
zip --symlinks -x *.git* -r $bundle ./dist
#---------------------------------------------------------------
# copy development.php if non-production else copy production.php
# the file goes to the view/cms/
#---------------------------------------------------------------
sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null development.php ubuntu#$instance:/home/ubuntu
sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null production.php ubuntu#$instance:/home/ubuntu
#copy the bundle file to instance
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r $bundle.zip ubuntu#$instance:/home/ubuntu/
ssh -tt -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$instance '
SOURCE_FOLDER='/home/ubuntu/mbm-admin/dist/'
DESTINATION_FOLDER='/home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console/dist/'
rm -rf mbm-admin
mkdir -p mbm-admin
mv mbm_admin_dist.zip mbm-admin
cd mbm-admin
unzip mbm_admin_dist.zip
rm -f mbm_admin_dist.zip
sudo rsync -arz --force --delete --progress $SOURCE_FOLDER $DESTINATION_FOLDER
#moving the copied file to the /app/views/admin/admin_console
sudo mv /home/ubuntu/development.php /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console
sudo mv /home/ubuntu/production.php /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console
#moving the hash.php to the desired location
sudo cp -fv /home/ubuntu/admin_hash.json /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console/hash.json
'
server_deployed+=(["$server"]="SUCCESS")
done
Here is the trap , as seen the script section
trap "sendMessage" INT EXIT
I need to run the sendMessage function whenever some exit code is
detected or the program exits due to any error.
Problem:
When I put the exit 1 at the end of the script the function gets called by the trap but suppose, If I put it somewhere in the middle or exactly after the main for loop starts the trap does not catch the exit code.
What am I not understanding or missing exactly in here?

How to use eval Makefile variable from recipe to conditionally execute some commands in recipe

Goal is to apply patch ONLY if patch is not present. If patch is present don't do anything.
I used below makefile rule.
C_FILE_PATCH_SIG=###MAGIC_CODE;
C_FILE_CODE=~/code/file.c
C_PATCH_FILE=~/test.patch
.tmp/patch_c:
cp ${C_PATCH_FILE} ${SDK}
ifneq ($(PATCH_DONE), 1)
$(MAKE) applypatch || $(MAKE) helppatch
endif
#echo DONE > .tmp/patch_c
applypatch:
#echo "Patching ${C_FILE_CODE}"
if grep -Fq '${C_FILE_PATCH_SIG}' ${C_FILE_CODE} ; \
then \
echo 1 > .tmp/PATCH_PRESENT_file; \
else \
echo 0 > .tmp/PATCH_PRESENT_file;\
fi
cat .tmp/PATCH_PRESENT_file
# $(eval PATCH_PRESENT := `cat .tmp/PATCH_PRESENT_file`)
$(eval PATCH_PRESENT := $(shell cat .tmp/PATCH_PRESENT_file))
#echo "WWWWWW PATCH_PRESENT=[$(PATCH_PRESENT)] WWWWWWW"
ifeq ($(PATCH_PRESENT), 0)
#echo "Applying the patch $(PATCH_PRESENT)"
cd ~/code && git apply -v ${C_PATCH_FILE}
else
#echo "NOT Applying the patch $(PATCH_PRESENT)"
endif
helppatch:
#echo -e "\n\n\n"
#echo -e "++++++++++ Apply below patch manually then run 'make build PATCH_DONE=1' ++++++++++\n\n"
#echo -e "+++++++++++++++++++++++++++++++++++++"
#cat ${C_PATCH_FILE}
#echo -e "+++++++++++++++++++++++++++++++++++++"
#echo -e "\n\n\n"
false
But it always evaluates to the else part of ifeq.
Where am I doing wrong?
If I use the patch command of git withing the shell multiline I loose the error code returned by the git patch.
Thanks in advance...
Your ifeq will be evaluated when the makefile is first read (as opposed to when the recipe is run). The eval, on the other hand, will not be executed until the recipe is run (afterwards). Thus, PATCH_PRESENT is not equal to 0 at parse time, and make will expand the else portion of the clause. By the time the eval is run, the if statement is already evaluated and gone from memory.
BTW, a cleaner way to do this is to do everything in bash:
applypatch:
#echo "Patching ${C_FILE_CODE}"
#if grep -Fq '${C_FILE_PATCH_SIG}' ${C_FILE_CODE}; then \
echo "NOT Applying the patch"; \
else \
echo "Applying the patch"; \
cd ~/code && git apply -v ${C_PATCH_FILE}; \
fi

convert sh file to bat file, existance of directories

I would like to convert this code from sh file to .bat file, how can i do?
# Validate directories
if [ ! -d "$DIRA" -o \
! -d "$DIRB" -o \
! -d "$DIRC" ]
then
echo " invalid path"
exit 1
fi
It can be written like this in .bat file:
rem validate directories
set VALID_PATH=true
IF NOT EXIST %DIRA%\NUL set VALID_PATH=false
IF NOT EXIST %DIRB%\NUL set VALID_PATH=false
IF NOT EXIST %DIRC%\NUL set VALID_PATH=false
if %VALID_PATH%==false (
echo invalid path
goto done
)

Resources