Skip to content

Instantly share code, notes, and snippets.

@reinbach
Created February 13, 2012 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinbach/1818298 to your computer and use it in GitHub Desktop.
Save reinbach/1818298 to your computer and use it in GitHub Desktop.
Convert dir from url_pattern to brand_code
#!/usr/bin/env python
#
# brand templates are stored in url_pattern values
# script converts these dirnames to brand_code values
#
# for each dir in the brand template
# find the brand based on the dir being url_pattern of a brand
# get the brand code for the brand
# change the dir name to the brand_code value
# NOTE this script needs to be run from the repo src dir
import MySQLdb
import os
from pbs import hg
BRAND_TEMPLATE_DIR = "/home/greg/env/cs_notify/master/templates"
DB_NAME = 'cashstardb'
DB_USER = 'user'
DB_PASS = 'pass'
#---------------------------------------------------------------------------
def get_brand_dirnames():
data = []
root_dir = "{0}/brand/".format(BRAND_TEMPLATE_DIR)
for top, dirnames, filenames in os.walk(root_dir):
if top == root_dir:
for brand in dirnames:
data.append("{0}".format(brand))
return data
#---------------------------------------------------------------------------
def get_brand_data():
db = MySQLdb.connect(user=DB_USER, passwd=DB_PASS, db=DB_NAME)
cur = db.cursor()
cur.execute("SELECT url_pattern, brand_code FROM brand_brand")
return cur.fetchall()
#---------------------------------------------------------------------------
def main():
brand_urlpatterns = get_brand_dirnames()
brands = get_brand_data()
brand_missing = []
for url_pattern, brand_code in brands:
if url_pattern in brand_urlpatterns:
# do the actual renaming
os.rename(
"{0}/brand/{1}".format(BRAND_TEMPLATE_DIR, url_pattern),
"{0}/brand/{1}".format(BRAND_TEMPLATE_DIR, brand_code),
)
try:
hg.remove("templates/brand/{0}/".format(url_pattern))
except:
# appears we get untracked error on the dir
# so ignoring for now
pass
brand_urlpatterns.remove(url_pattern)
else:
brand_missing.append(brand_code)
# brands that are missing, no template dir found
print "Missing brand template dir: {0}".format(brand_missing)
print "Missing brand codes: {0}".format(brand_urlpatterns)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment