Working full stack at a marketing firm means making lots of copies of websites.
We had different kinds of needs and at one end of the spectrum were websites with almost completely static content. Making these websites used to take my company days. I made a command line tool that knocks it out in seconds.
Check out my git repo here: Site-Generator
What this solves
I needed a way to make many websites automatically based on just their name. Those websites were all populated with the same message. To make this program I wrote a function that makes directories given a path name.
The function uses the ‘parents’ flag of the mkdir function. That option also makes parent directories.
Create web folders programmatically
#!/usr/bin/python import os import errno # the folder where the assets will be created # e.g. /var/www/html/public targetFolder = "C:\\User\\dummy\\test_folder"; # fn to replicate mkdir -p def mkdir_p(path): try: os.makedirs(path) print("successfully created " + path) except OSError as exc: if exc.errno == errno.EEXIST and os.path.isdir(path): print("Error: The path '" + path + "' already exists") pass # end mkdir_p()
Below you will see a simplified version of what is happening at the opening of main.py. The program needs a set of file paths to replicate. It goes to the public folder and
#!/usr/bin/python import os import CreatePath as CP # site paths desired domains = [ "domain1.com/one/two/three/four/", "domain2.com/one/two", "domain3.com/one/", "domain4.com/one/two/three/four/", "domain5.com/one/two/three/" ] # go to target folder os.chdir(CP.targetFolder) # make directories on target folder for i in domains: CP.mkdir_p(i) # end step 1
Then put content in the folder
You can do this many ways. If you’re still following along in my main.py file you’re probably wondering what’s this about target branches. The script will go to each directory of each website and create an index.php file. The target branches are the list of every directory path to visit. The way script is written now, it writes “<?php echo ‘success’ ?>” to the index file.
target_branches = [ "domain1.com/one/two/three/four/", "domain1.com/one/two/three/", "domain1.com/one/two/", "domain1.com/one/", "domain2.com/one/two/", "domain2.com/one/", "domain3.com/one/", "domain4.com/one/two/three/four/", "domain4.com/one/two/three/", "domain4.com/one/two/", "domain4.com/one/", "domain5.com/one/two/three/" "domain5.com/one/two/" "domain5.com/one/" ] # go to every folder for i in target_branches: print("Target folder: " + i) # create an index.php file_name = i + "\\index.php" IFH = open(file_name, 'w') print("created index.php") # write the forwarding script to the index # todo write the proper php statement payload = "<?php echo 'success' ?>" IFH.write(payload) print("forwarding script written to file") IFH.close() print("File saved\n")
All-together there are only a few moving parts but it still takes a lot of attention to detail to pull this off.
See the full code here: Site-Generator