hugadams/pyuvvis — pull request #171 by hugadams (2014-12-07 00:11:29+00:00)
Title: Projectrename

Renamed the entire project to scikit spec. Script to do it attached below. Some issues with this script are that it doesn't output into a new directory, so I first copy pasted pyuvvis to a dummy folder, foouvvis, and worked on this folder. Since all contents are copied, the git connection was not severed. Here are the steps I followed losely:

1. Make a backup branch "backup" and push to github!
2. Create a new branch, "projecrename" to work out of.
3. Copy locally my pyuvvis folder into a new folder called foouvvis.
- git connection should still be fine.
4. Run the script (PARAMETERS IN BOTTOM) with case insensitive on. (Note, this will replace long names like PYuvvis with skspec. In many isntances, I wanted scikit-spectra not skspec, but had to manually fix these later.
5.. Either manually change all instances of pyuvvis in top-level files including setup.py, readme, licesne, or copy paste them in a new directory and run script on that directory to do a batch replace.
6. Go through and change instances of shortname to longname. For example, readme header was "skspec: Tools for explorative spectroscopy" and here' s an instance where I actually want it to read "sickit-spectra: Tools..."
7. Carefully inspect and pull.

```
""" Replace an entire project (directory of files) recursively with a
new string. Used to rename pyuvvis to scikit-spectra. First, we had to
rename imports/function calls(pyuvvis.) with (skpectra.). I ran this program
several times to do each replace, so it's not built to do it all in one-go.
Writes to an outdir, so if I have to replace say 3 phrases, have to run it
iteratively. IE

Trial 1: pyuvvis ---> skspec (replaced .pyuvvis with .skspec)
Trial 2: skspec ----> skspec2 (replaced ...?)

"""

import os
import re
import os.path as op

# http://stackoverflow.com/questions/919056/python-case-insensitive-replace
def replace(contents, old, new, ignorecase=False):
""" Replace old expression with new expression in a string (contents).
"""
if ignorecase:
return re.sub(old, new, contents, flags = re.IGNORECASE)
else:
return re.sub(old, new, contents)

def hasexpression(path, expression, ignorecase = False):
""" Return the contents of a file if file contains string expression. """
with open(path, 'r') as f:
contents = f.read()
if ignorecase:
foundlist = re.findall(expression, contents, flags = re.IGNORECASE)
else:
foundlist = re.findall(expression, contents)
if foundlist:
print 'found "%s" in %s' % (expression, op.basename(path))
return contents

def main(inroot):
""" Walk the directory tree, replace expressions only .py, .ipynb and
.rst files """

for (rootpath, rootdirs, rootfiles) in \
os.walk(inroot,
topdown=True,
onerror=None,
followlinks=False
):

for folder in rootdirs:

indir = op.join(rootpath, folder)

for shortname in os.listdir(indir):
outpath = op.join(rootpath, shortname) #Cut extension is here

# SHOULDR REFACTOR FROM HERE DOWN TO BECAUSE DIDN"T INCLUDE
# FILES IN TOPLEVEL LIKE SETUP.PY, LICENSE.TXT etc...

filepath = op.join(indir, shortname)
newcontents = None

if op.isfile(filepath):
if op.splitext(filepath)[-1] in ALLOWED:
contents = hasexpression(filepath, EXP_OLD, ignorecase=IGNORECASE)
if contents:
newcontents = replace(contents,
EXP_OLD,
EXP_NEW,
ignorecase=IGNORECASE)
if newcontents:
print 'replace in', filepath

if not DRYRUN:
with open(filepath, 'w') as f:
f.write(newcontents)

# print filepath, 'OLD'
# print outpath, 'NEW'

# print folder, 'folder'

if __name__ == '__main__':
ALLOWED=['.py','.rst','.ipynb', '.txt']

EXP_OLD = 'pyuvvis'
EXP_NEW = 'skspec'
IGNORECASE = True

DRYRUN = False
DIRECTORY = 'foouvvis'
main(DIRECTORY)
```